Skip to content
Snippets Groups Projects
dev.c 154 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* Initialize per network namespace state */
    
    static int __net_init netdev_init(struct net *net)
    
    	if (net != &init_net)
    		INIT_LIST_HEAD(&net->dev_base_head);
    
    	net->dev_name_head = netdev_create_hash();
    	if (net->dev_name_head == NULL)
    		goto err_name;
    
    	net->dev_index_head = netdev_create_hash();
    	if (net->dev_index_head == NULL)
    		goto err_idx;
    
    
    err_idx:
    	kfree(net->dev_name_head);
    err_name:
    	return -ENOMEM;
    
    /**
     *	netdev_drivername - network driver for the device
     *	@dev: network device
     *
     *	Determine network driver for device.
     */
    
    const char *netdev_drivername(const struct net_device *dev)
    
    	const struct device_driver *driver;
    	const struct device *parent;
    
    	const char *empty = "";
    
    
    	parent = dev->dev.parent;
    	if (!parent)
    
    
    	driver = parent->driver;
    	if (driver && driver->name)
    
    		return driver->name;
    	return empty;
    
    static int __netdev_printk(const char *level, const struct net_device *dev,
    
    	if (dev && dev->dev.parent) {
    
    		r = dev_printk_emit(level[1] - '0',
    				    dev->dev.parent,
    				    "%s %s %s: %pV",
    				    dev_driver_string(dev->dev.parent),
    				    dev_name(dev->dev.parent),
    				    netdev_name(dev), vaf);
    
    		r = printk("%s%s: %pV", level, netdev_name(dev), vaf);
    
    		r = printk("%s(NULL net_device): %pV", level, vaf);
    
    
    	return r;
    }
    
    int netdev_printk(const char *level, const struct net_device *dev,
    		  const char *format, ...)
    {
    	struct va_format vaf;
    	va_list args;
    	int r;
    
    	va_start(args, format);
    
    	vaf.fmt = format;
    	vaf.va = &args;
    
    	r = __netdev_printk(level, dev, &vaf);
    
    	va_end(args);
    
    	return r;
    }
    EXPORT_SYMBOL(netdev_printk);
    
    #define define_netdev_printk_level(func, level)			\
    int func(const struct net_device *dev, const char *fmt, ...)	\
    {								\
    	int r;							\
    	struct va_format vaf;					\
    	va_list args;						\
    								\
    	va_start(args, fmt);					\
    								\
    	vaf.fmt = fmt;						\
    	vaf.va = &args;						\
    								\
    	r = __netdev_printk(level, dev, &vaf);			\
    
    	va_end(args);						\
    								\
    	return r;						\
    }								\
    EXPORT_SYMBOL(func);
    
    define_netdev_printk_level(netdev_emerg, KERN_EMERG);
    define_netdev_printk_level(netdev_alert, KERN_ALERT);
    define_netdev_printk_level(netdev_crit, KERN_CRIT);
    define_netdev_printk_level(netdev_err, KERN_ERR);
    define_netdev_printk_level(netdev_warn, KERN_WARNING);
    define_netdev_printk_level(netdev_notice, KERN_NOTICE);
    define_netdev_printk_level(netdev_info, KERN_INFO);
    
    
    static void __net_exit netdev_exit(struct net *net)
    
    {
    	kfree(net->dev_name_head);
    	kfree(net->dev_index_head);
    }
    
    
    static struct pernet_operations __net_initdata netdev_net_ops = {
    
    static void __net_exit default_device_exit(struct net *net)
    
    	struct net_device *dev, *aux;
    
    	 * Push all migratable network devices back to the
    
    	 * initial network namespace
    	 */
    	rtnl_lock();
    
    	for_each_netdev_safe(net, dev, aux) {
    
    
    		/* Ignore unmoveable devices (i.e. loopback) */
    		if (dev->features & NETIF_F_NETNS_LOCAL)
    			continue;
    
    
    		/* Leave virtual devices for the generic cleanup */
    		if (dev->rtnl_link_ops)
    			continue;
    
    Lucas De Marchi's avatar
    Lucas De Marchi committed
    		/* Push remaining network devices to init_net */
    
    		snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
    		err = dev_change_net_namespace(dev, &init_net, fb_name);
    
    			pr_emerg("%s: failed to move %s to init_net: %d\n",
    				 __func__, dev->name, err);
    
    static void __net_exit default_device_exit_batch(struct list_head *net_list)
    {
    	/* At exit all network devices most be removed from a network
    
    	 * namespace.  Do this in the reverse order of registration.
    
    	 * Do this across as many network namespaces as possible to
    	 * improve batching efficiency.
    	 */
    	struct net_device *dev;
    	struct net *net;
    	LIST_HEAD(dev_kill_list);
    
    	rtnl_lock();
    	list_for_each_entry(net, net_list, exit_list) {
    		for_each_netdev_reverse(net, dev) {
    			if (dev->rtnl_link_ops)
    				dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
    			else
    				unregister_netdevice_queue(dev, &dev_kill_list);
    		}
    	}
    	unregister_netdevice_many(&dev_kill_list);
    
    	list_del(&dev_kill_list);
    
    static struct pernet_operations __net_initdata default_device_ops = {
    
    	.exit_batch = default_device_exit_batch,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     *	Initialize the DEV module. At boot time this walks the device list and
     *	unhooks any devices that fail to initialise (normally hardware not
     *	present) and leaves us with a valid list of present and active devices.
     *
     */
    
    /*
     *       This is called single threaded during boot, so no need
     *       to take the rtnl semaphore.
     */
    static int __init net_dev_init(void)
    {
    	int i, rc = -ENOMEM;
    
    	BUG_ON(!dev_boot_phase);
    
    	if (dev_proc_init())
    		goto out;
    
    
    	if (netdev_kobject_init())
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out;
    
    	INIT_LIST_HEAD(&ptype_all);
    
    	for (i = 0; i < PTYPE_HASH_SIZE; i++)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		INIT_LIST_HEAD(&ptype_base[i]);
    
    
    	INIT_LIST_HEAD(&offload_base);
    
    
    	if (register_pernet_subsys(&netdev_net_ops))
    		goto out;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/*
    	 *	Initialise the packet receive queues.
    	 */
    
    
    	for_each_possible_cpu(i) {
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		struct softnet_data *sd = &per_cpu(softnet_data, i);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Changli Gao's avatar
    Changli Gao committed
    		memset(sd, 0, sizeof(*sd));
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		skb_queue_head_init(&sd->input_pkt_queue);
    
    		skb_queue_head_init(&sd->process_queue);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		sd->completion_queue = NULL;
    		INIT_LIST_HEAD(&sd->poll_list);
    
    		sd->output_queue = NULL;
    		sd->output_queue_tailp = &sd->output_queue;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    #ifdef CONFIG_RPS
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		sd->csd.func = rps_trigger_softirq;
    		sd->csd.info = sd;
    		sd->csd.flags = 0;
    		sd->cpu = i;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		sd->backlog.poll = process_backlog;
    		sd->backlog.weight = weight_p;
    		sd->backlog.gro_list = NULL;
    		sd->backlog.gro_count = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	dev_boot_phase = 0;
    
    
    	/* The loopback device is special if any other network devices
    	 * is present in a network namespace the loopback device must
    	 * be present. Since we now dynamically allocate and free the
    	 * loopback device ensure this invariant is maintained by
    	 * keeping the loopback device as the first device on the
    	 * list of network devices.  Ensuring the loopback devices
    	 * is the first device that appears and the last network device
    	 * that disappears.
    	 */
    	if (register_pernet_device(&loopback_net_ops))
    		goto out;
    
    	if (register_pernet_device(&default_device_ops))
    		goto out;
    
    
    	open_softirq(NET_TX_SOFTIRQ, net_tx_action);
    	open_softirq(NET_RX_SOFTIRQ, net_rx_action);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	hotcpu_notifier(dev_cpu_callback, 0);
    	dst_init();
    	rc = 0;
    out:
    	return rc;
    }
    
    subsys_initcall(net_dev_init);