📄 network.c
字号:
list_for_each(ele, &opened) { struct iss_net_private *lp; lp = list_entry(ele, struct iss_net_private, opened_list); if (!netif_running(lp->dev)) break; spin_lock(&lp->lock); while ((err = iss_net_rx(lp->dev)) > 0) ret++; spin_unlock(&lp->lock); if (err < 0) { printk(KERN_ERR "Device '%s' read returned %d, " "shutting it down\n", lp->dev->name, err); dev_close(lp->dev); } else { // FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); } } spin_unlock(&opened_lock); return ret;}static void iss_net_timer(unsigned long priv){ struct iss_net_private* lp = (struct iss_net_private*) priv; spin_lock(&lp->lock); iss_net_poll(); mod_timer(&lp->timer, jiffies + lp->timer_val); spin_unlock(&lp->lock);}static int iss_net_open(struct net_device *dev){ struct iss_net_private *lp = dev->priv; char addr[sizeof "255.255.255.255\0"]; int err; spin_lock(&lp->lock); if ((err = lp->tp.open(lp)) < 0) goto out; if (!lp->have_mac) { dev_ip_addr(dev, addr, &lp->mac[2]); set_ether_mac(dev, lp->mac); } netif_start_queue(dev); /* clear buffer - it can happen that the host side of the interface * is full when we get here. In this case, new data is never queued, * SIGIOs never arrive, and the net never works. */ while ((err = iss_net_rx(dev)) > 0) ; spin_lock(&opened_lock); list_add(&lp->opened_list, &opened); spin_unlock(&opened_lock); init_timer(&lp->timer); lp->timer_val = ISS_NET_TIMER_VALUE; lp->timer.data = (unsigned long) lp; lp->timer.function = iss_net_timer; mod_timer(&lp->timer, jiffies + lp->timer_val);out: spin_unlock(&lp->lock); return err;}static int iss_net_close(struct net_device *dev){ struct iss_net_private *lp = dev->priv;printk("iss_net_close!\n"); netif_stop_queue(dev); spin_lock(&lp->lock); spin_lock(&opened_lock); list_del(&opened); spin_unlock(&opened_lock); del_timer_sync(&lp->timer); lp->tp.close(lp); spin_unlock(&lp->lock); return 0;}static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev){ struct iss_net_private *lp = dev->priv; unsigned long flags; int len; netif_stop_queue(dev); spin_lock_irqsave(&lp->lock, flags); len = lp->tp.write(lp, &skb); if (len == skb->len) { lp->stats.tx_packets++; lp->stats.tx_bytes += skb->len; dev->trans_start = jiffies; netif_start_queue(dev); /* this is normally done in the interrupt when tx finishes */ netif_wake_queue(dev); } else if (len == 0) { netif_start_queue(dev); lp->stats.tx_dropped++; } else { netif_start_queue(dev); printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len); } spin_unlock_irqrestore(&lp->lock, flags); dev_kfree_skb(skb); return 0;}static struct net_device_stats *iss_net_get_stats(struct net_device *dev){ struct iss_net_private *lp = dev->priv; return &lp->stats;}static void iss_net_set_multicast_list(struct net_device *dev){#if 0 if (dev->flags & IFF_PROMISC) return; else if (dev->mc_count) dev->flags |= IFF_ALLMULTI; else dev->flags &= ~IFF_ALLMULTI;#endif}static void iss_net_tx_timeout(struct net_device *dev){#if 0 dev->trans_start = jiffies; netif_wake_queue(dev);#endif}static int iss_net_set_mac(struct net_device *dev, void *addr){#if 0 struct iss_net_private *lp = dev->priv; struct sockaddr *hwaddr = addr; spin_lock(&lp->lock); memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN); spin_unlock(&lp->lock);#endif return 0;}static int iss_net_change_mtu(struct net_device *dev, int new_mtu){#if 0 struct iss_net_private *lp = dev->priv; int err = 0; spin_lock(&lp->lock); // FIXME not needed new_mtu = transport_set_mtu(new_mtu, &lp->user); if (new_mtu < 0) err = new_mtu; else dev->mtu = new_mtu; spin_unlock(&lp->lock); return err;#endif return -EINVAL;}void iss_net_user_timer_expire(unsigned long _conn){}static struct platform_driver iss_net_driver = { .driver = { .name = DRIVER_NAME, },};static int driver_registered;static int iss_net_configure(int index, char *init){ struct net_device *dev; struct iss_net_private *lp; int err; if ((dev = alloc_etherdev(sizeof *lp)) == NULL) { printk(KERN_ERR "eth_configure: failed to allocate device\n"); return 1; } /* Initialize private element. */ lp = dev->priv; *lp = ((struct iss_net_private) { .device_list = LIST_HEAD_INIT(lp->device_list), .opened_list = LIST_HEAD_INIT(lp->opened_list), .lock = SPIN_LOCK_UNLOCKED, .dev = dev, .index = index, //.fd = -1, .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 }, .have_mac = 0, }); /* * Try all transport protocols. * Note: more protocols can be added by adding '&& !X_init(lp, eth)'. */ if (!tuntap_probe(lp, index, init)) { printk("Invalid arguments. Skipping device!\n"); goto errout; } printk(KERN_INFO "Netdevice %d ", index); if (lp->have_mac) printk("(%02x:%02x:%02x:%02x:%02x:%02x) ", lp->mac[0], lp->mac[1], lp->mac[2], lp->mac[3], lp->mac[4], lp->mac[5]); printk(": "); /* sysfs register */ if (!driver_registered) { platform_driver_register(&iss_net_driver); driver_registered = 1; } spin_lock(&devices_lock); list_add(&lp->device_list, &devices); spin_unlock(&devices_lock); lp->pdev.id = index; lp->pdev.name = DRIVER_NAME; platform_device_register(&lp->pdev); SET_NETDEV_DEV(dev,&lp->pdev.dev); /* * If this name ends up conflicting with an existing registered * netdevice, that is OK, register_netdev{,ice}() will notice this * and fail. */ snprintf(dev->name, sizeof dev->name, "eth%d", index); dev->mtu = lp->mtu; dev->open = iss_net_open; dev->hard_start_xmit = iss_net_start_xmit; dev->stop = iss_net_close; dev->get_stats = iss_net_get_stats; dev->set_multicast_list = iss_net_set_multicast_list; dev->tx_timeout = iss_net_tx_timeout; dev->set_mac_address = iss_net_set_mac; dev->change_mtu = iss_net_change_mtu; dev->watchdog_timeo = (HZ >> 1); dev->irq = -1; rtnl_lock(); err = register_netdevice(dev); rtnl_unlock(); if (err) { printk("Error registering net device!\n"); /* XXX: should we call ->remove() here? */ free_netdev(dev); return 1; } init_timer(&lp->tl); lp->tl.function = iss_net_user_timer_expire;#if 0 if (lp->have_mac) set_ether_mac(dev, lp->mac);#endif return 0;errout: // FIXME: unregister; free, etc.. return -EIO;}/* ------------------------------------------------------------------------- *//* Filled in during early boot */struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);struct iss_net_init { struct list_head list; char *init; /* init string */ int index;};/* * Parse the command line and look for 'ethX=...' fields, and register all * those fields. They will be later initialized in iss_net_init. */#define ERR KERN_ERR "iss_net_setup: "static int iss_net_setup(char *str){ struct iss_net_private *device = NULL; struct iss_net_init *new; struct list_head *ele; char *end; int n; n = simple_strtoul(str, &end, 0); if (end == str) { printk(ERR "Failed to parse '%s'\n", str); return 1; } if (n < 0) { printk(ERR "Device %d is negative\n", n); return 1; } if (*(str = end) != '=') { printk(ERR "Expected '=' after device number\n"); return 1; } spin_lock(&devices_lock); list_for_each(ele, &devices) { device = list_entry(ele, struct iss_net_private, device_list); if (device->index == n) break; } spin_unlock(&devices_lock); if (device && device->index == n) { printk(ERR "Device %d already configured\n", n); return 1; } if ((new = alloc_bootmem(sizeof new)) == NULL) { printk("Alloc_bootmem failed\n"); return 1; } INIT_LIST_HEAD(&new->list); new->index = n; new->init = str + 1; list_add_tail(&new->list, ð_cmd_line); return 1;}#undef ERR__setup("eth=", iss_net_setup);/* * Initialize all ISS Ethernet devices previously registered in iss_net_setup. */static int iss_net_init(void){ struct list_head *ele, *next; /* Walk through all Ethernet devices specified in the command line. */ list_for_each_safe(ele, next, ð_cmd_line) { struct iss_net_init *eth; eth = list_entry(ele, struct iss_net_init, list); iss_net_configure(eth->index, eth->init); } return 1;}module_init(iss_net_init);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -