Phonet: use per-namespace devices list

Signed-off-by: Rémi Denis-Courmont <remi.denis-courmont@nokia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
remi.denis-courmont@nokia 2009-01-23 03:00:30 +00:00 коммит произвёл David S. Miller
Родитель 6530e0fee1
Коммит 9a3b7a42bb
3 изменённых файлов: 81 добавлений и 40 удалений

Просмотреть файл

@ -28,7 +28,7 @@ struct phonet_device_list {
spinlock_t lock; spinlock_t lock;
}; };
extern struct phonet_device_list pndevs; struct phonet_device_list *phonet_device_list(struct net *net);
struct phonet_device { struct phonet_device {
struct list_head list; struct list_head list;

Просмотреть файл

@ -28,32 +28,41 @@
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/phonet.h> #include <linux/phonet.h>
#include <net/sock.h> #include <net/sock.h>
#include <net/netns/generic.h>
#include <net/phonet/pn_dev.h> #include <net/phonet/pn_dev.h>
/* when accessing, remember to lock with spin_lock(&pndevs.lock); */ struct phonet_net {
struct phonet_device_list pndevs = { struct phonet_device_list pndevs;
.list = LIST_HEAD_INIT(pndevs.list),
.lock = __SPIN_LOCK_UNLOCKED(pndevs.lock),
}; };
int phonet_net_id;
struct phonet_device_list *phonet_device_list(struct net *net)
{
struct phonet_net *pnn = net_generic(net, phonet_net_id);
return &pnn->pndevs;
}
/* Allocate new Phonet device. */ /* Allocate new Phonet device. */
static struct phonet_device *__phonet_device_alloc(struct net_device *dev) static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
{ {
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC); struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
if (pnd == NULL) if (pnd == NULL)
return NULL; return NULL;
pnd->netdev = dev; pnd->netdev = dev;
bitmap_zero(pnd->addrs, 64); bitmap_zero(pnd->addrs, 64);
list_add(&pnd->list, &pndevs.list); list_add(&pnd->list, &pndevs->list);
return pnd; return pnd;
} }
static struct phonet_device *__phonet_get(struct net_device *dev) static struct phonet_device *__phonet_get(struct net_device *dev)
{ {
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
struct phonet_device *pnd; struct phonet_device *pnd;
list_for_each_entry(pnd, &pndevs.list, list) { list_for_each_entry(pnd, &pndevs->list, list) {
if (pnd->netdev == dev) if (pnd->netdev == dev)
return pnd; return pnd;
} }
@ -68,32 +77,33 @@ static void __phonet_device_free(struct phonet_device *pnd)
struct net_device *phonet_device_get(struct net *net) struct net_device *phonet_device_get(struct net *net)
{ {
struct phonet_device_list *pndevs = phonet_device_list(net);
struct phonet_device *pnd; struct phonet_device *pnd;
struct net_device *dev; struct net_device *dev;
spin_lock_bh(&pndevs.lock); spin_lock_bh(&pndevs->lock);
list_for_each_entry(pnd, &pndevs.list, list) { list_for_each_entry(pnd, &pndevs->list, list) {
dev = pnd->netdev; dev = pnd->netdev;
BUG_ON(!dev); BUG_ON(!dev);
if (net_eq(dev_net(dev), net) && if ((dev->reg_state == NETREG_REGISTERED) &&
(dev->reg_state == NETREG_REGISTERED) &&
((pnd->netdev->flags & IFF_UP)) == IFF_UP) ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
break; break;
dev = NULL; dev = NULL;
} }
if (dev) if (dev)
dev_hold(dev); dev_hold(dev);
spin_unlock_bh(&pndevs.lock); spin_unlock_bh(&pndevs->lock);
return dev; return dev;
} }
int phonet_address_add(struct net_device *dev, u8 addr) int phonet_address_add(struct net_device *dev, u8 addr)
{ {
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
struct phonet_device *pnd; struct phonet_device *pnd;
int err = 0; int err = 0;
spin_lock_bh(&pndevs.lock); spin_lock_bh(&pndevs->lock);
/* Find or create Phonet-specific device data */ /* Find or create Phonet-specific device data */
pnd = __phonet_get(dev); pnd = __phonet_get(dev);
if (pnd == NULL) if (pnd == NULL)
@ -102,31 +112,33 @@ int phonet_address_add(struct net_device *dev, u8 addr)
err = -ENOMEM; err = -ENOMEM;
else if (test_and_set_bit(addr >> 2, pnd->addrs)) else if (test_and_set_bit(addr >> 2, pnd->addrs))
err = -EEXIST; err = -EEXIST;
spin_unlock_bh(&pndevs.lock); spin_unlock_bh(&pndevs->lock);
return err; return err;
} }
int phonet_address_del(struct net_device *dev, u8 addr) int phonet_address_del(struct net_device *dev, u8 addr)
{ {
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
struct phonet_device *pnd; struct phonet_device *pnd;
int err = 0; int err = 0;
spin_lock_bh(&pndevs.lock); spin_lock_bh(&pndevs->lock);
pnd = __phonet_get(dev); pnd = __phonet_get(dev);
if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs)) if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
err = -EADDRNOTAVAIL; err = -EADDRNOTAVAIL;
else if (bitmap_empty(pnd->addrs, 64)) else if (bitmap_empty(pnd->addrs, 64))
__phonet_device_free(pnd); __phonet_device_free(pnd);
spin_unlock_bh(&pndevs.lock); spin_unlock_bh(&pndevs->lock);
return err; return err;
} }
/* Gets a source address toward a destination, through a interface. */ /* Gets a source address toward a destination, through a interface. */
u8 phonet_address_get(struct net_device *dev, u8 addr) u8 phonet_address_get(struct net_device *dev, u8 addr)
{ {
struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
struct phonet_device *pnd; struct phonet_device *pnd;
spin_lock_bh(&pndevs.lock); spin_lock_bh(&pndevs->lock);
pnd = __phonet_get(dev); pnd = __phonet_get(dev);
if (pnd) { if (pnd) {
BUG_ON(bitmap_empty(pnd->addrs, 64)); BUG_ON(bitmap_empty(pnd->addrs, 64));
@ -136,30 +148,31 @@ u8 phonet_address_get(struct net_device *dev, u8 addr)
addr = find_first_bit(pnd->addrs, 64) << 2; addr = find_first_bit(pnd->addrs, 64) << 2;
} else } else
addr = PN_NO_ADDR; addr = PN_NO_ADDR;
spin_unlock_bh(&pndevs.lock); spin_unlock_bh(&pndevs->lock);
return addr; return addr;
} }
int phonet_address_lookup(struct net *net, u8 addr) int phonet_address_lookup(struct net *net, u8 addr)
{ {
struct phonet_device_list *pndevs = phonet_device_list(net);
struct phonet_device *pnd; struct phonet_device *pnd;
int err = -EADDRNOTAVAIL;
spin_lock_bh(&pndevs.lock); spin_lock_bh(&pndevs->lock);
list_for_each_entry(pnd, &pndevs.list, list) { list_for_each_entry(pnd, &pndevs->list, list) {
if (!net_eq(dev_net(pnd->netdev), net))
continue;
/* Don't allow unregistering devices! */ /* Don't allow unregistering devices! */
if ((pnd->netdev->reg_state != NETREG_REGISTERED) || if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
((pnd->netdev->flags & IFF_UP)) != IFF_UP) ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
continue; continue;
if (test_bit(addr >> 2, pnd->addrs)) { if (test_bit(addr >> 2, pnd->addrs)) {
spin_unlock_bh(&pndevs.lock); err = 0;
return 0; goto found;
} }
} }
spin_unlock_bh(&pndevs.lock); found:
return -EADDRNOTAVAIL; spin_unlock_bh(&pndevs->lock);
return err;
} }
/* notify Phonet of device events */ /* notify Phonet of device events */
@ -169,14 +182,16 @@ static int phonet_device_notify(struct notifier_block *me, unsigned long what,
struct net_device *dev = arg; struct net_device *dev = arg;
if (what == NETDEV_UNREGISTER) { if (what == NETDEV_UNREGISTER) {
struct phonet_device_list *pndevs;
struct phonet_device *pnd; struct phonet_device *pnd;
/* Destroy phonet-specific device data */ /* Destroy phonet-specific device data */
spin_lock_bh(&pndevs.lock); pndevs = phonet_device_list(dev_net(dev));
spin_lock_bh(&pndevs->lock);
pnd = __phonet_get(dev); pnd = __phonet_get(dev);
if (pnd) if (pnd)
__phonet_device_free(pnd); __phonet_device_free(pnd);
spin_unlock_bh(&pndevs.lock); spin_unlock_bh(&pndevs->lock);
} }
return 0; return 0;
@ -187,10 +202,41 @@ static struct notifier_block phonet_device_notifier = {
.priority = 0, .priority = 0,
}; };
/* Per-namespace Phonet devices handling */
static int phonet_init_net(struct net *net)
{
struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
if (!pnn)
return -ENOMEM;
INIT_LIST_HEAD(&pnn->pndevs.list);
spin_lock_init(&pnn->pndevs.lock);
net_assign_generic(net, phonet_net_id, pnn);
return 0;
}
static void phonet_exit_net(struct net *net)
{
struct phonet_net *pnn = net_generic(net, phonet_net_id);
struct phonet_device *pnd, *n;
list_for_each_entry_safe(pnd, n, &pnn->pndevs.list, list)
__phonet_device_free(pnd);
kfree(pnn);
}
static struct pernet_operations phonet_net_ops = {
.init = phonet_init_net,
.exit = phonet_exit_net,
};
/* Initialize Phonet devices list */ /* Initialize Phonet devices list */
int __init phonet_device_init(void) int __init phonet_device_init(void)
{ {
int err; int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
if (err)
return err;
register_netdevice_notifier(&phonet_device_notifier); register_netdevice_notifier(&phonet_device_notifier);
err = phonet_netlink_register(); err = phonet_netlink_register();
@ -201,11 +247,7 @@ int __init phonet_device_init(void)
void phonet_device_exit(void) void phonet_device_exit(void)
{ {
struct phonet_device *pnd, *n;
rtnl_unregister_all(PF_PHONET); rtnl_unregister_all(PF_PHONET);
unregister_netdevice_notifier(&phonet_device_notifier); unregister_netdevice_notifier(&phonet_device_notifier);
unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
list_for_each_entry_safe(pnd, n, &pndevs.list, list)
__phonet_device_free(pnd);
} }

Просмотреть файл

@ -123,17 +123,16 @@ nla_put_failure:
static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb) static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
{ {
struct net *net = sock_net(skb->sk); struct phonet_device_list *pndevs;
struct phonet_device *pnd; struct phonet_device *pnd;
int dev_idx = 0, dev_start_idx = cb->args[0]; int dev_idx = 0, dev_start_idx = cb->args[0];
int addr_idx = 0, addr_start_idx = cb->args[1]; int addr_idx = 0, addr_start_idx = cb->args[1];
spin_lock_bh(&pndevs.lock); pndevs = phonet_device_list(sock_net(skb->sk));
list_for_each_entry(pnd, &pndevs.list, list) { spin_lock_bh(&pndevs->lock);
list_for_each_entry(pnd, &pndevs->list, list) {
u8 addr; u8 addr;
if (!net_eq(dev_net(pnd->netdev), net))
continue;
if (dev_idx > dev_start_idx) if (dev_idx > dev_start_idx)
addr_start_idx = 0; addr_start_idx = 0;
if (dev_idx++ < dev_start_idx) if (dev_idx++ < dev_start_idx)
@ -153,7 +152,7 @@ static int getaddr_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
} }
out: out:
spin_unlock_bh(&pndevs.lock); spin_unlock_bh(&pndevs->lock);
cb->args[0] = dev_idx; cb->args[0] = dev_idx;
cb->args[1] = addr_idx; cb->args[1] = addr_idx;