public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Jiri Pirko <jpirko@redhat.com>
To: Patrick McHardy <kaber@trash.net>
Cc: netdev@vger.kernel.org, davem@davemloft.net,
	shemminger@vyatta.com, eric.dumazet@gmail.com
Subject: Re: [PATCH net-next-2.6] net: replace hooks in __netif_receive_skb
Date: Thu, 27 May 2010 16:57:11 +0200	[thread overview]
Message-ID: <20100527145710.GB6916@psychotron.lab.eng.brq.redhat.com> (raw)
In-Reply-To: <4BFE7C7E.8090803@trash.net>

Thu, May 27, 2010 at 04:06:54PM CEST, kaber@trash.net wrote:
>Jiri Pirko wrote:
>> @@ -511,10 +512,16 @@ static void macvlan_setup(struct net_device *dev)
>>  	dev->tx_queue_len	= 0;
>>  }
>>  
>> +static struct netdev_rx_handler macvlan_rx_handler = {
>> +	.order		= NETDEV_RX_HANDLER_ORDER_MACVLAN,
>> +	.callback	= macvlan_handle_frame,
>> +};
>
>It seems this could be const since you duplicate it on
>registration.

Noted.
>
>> +
>>  static int macvlan_port_create(struct net_device *dev)
>>  {
>>  	struct macvlan_port *port;
>>  	unsigned int i;
>> +	int err;
>>  
>>  	if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
>>  		return -EINVAL;
>> @@ -528,6 +535,15 @@ static int macvlan_port_create(struct net_device *dev)
>>  	for (i = 0; i < MACVLAN_HASH_SIZE; i++)
>>  		INIT_HLIST_HEAD(&port->vlan_hash[i]);
>>  	rcu_assign_pointer(dev->macvlan_port, port);
>> +
>> +	err = netdev_rx_handler_register(dev, &macvlan_rx_handler);
>> +	if (err) {
>> +		rcu_assign_pointer(dev->macvlan_port, NULL);
>> +		synchronize_rcu();
>> +		kfree(port);
>> +		return err;
>> +	}
>
>I'd prefer goto-based unroll since that makes changes in the
>future easier.

Ok, I thought this won't be necessary in this case, but right, looks better.
>
>> +
>>  	return 0;
>>  }
>>  
>> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
>> index a1bff65..8e95b2d 100644
>> --- a/include/linux/netdevice.h
>> +++ b/include/linux/netdevice.h
>> @@ -254,6 +254,15 @@ struct netdev_hw_addr_list {
>>  #define netdev_for_each_mc_addr(ha, dev) \
>>  	netdev_hw_addr_list_for_each(ha, &(dev)->mc)
>>  
>> +
>> +struct netdev_rx_handler {
>> +	struct list_head	list;
>> +	unsigned int		order;
>> +#define NETDEV_RX_HANDLER_ORDER_BRIDGE	1
>> +#define NETDEV_RX_HANDLER_ORDER_MACVLAN	2
>
>Any reason for not using an enum?

No, I can use enum, but this "inlining" gives a person who is looking at the
code the connection on the first look.

>
>> +	struct sk_buff		*(*callback)(struct sk_buff *skb);
>> +};
>> +
>>  struct hh_cache {
>>  	struct hh_cache *hh_next;	/* Next entry			     */
>>  	atomic_t	hh_refcnt;	/* number of users                   */
>> @@ -1031,6 +1040,10 @@ struct net_device {
>>  	/* GARP */
>>  	struct garp_port	*garp_port;
>>  
>> +	/* receive handlers (hooks) list */
>> +	spinlock_t		rx_handlers_lock;
>> +	struct list_head	rx_handlers;
>> +
>>  	/* class/net/name entry */
>>  	struct device		dev;
>>  	/* space for optional device, statistics, and wireless sysfs groups */
>> diff --git a/net/core/dev.c b/net/core/dev.c
>> index 6c82065..8d4a817 100644
>> --- a/net/core/dev.c
>> +++ b/net/core/dev.c
>> @@ -2744,6 +2688,82 @@ void netif_nit_deliver(struct sk_buff *skb)
>>  	rcu_read_unlock();
>>  }
>>  
>> +static bool rx_handlers_equal(struct netdev_rx_handler *rh1,
>> +			      struct netdev_rx_handler *rh2)
>> +{
>> +	return (rh1->order == rh2->order) && (rh1->callback == rh2->callback);
>> +}
>> +
>> +/**
>> + *	netdev_rx_handler_register - register receive handler
>> + *	@dev: device to register a handler for
>> + *	@rh: receive handler to register
>> + *
>> + *	Register a receive hander for a device. This handler will then be
>> + *	called from __netif_receive_skb. A negative errno code is returned
>> + *	on a failure.
>> + */
>> +int netdev_rx_handler_register(struct net_device *dev,
>> +			       struct netdev_rx_handler *rh)
>> +{
>> +	struct list_head *list, *add_after;
>> +	struct netdev_rx_handler *rh1;
>> +	int err = 0;
>> +
>> +	spin_lock_bh(&dev->rx_handlers_lock);
>
>Why are you using a spin lock and even disable BHs? This function
>should only be called from user context, so a mutex will work fine
>(and would fix the use of GFP_KERNEL in an atomic section).

Right, I will use rather rtnl_lock as suggested by Stephen.

>
>> +	add_after = &dev->rx_handlers;
>> +	list_for_each(list, &dev->rx_handlers) {
>
>Naming the element "list" is confusing. Also this should be
>using list_for_each_entry().

Well I'm not using list_for_each_entry because I use the list_head cursor as a
head in list_add_rcu (add_after assignment)

>
>> +		rh1 = list_entry(list, struct netdev_rx_handler, list);
>> +		if (rx_handlers_equal(rh, rh1)) {
>> +			err = -EEXIST;
>> +			goto unlock;
>> +		}
>> +		if (rh1->order > rh->order)
>> +			break;
>> +		add_after = list;
>> +	}
>> +	rh1 = kzalloc(sizeof(*rh), GFP_KERNEL);
>> +	if (!rh1) {
>> +		err = -ENOMEM;
>> +		goto unlock;
>> +	}
>> +
>> +	rh1->order = rh->order;
>> +	rh1->callback = rh->callback;
>> +	list_add_rcu(&rh1->list, add_after);
>> +
>> +unlock:
>> +	spin_unlock_bh(&dev->rx_handlers_lock);
>> +
>> +	return err;
>> +}
>> +EXPORT_SYMBOL(netdev_rx_handler_register);


Thanks all for comments, I'll send V2 soon

  reply	other threads:[~2010-05-27 14:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-27 13:49 [PATCH net-next-2.6] net: replace hooks in __netif_receive_skb Jiri Pirko
2010-05-27 14:06 ` Patrick McHardy
2010-05-27 14:57   ` Jiri Pirko [this message]
2010-05-27 14:17 ` Eric Dumazet
2010-05-27 15:02   ` Jiri Pirko
2010-05-27 14:47 ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100527145710.GB6916@psychotron.lab.eng.brq.redhat.com \
    --to=jpirko@redhat.com \
    --cc=davem@davemloft.net \
    --cc=eric.dumazet@gmail.com \
    --cc=kaber@trash.net \
    --cc=netdev@vger.kernel.org \
    --cc=shemminger@vyatta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox