📄 netproto.c
字号:
dbg_pinit (1, "%d %ld %ld", interface, device->stats.rx_dropped, device->stats.rx_errors); }}/* ** netproto_rx_length_error* @int - network interface* */void netproto_rx_length_error (int interface){ struct netproto_dev *device; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.rx_length_errors++; device->stats.rx_dropped++; device->stats.rx_errors++; dbg_pinit (1, "%d %ld %ld %ld", interface, device->stats.rx_length_errors, device->stats.rx_dropped, device->stats.rx_errors); }}/* ** netproto_rx_over_error* @int - network interface* */void netproto_rx_over_error (int interface){ struct netproto_dev *device; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.rx_over_errors++; device->stats.rx_dropped++; device->stats.rx_errors++; dbg_pinit (1, "%d %ld %ld %ld", interface, device->stats.rx_over_errors, device->stats.rx_dropped, device->stats.rx_errors); }}/* ** netproto_rx_crc_error* @int - network interface* */void netproto_rx_crc_error (int interface){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.rx_crc_errors++; device->stats.rx_dropped++; device->stats.rx_errors++; dbg_pinit (3, "%d %ld %ld %ld", interface, device->stats.rx_crc_errors, device->stats.rx_dropped, device->stats.rx_errors); }}/* ** netproto_rx_frame_error* @int - network interface* */void netproto_rx_frame_error (int interface){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.rx_frame_errors++; device->stats.rx_dropped++; device->stats.rx_errors++; dbg_pinit (1, "%d %ld %ld %ld", interface, device->stats.rx_frame_errors, device->stats.rx_dropped, device->stats.rx_errors); }}/* ** netproto_rx_fifo_error* @int - network interface* */void netproto_rx_fifo_error (int interface){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.rx_fifo_errors++; device->stats.rx_dropped++; device->stats.rx_errors++; dbg_pinit (1, "%d %ld %ld %ld", interface, device->stats.rx_fifo_errors, device->stats.rx_dropped, device->stats.rx_errors); }}/* ** netproto_rx_missed_error* @int - network interface* */void netproto_rx_missed_error (int interface){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.rx_missed_errors++; device->stats.rx_dropped++; device->stats.rx_errors++; dbg_pinit (1, "%d %ld %ld %ld", interface, device->stats.rx_missed_errors, device->stats.rx_dropped, device->stats.rx_errors); }}/* ** netproto_rx_packets* @int - network interface* */void netproto_rx_packets (int interface, int len){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.rx_packets++; device->stats.rx_bytes += len; }}/* ** netproto_tx_dropped* @int - network interface* */void netproto_tx_dropped (int interface){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.tx_dropped++; }}/* ** netproto_tx_carrier_error* @int - network interface* */void netproto_tx_carrier_errors (int interface){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.tx_errors++; device->stats.tx_carrier_errors++; }}/* ** netproto_tx_fifo_error* @int - network interface* */void netproto_tx_fifo_errors (int interface){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.tx_errors++; device->stats.tx_fifo_errors++; }}/* ** netproto_tx_packets* @int - network interface* */void netproto_tx_packets (int interface, int len){ struct netproto_dev *device; //return; if ((interface < netproto_devices) && (device = netproto_device_array[interface])) { device->stats.tx_packets++; device->stats.tx_bytes += len; }}/* ** netproto_name* @int - network interface* */char *netproto_name (int interface){ struct netproto_dev *device; if ((interface < netproto_devices) && (device = netproto_device_array[interface]) && (device->net_dev)) { return device->net_dev->name; } return NULL;}/* ** netproto_mtu* @int - network interface* */int netproto_mtu (int interface){ struct netproto_dev *device; if (0 <= interface && (interface < netproto_devices) && (device = netproto_device_array[interface]) && (device->net_dev)) { return device->net_dev->mtu; } return 0;}/* ** netproto_on* @int - network interface* */void netproto_on (int interface){ struct netproto_dev *device; dbg_pinit (1, "%d", interface); if ((interface < netproto_devices) && (device = netproto_device_array[interface]) && (device->net_dev)) { dbg_pinit (1, "calling netif_carrier_on" ); netif_carrier_on (device->net_dev); netif_wake_queue (device->net_dev); }}/* ** netproto_off* @int - network interface* */void netproto_off (int interface){ struct netproto_dev *device; dbg_pinit (1, "%d", interface); if ((interface < netproto_devices) && (device = netproto_device_array[interface]) && (device->net_dev)) { netif_stop_queue (device->net_dev); netif_carrier_off (device->net_dev); }}/*** netproto_modinit - initialize netproto library* @name: name for proc file system* @num: number of interfaces to allow** Initialize the netproto library setting the maximum number of network* interfaces that may be created. */int netproto_modinit (char *name, int maximum_interfaces){ struct proc_dir_entry *p; struct netproto_dev **nda; // pointer to array of pointers to netproto dev structures dbg_pinit (1, "%s maximum_interfaces: %d", name, maximum_interfaces); if (!name || !strlen (name)) { dbg_pinit (0, "name null or zero length"); return -EINVAL; } // verify that netproto_devices not previously set, then allocate array and set it if ((nda = kmalloc (sizeof (struct netproto_dev *) * maximum_interfaces, GFP_ATOMIC)) == NULL) { dbg_pinit (0, "kmalloc failed"); return -EINVAL; } memset (nda, 0, sizeof (struct netproto_dev *) * maximum_interfaces); { unsigned long flags; write_lock_irqsave (&netproto_rwlock, flags); if (netproto_devices) { write_unlock_irqrestore (&netproto_rwlock, flags); kfree (nda); dbg_pinit (0, "netproto_devices already set"); return -EINVAL; } netproto_device_array = nda; netproto_devices = maximum_interfaces; write_unlock_irqrestore (&netproto_rwlock, flags); } return 0; // create proc filesystem entry if ((p = create_proc_entry (name, 0, NULL)) == NULL) return -ENOMEM; p->proc_fops = &netproto_proc_operations; return 0;}/*** netproto_create - create network interface* @name: name of interface (use " " to get default "ethN")* @xmit_skb: call back to transmit skb* @set_addr: callback to set mac address* @addr: - pointer to mac address buffer* @addrlen: - length of address buffer* @mtu: length of mac address buffer* @max_queue_entries: max number of outstanding skbs allow* @max_queue_bytes: max number of outstanding bytes allow* @flags:** Create a network interface, providing an xmit skb function and an* optional mac address.** A returned value greater or equal to zero indicates success and can be* used with subsequent function calls to indicate the created network* interface. */int netproto_create (char *name, int (*xmit_skb) (int, struct sk_buff *), int (*set_addr) (int, void *, int), int (*tx_timeout) (int), void *addr, int addrlen, int mtu, int max_queue_entries, int max_queue_bytes, int flags){ struct netproto_dev *device; struct net_device *net_dev; int i; unsigned char *cp = addr; dbg_pinit (1, "name: %s", name); dbg_pinit (1, "addr: %x:%x:%x:%x:%x:%x len: %d", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5], addrlen); if (!name || !strlen (name) || !xmit_skb) { dbg_pinit (0, "invalid args"); return -EINVAL; } if ((net_dev = kmalloc (sizeof (struct net_device), GFP_ATOMIC)) == NULL) { dbg_pinit (0, "name: %s kmalloc failed", name); return -ENOMEM; } if ((device = kmalloc (sizeof (struct netproto_dev), GFP_ATOMIC)) == NULL) { dbg_pinit (0, "name: %s kmalloc failed", name); kfree (net_dev); return -ENOMEM; } dbg_pinit (1, "kmalloc device: %p net_dev: %p", device, net_dev); memset (net_dev, 0, sizeof (struct net_device)); memset (device, 0, sizeof (struct netproto_dev)); device->net_dev = net_dev; device->ops_in_progress = 0; device->cmd_queue_count = 0; device->state = 0; device->xmit_skb = xmit_skb; device->set_addr = set_addr; device->tx_timeout = tx_timeout; // trim name to less then net_dev.name if ((i = strlen (name)) >= sizeof (net_dev->name)) { i = sizeof (net_dev->name) - 2; } memcpy (net_dev->name, name, i); // XXX ether_setup (net_dev); if (addrlen && addrlen <= ETH_ALEN) { memcpy (&(net_dev->dev_addr), addr, addrlen); device->mac_was_set = 1; } // set net_dev net_dev->priv = device; net_dev->do_ioctl = netproto_do_ioctl; net_dev->set_config = netproto_set_config; net_dev->set_mac_address = netproto_set_mac_addr; net_dev->hard_start_xmit = netproto_start_xmit; net_dev->get_stats = netproto_get_stats; //net_dev->set_multicast = netproto_set_multicast; net_dev->change_mtu = netproto_change_mtu; net_dev->init = netproto_init; net_dev->uninit = netproto_uninit; net_dev->open = netproto_open; net_dev->stop = netproto_stop; net_dev->tx_timeout = netproto_tx_timeout; device->mtu = mtu ? mtu : net_dev->mtu; device->max_queue_entries = max_queue_entries ? max_queue_entries : 1; device->max_queue_bytes = max_queue_bytes ? max_queue_bytes : 2000; // get global lock, // derive device number by seeing how many devices we already have { unsigned long flags; write_lock_irqsave (&netproto_rwlock, flags); for (i = 0; i < netproto_devices; i++) { if (netproto_device_array[i] == NULL) { break; } } if (i == netproto_devices) { dbg_pinit (0, "name: %s cannot find netproto_devices", name); write_unlock_irqrestore (&netproto_rwlock, flags); dbg_pinit (2, "kfree device: %p", device); kfree (net_dev); ///// QQQ kfree (device); ///// QQQ return -ENOMEM; } device->interface = i; netproto_device_array[i] = device; write_unlock_irqrestore (&netproto_rwlock, flags); } if (!register_netdev (net_dev)) { device->state |= NP_REGISTERED | NP_ENABLED; dbg_pinit (1, "REGISTERED DEVICE->STATE: #%x", device->state); } else { dbg_pinit (0, "REGISTER_NETDEV FAILED"); } // set state to inactive and request to desired value //netif_device_detach(net_dev); //netif_carrier_off(net_dev); //netif_stop_queue(net_dev); return i;}/*** netproto_control - control network interface* @interface: network interface* @operation: operation** Control a network interface, */int netproto_control (int interface, int operation){ dbg_pmgmt (1, "interface: %d operation:%d", interface, operation); return 0;}/*** netproto_destroy - destroy a network interface* @interface: network interface** Call to tear down a previously created network interface */int netproto_destroy (int interface){ struct netproto_dev *device; struct net_device *net_dev; dbg_pinit (1, "interface: %d", interface); { unsigned long flags; write_lock_irqsave (&netproto_rwlock, flags); if ((interface >= netproto_devices) || !(device = netproto_device_array[interface])) { write_lock_irqsave (&netproto_rwlock, flags); dbg_pinit (0, "interface: %d netproto_device_array bad", interface); return 0; } netproto_device_array[interface] = NULL; write_lock_irqsave (&netproto_rwlock, flags); } net_dev = device->net_dev; device->net_dev = NULL; dbg_pinit (1, "state: %x", device->state); device->state &= ~NP_ENABLED; if ((device->state & NP_REGISTERED) && net_dev) { device->state &= ~NP_REGISTERED; dbg_pinit (1, "unregistering: %p", net_dev); unregister_netdev (net_dev); kfree (net_dev); } kfree (device); return 0;}/*** netproto_modexit** Call to unload the library. */int netproto_modexit (void){ int devices; struct netproto_dev **device_array; dbg_pinit (1, ""); if (netproto_devices) { return 0; } // remove proc filesystem entry //remove_proc_entry(netproto_procname, NULL); netproto_procname = NULL; // XXX need to lock here devices = netproto_devices; netproto_devices = 0; //for (i = 0; i < devices; i++) { // netproto_destroy(i); //} { unsigned long flags; write_lock_irqsave (&netproto_rwlock, flags); device_array = netproto_device_array; netproto_device_array = NULL; write_lock_irqsave (&netproto_rwlock, flags); } kfree (netproto_device_array); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -