Commit edab0449 authored by Stephen Hemminger's avatar Stephen Hemminger

[NET]: Make lapbether work on 2.6.0-test3.

- unneeded include (no proc here)
- redundant fields in local device structure
+ convert to dynamic network device allocation
- refcounts on local data are redundant, it is really part of network_device
- excessive __inline__
+ correctly manage references to underlying network device
+ cascade unregister
+ use RCU and RTNL to avoid deadlock
+ account for bytes as well as packets
parent 3201188e
...@@ -37,7 +37,6 @@ ...@@ -37,7 +37,6 @@
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/notifier.h> #include <linux/notifier.h>
#include <linux/proc_fs.h>
#include <linux/stat.h> #include <linux/stat.h>
#include <linux/netfilter.h> #include <linux/netfilter.h>
#include <linux/module.h> #include <linux/module.h>
...@@ -52,50 +51,27 @@ static char bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; ...@@ -52,50 +51,27 @@ static char bcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
struct lapbethdev { struct lapbethdev {
struct list_head node; struct list_head node;
char ethname[14]; /* ether device name */
struct net_device *ethdev; /* link to ethernet device */ struct net_device *ethdev; /* link to ethernet device */
struct net_device axdev; /* lapbeth device (lapb#) */ struct net_device *axdev; /* lapbeth device (lapb#) */
struct net_device_stats stats; /* some statistics */ struct net_device_stats stats; /* some statistics */
atomic_t refcnt;
}; };
static struct list_head lapbeth_devices = LIST_HEAD_INIT(lapbeth_devices); static struct list_head lapbeth_devices = LIST_HEAD_INIT(lapbeth_devices);
static rwlock_t lapbeth_devices_lock = RW_LOCK_UNLOCKED;
static __inline__ void lapbeth_hold(struct lapbethdev *lapbeth)
{
atomic_inc(&lapbeth->refcnt);
}
static __inline__ void lapbeth_put(struct lapbethdev *lapbeth)
{
if (atomic_dec_and_test(&lapbeth->refcnt))
kfree(lapbeth);
}
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* /*
* Get the LAPB device for the ethernet device * Get the LAPB device for the ethernet device
*/ */
static __inline__ struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev) static struct lapbethdev *lapbeth_get_x25_dev(struct net_device *dev)
{ {
struct list_head *entry; struct lapbethdev *lapbeth;
struct lapbethdev *lapbeth, *use = NULL;
read_lock(&lapbeth_devices_lock);
list_for_each(entry, &lapbeth_devices) { list_for_each_entry_rcu(lapbeth, &lapbeth_devices, node) {
lapbeth = list_entry(entry, struct lapbethdev, node); if (lapbeth->ethdev == dev)
if (lapbeth->ethdev == dev) { return lapbeth;
use = lapbeth;
break;
}
} }
if (use) return NULL;
lapbeth_hold(use);
read_unlock(&lapbeth_devices_lock);
return use;
} }
static __inline__ int dev_is_ethdev(struct net_device *dev) static __inline__ int dev_is_ethdev(struct net_device *dev)
...@@ -103,36 +79,6 @@ static __inline__ int dev_is_ethdev(struct net_device *dev) ...@@ -103,36 +79,6 @@ static __inline__ int dev_is_ethdev(struct net_device *dev)
return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5); return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
} }
/*
* Sanity check: remove all devices that ceased to exists and
* return '1' if the given LAPB device was affected.
*/
static int lapbeth_check_devices(struct net_device *dev)
{
struct lapbethdev *lapbeth;
struct list_head *entry, *tmp;
int result = 0;
write_lock(&lapbeth_devices_lock);
list_for_each_safe(entry, tmp, &lapbeth_devices) {
lapbeth = list_entry(entry, struct lapbethdev, node);
if (!dev_get(lapbeth->ethname)) {
if (&lapbeth->axdev == dev)
result = 1;
unregister_netdev(&lapbeth->axdev);
dev_put(lapbeth->ethdev);
list_del(&lapbeth->node);
lapbeth_put(lapbeth);
}
}
write_unlock(&lapbeth_devices_lock);
return result;
}
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
/* /*
...@@ -145,29 +91,28 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe ...@@ -145,29 +91,28 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
skb->sk = NULL; /* Initially we don't know who it's for */ skb->sk = NULL; /* Initially we don't know who it's for */
rcu_read_lock();
lapbeth = lapbeth_get_x25_dev(dev); lapbeth = lapbeth_get_x25_dev(dev);
if (!lapbeth) if (!lapbeth)
goto drop; goto drop;
if (!netif_running(&lapbeth->axdev)) if (!netif_running(lapbeth->axdev))
goto put_drop; goto drop;
lapbeth->stats.rx_packets++; lapbeth->stats.rx_packets++;
len = skb->data[0] + skb->data[1] * 256; len = skb->data[0] + skb->data[1] * 256;
lapbeth->stats.rx_bytes += len;
skb_pull(skb, 2); /* Remove the length bytes */ skb_pull(skb, 2); /* Remove the length bytes */
skb_trim(skb, len); /* Set the length of the data */ skb_trim(skb, len); /* Set the length of the data */
if ((err = lapb_data_received(lapbeth, skb)) != LAPB_OK) { if ((err = lapb_data_received(lapbeth, skb)) != LAPB_OK) {
printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err); printk(KERN_DEBUG "lapbether: lapb_data_received err - %d\n", err);
goto put_drop; goto drop;
} }
lapbeth_put(lapbeth);
out: out:
rcu_read_unlock();
return 0; return 0;
put_drop:
lapbeth_put(lapbeth);
drop: drop:
kfree_skb(skb); kfree_skb(skb);
goto out; goto out;
...@@ -181,7 +126,7 @@ static int lapbeth_data_indication(void *token, struct sk_buff *skb) ...@@ -181,7 +126,7 @@ static int lapbeth_data_indication(void *token, struct sk_buff *skb)
ptr = skb_push(skb, 1); ptr = skb_push(skb, 1);
*ptr = 0x00; *ptr = 0x00;
skb->dev = &lapbeth->axdev; skb->dev = lapbeth->axdev;
skb->protocol = htons(ETH_P_X25); skb->protocol = htons(ETH_P_X25);
skb->mac.raw = skb->data; skb->mac.raw = skb->data;
skb->pkt_type = PACKET_HOST; skb->pkt_type = PACKET_HOST;
...@@ -203,7 +148,6 @@ static int lapbeth_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -203,7 +148,6 @@ static int lapbeth_xmit(struct sk_buff *skb, struct net_device *dev)
* is down, the ethernet device may have gone. * is down, the ethernet device may have gone.
*/ */
if (!netif_running(dev)) { if (!netif_running(dev)) {
lapbeth_check_devices(dev);
goto drop; goto drop;
} }
...@@ -257,6 +201,7 @@ static void lapbeth_data_transmit(void *token, struct sk_buff *skb) ...@@ -257,6 +201,7 @@ static void lapbeth_data_transmit(void *token, struct sk_buff *skb)
*ptr++ = size / 256; *ptr++ = size / 256;
lapbeth->stats.tx_packets++; lapbeth->stats.tx_packets++;
lapbeth->stats.tx_bytes += size;
skb->dev = dev = lapbeth->ethdev; skb->dev = dev = lapbeth->ethdev;
...@@ -279,7 +224,7 @@ static void lapbeth_connected(void *token, int reason) ...@@ -279,7 +224,7 @@ static void lapbeth_connected(void *token, int reason)
ptr = skb_put(skb, 1); ptr = skb_put(skb, 1);
*ptr = 0x01; *ptr = 0x01;
skb->dev = &lapbeth->axdev; skb->dev = lapbeth->axdev;
skb->protocol = htons(ETH_P_X25); skb->protocol = htons(ETH_P_X25);
skb->mac.raw = skb->data; skb->mac.raw = skb->data;
skb->pkt_type = PACKET_HOST; skb->pkt_type = PACKET_HOST;
...@@ -302,7 +247,7 @@ static void lapbeth_disconnected(void *token, int reason) ...@@ -302,7 +247,7 @@ static void lapbeth_disconnected(void *token, int reason)
ptr = skb_put(skb, 1); ptr = skb_put(skb, 1);
*ptr = 0x02; *ptr = 0x02;
skb->dev = &lapbeth->axdev; skb->dev = lapbeth->axdev;
skb->protocol = htons(ETH_P_X25); skb->protocol = htons(ETH_P_X25);
skb->mac.raw = skb->data; skb->mac.raw = skb->data;
skb->pkt_type = PACKET_HOST; skb->pkt_type = PACKET_HOST;
...@@ -330,27 +275,26 @@ static int lapbeth_set_mac_address(struct net_device *dev, void *addr) ...@@ -330,27 +275,26 @@ static int lapbeth_set_mac_address(struct net_device *dev, void *addr)
return 0; return 0;
} }
static struct lapb_register_struct lapbeth_callbacks = {
.connect_confirmation = lapbeth_connected,
.connect_indication = lapbeth_connected,
.disconnect_confirmation = lapbeth_disconnected,
.disconnect_indication = lapbeth_disconnected,
.data_indication = lapbeth_data_indication,
.data_transmit = lapbeth_data_transmit,
};
/* /*
* open/close a device * open/close a device
*/ */
static int lapbeth_open(struct net_device *dev) static int lapbeth_open(struct net_device *dev)
{ {
struct lapb_register_struct lapbeth_callbacks;
struct lapbethdev *lapbeth; struct lapbethdev *lapbeth;
int err; int err;
if (lapbeth_check_devices(dev))
return -ENODEV; /* oops, it's gone */
lapbeth = (struct lapbethdev *)dev->priv; lapbeth = (struct lapbethdev *)dev->priv;
lapbeth_callbacks.connect_confirmation = lapbeth_connected;
lapbeth_callbacks.connect_indication = lapbeth_connected;
lapbeth_callbacks.disconnect_confirmation = lapbeth_disconnected;
lapbeth_callbacks.disconnect_indication = lapbeth_disconnected;
lapbeth_callbacks.data_indication = lapbeth_data_indication;
lapbeth_callbacks.data_transmit = lapbeth_data_transmit;
if ((err = lapb_register(lapbeth, &lapbeth_callbacks)) != LAPB_OK) { if ((err = lapb_register(lapbeth, &lapbeth_callbacks)) != LAPB_OK) {
printk(KERN_ERR "lapbeth: lapb_register error - %d\n", err); printk(KERN_ERR "lapbeth: lapb_register error - %d\n", err);
return -ENODEV; return -ENODEV;
...@@ -375,65 +319,52 @@ static int lapbeth_close(struct net_device *dev) ...@@ -375,65 +319,52 @@ static int lapbeth_close(struct net_device *dev)
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
static void lapbeth_setup(struct net_device *dev)
{
dev->hard_start_xmit = lapbeth_xmit;
dev->open = lapbeth_open;
dev->stop = lapbeth_close;
dev->destructor = (void (*)(struct net_device *))kfree;
dev->set_mac_address = lapbeth_set_mac_address;
dev->get_stats = lapbeth_get_stats;
dev->type = ARPHRD_X25;
dev->hard_header_len = 3;
dev->mtu = 1000;
dev->addr_len = 0;
SET_MODULE_OWNER(dev);
}
/* /*
* Setup a new device. * Setup a new device.
*/ */
static int lapbeth_new_device(struct net_device *dev) static int lapbeth_new_device(struct net_device *dev)
{ {
unsigned char buf[14]; struct net_device *ndev;
struct lapbethdev *lapbeth; struct lapbethdev *lapbeth;
int k, rc = -ENOMEM; int rc = -ENOMEM;
if ((lapbeth = kmalloc(sizeof(struct lapbethdev), GFP_ATOMIC)) == NULL) ASSERT_RTNL();
ndev = alloc_netdev(sizeof(*lapbeth), "lapb%d",
lapbeth_setup);
if (!ndev)
goto out; goto out;
memset(lapbeth, 0, sizeof(struct lapbethdev)); lapbeth = ndev->priv;
lapbeth->axdev = ndev;
dev_hold(dev); dev_hold(dev);
lapbeth->ethdev = dev; lapbeth->ethdev = dev;
strncpy(lapbeth->ethname, dev->name, sizeof(lapbeth->ethname) - 1); rc = dev_alloc_name(ndev, ndev->name);
lapbeth->ethname[sizeof(lapbeth->ethname) - 1] = '\0'; if (rc < 0)
atomic_set(&lapbeth->refcnt, 1);
dev = &lapbeth->axdev;
SET_MODULE_OWNER(dev);
for (k = 0; k < MAXLAPBDEV; k++) {
struct net_device *odev;
sprintf(buf, "lapb%d", k);
if ((odev = __dev_get_by_name(buf)) == NULL ||
lapbeth_check_devices(odev))
break;
}
rc = -ENODEV;
if (k == MAXLAPBDEV)
goto fail; goto fail;
dev->priv = (void *)lapbeth; /* pointer back */
strcpy(dev->name, buf);
rc = -EIO; rc = -EIO;
if (register_netdev(dev)) if (register_netdevice(ndev))
goto fail; goto fail;
dev->hard_start_xmit = lapbeth_xmit; list_add_rcu(&lapbeth->node, &lapbeth_devices);
dev->open = lapbeth_open;
dev->stop = lapbeth_close;
dev->set_mac_address = lapbeth_set_mac_address;
dev->get_stats = lapbeth_get_stats;
dev->type = ARPHRD_X25;
dev->hard_header_len = 3;
dev->mtu = 1000;
dev->addr_len = 0;
write_lock(&lapbeth_devices_lock);
list_add(&lapbeth->node, &lapbeth_devices);
lapbeth_hold(lapbeth);
write_unlock(&lapbeth_devices_lock);
rc = 0; rc = 0;
out: out:
return rc; return rc;
...@@ -443,6 +374,16 @@ static int lapbeth_new_device(struct net_device *dev) ...@@ -443,6 +374,16 @@ static int lapbeth_new_device(struct net_device *dev)
goto out; goto out;
} }
/*
* Free a lapb network device.
*/
static void lapbeth_free_device(struct lapbethdev *lapbeth)
{
dev_put(lapbeth->ethdev);
list_del_rcu(&lapbeth->node);
unregister_netdevice(lapbeth->axdev);
}
/* /*
* Handle device status changes. * Handle device status changes.
*/ */
...@@ -455,30 +396,27 @@ static int lapbeth_device_event(struct notifier_block *this, ...@@ -455,30 +396,27 @@ static int lapbeth_device_event(struct notifier_block *this,
if (!dev_is_ethdev(dev)) if (!dev_is_ethdev(dev))
return NOTIFY_DONE; return NOTIFY_DONE;
lapbeth_check_devices(NULL); rcu_read_lock();
switch (event) { switch (event) {
case NETDEV_UP: case NETDEV_UP:
/* /* New ethernet device -> new LAPB interface */
* New ethernet device -> new LAPB interface if (lapbeth_get_x25_dev(dev) == NULL)
*/ lapbeth_new_device(dev);
break;
case NETDEV_DOWN:
/* ethernet device closed -> close LAPB interface */
lapbeth = lapbeth_get_x25_dev(dev); lapbeth = lapbeth_get_x25_dev(dev);
if (lapbeth) if (lapbeth)
lapbeth_put(lapbeth); dev_close(lapbeth->axdev);
else
lapbeth_new_device(dev);
break; break;
case NETDEV_GOING_DOWN: case NETDEV_UNREGISTER:
case NETDEV_DOWN: /* ethernet device closed -> close LAPB interface */ /* ethernet device disappears -> remove LAPB interface */
lapbeth = lapbeth_get_x25_dev(dev); lapbeth = lapbeth_get_x25_dev(dev);
if (lapbeth)
if (lapbeth) { lapbeth_free_device(lapbeth);
dev_close(lapbeth->ethdev);
lapbeth_put(lapbeth);
}
break; break;
} }
rcu_read_unlock();
return NOTIFY_DONE; return NOTIFY_DONE;
} }
...@@ -506,15 +444,13 @@ static int __init lapbeth_init_driver(void) ...@@ -506,15 +444,13 @@ static int __init lapbeth_init_driver(void)
printk(banner); printk(banner);
read_lock_bh(&dev_base_lock); rtnl_lock();
for (dev = dev_base; dev; dev = dev->next) { for (dev = dev_base; dev; dev = dev->next) {
if (dev_is_ethdev(dev)) { if (dev_is_ethdev(dev)) {
read_unlock_bh(&dev_base_lock);
lapbeth_new_device(dev); lapbeth_new_device(dev);
read_lock_bh(&dev_base_lock);
} }
} }
read_unlock_bh(&dev_base_lock); rtnl_unlock();
return 0; return 0;
} }
...@@ -528,16 +464,13 @@ static void __exit lapbeth_cleanup_driver(void) ...@@ -528,16 +464,13 @@ static void __exit lapbeth_cleanup_driver(void)
dev_remove_pack(&lapbeth_packet_type); dev_remove_pack(&lapbeth_packet_type);
unregister_netdevice_notifier(&lapbeth_dev_notifier); unregister_netdevice_notifier(&lapbeth_dev_notifier);
write_lock(&lapbeth_devices_lock); rtnl_lock();
list_for_each_safe(entry, tmp, &lapbeth_devices) { list_for_each_safe(entry, tmp, &lapbeth_devices) {
lapbeth = list_entry(entry, struct lapbethdev, node); lapbeth = list_entry(entry, struct lapbethdev, node);
unregister_netdev(&lapbeth->axdev);
list_del(&lapbeth->node);
lapbeth_put(lapbeth);
}
write_unlock(&lapbeth_devices_lock); unregister_netdevice(lapbeth->axdev);
}
rtnl_unlock();
} }
module_exit(lapbeth_cleanup_driver); module_exit(lapbeth_cleanup_driver);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment