Skip to content
Snippets Groups Projects
dev.c 166 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    	int err;
    
    
    	if (!ops->ndo_set_mac_address)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -EOPNOTSUPP;
    	if (sa->sa_family != dev->type)
    		return -EINVAL;
    	if (!netif_device_present(dev))
    		return -ENODEV;
    
    	err = ops->ndo_set_mac_address(dev, sa);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!err)
    
    		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
    
    	add_device_randomness(dev->dev_addr, dev->addr_len);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    EXPORT_SYMBOL(dev_set_mac_address);
    
     *	Perform the SIOCxIFxxx calls, inside rcu_read_lock()
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	int err;
    
    	struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (!dev)
    		return -ENODEV;
    
    	switch (cmd) {
    
    	case SIOCGIFFLAGS:	/* Get interface flags */
    		ifr->ifr_flags = (short) dev_get_flags(dev);
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCGIFMETRIC:	/* Get the metric on the interface
    				   (currently unused) */
    		ifr->ifr_metric = 0;
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCGIFMTU:	/* Get the MTU of a device */
    		ifr->ifr_mtu = dev->mtu;
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCGIFHWADDR:
    		if (!dev->addr_len)
    			memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
    		else
    			memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
    			       min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
    		ifr->ifr_hwaddr.sa_family = dev->type;
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCGIFSLAVE:
    		err = -EINVAL;
    		break;
    
    	case SIOCGIFMAP:
    		ifr->ifr_map.mem_start = dev->mem_start;
    		ifr->ifr_map.mem_end   = dev->mem_end;
    		ifr->ifr_map.base_addr = dev->base_addr;
    		ifr->ifr_map.irq       = dev->irq;
    		ifr->ifr_map.dma       = dev->dma;
    		ifr->ifr_map.port      = dev->if_port;
    		return 0;
    
    	case SIOCGIFINDEX:
    		ifr->ifr_ifindex = dev->ifindex;
    		return 0;
    
    	case SIOCGIFTXQLEN:
    		ifr->ifr_qlen = dev->tx_queue_len;
    		return 0;
    
    	default:
    		/* dev_ioctl() should ensure this case
    		 * is never reached
    		 */
    		WARN_ON(1);
    
    		break;
    
    
    	}
    	return err;
    }
    
    /*
     *	Perform the SIOCxIFxxx calls, inside rtnl_lock()
     */
    static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd)
    {
    	int err;
    	struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
    
    	const struct net_device_ops *ops;
    
    	ops = dev->netdev_ops;
    
    
    	case SIOCSIFFLAGS:	/* Set interface flags */
    		return dev_change_flags(dev, ifr->ifr_flags);
    
    	case SIOCSIFMETRIC:	/* Set the metric on the interface
    				   (currently unused) */
    		return -EOPNOTSUPP;
    
    	case SIOCSIFMTU:	/* Set the MTU of a device */
    		return dev_set_mtu(dev, ifr->ifr_mtu);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCSIFHWADDR:
    		return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCSIFHWBROADCAST:
    		if (ifr->ifr_hwaddr.sa_family != dev->type)
    			return -EINVAL;
    		memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
    		       min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
    		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCSIFMAP:
    		if (ops->ndo_set_config) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (!netif_device_present(dev))
    				return -ENODEV;
    
    			return ops->ndo_set_config(dev, &ifr->ifr_map);
    		}
    		return -EOPNOTSUPP;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCADDMULTI:
    
    		if (!ops->ndo_set_rx_mode ||
    
    		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
    			return -EINVAL;
    		if (!netif_device_present(dev))
    			return -ENODEV;
    
    		return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data);
    
    
    	case SIOCDELMULTI:
    
    		if (!ops->ndo_set_rx_mode ||
    
    		    ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
    			return -EINVAL;
    		if (!netif_device_present(dev))
    			return -ENODEV;
    
    		return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCSIFTXQLEN:
    		if (ifr->ifr_qlen < 0)
    			return -EINVAL;
    		dev->tx_queue_len = ifr->ifr_qlen;
    		return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCSIFNAME:
    		ifr->ifr_newname[IFNAMSIZ-1] = '\0';
    		return dev_change_name(dev, ifr->ifr_newname);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCSHWTSTAMP:
    		err = net_hwtstamp_validate(ifr);
    		if (err)
    			return err;
    		/* fall through */
    
    
    	/*
    	 *	Unknown or private ioctl
    	 */
    	default:
    		if ((cmd >= SIOCDEVPRIVATE &&
    		    cmd <= SIOCDEVPRIVATE + 15) ||
    		    cmd == SIOCBONDENSLAVE ||
    		    cmd == SIOCBONDRELEASE ||
    		    cmd == SIOCBONDSETHWADDR ||
    		    cmd == SIOCBONDSLAVEINFOQUERY ||
    		    cmd == SIOCBONDINFOQUERY ||
    		    cmd == SIOCBONDCHANGEACTIVE ||
    		    cmd == SIOCGMIIPHY ||
    		    cmd == SIOCGMIIREG ||
    		    cmd == SIOCSMIIREG ||
    		    cmd == SIOCBRADDIF ||
    		    cmd == SIOCBRDELIF ||
    		    cmd == SIOCSHWTSTAMP ||
    		    cmd == SIOCWANDEV) {
    			err = -EOPNOTSUPP;
    			if (ops->ndo_do_ioctl) {
    				if (netif_device_present(dev))
    					err = ops->ndo_do_ioctl(dev, ifr, cmd);
    				else
    					err = -ENODEV;
    			}
    		} else
    			err = -EINVAL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	}
    	return err;
    }
    
    /*
     *	This function handles all "interface"-type I/O control requests. The actual
     *	'doing' part of this is dev_ifsioc above.
     */
    
    /**
     *	dev_ioctl	-	network device ioctl
    
     *	@net: the applicable net namespace
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *	@cmd: command to issue
     *	@arg: pointer to a struct ifreq in user space
     *
     *	Issue ioctl functions to devices. This is normally called by the
     *	user space syscall interfaces but can sometimes be useful for
     *	other purposes. The return value is the return from the syscall if
     *	positive or a negative errno code on error.
     */
    
    
    int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct ifreq ifr;
    	int ret;
    	char *colon;
    
    	/* One special case: SIOCGIFCONF takes ifconf argument
    	   and requires shared lock, because it sleeps writing
    	   to user space.
    	 */
    
    	if (cmd == SIOCGIFCONF) {
    
    		rtnl_lock();
    
    		ret = dev_ifconf(net, (char __user *) arg);
    
    		rtnl_unlock();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return ret;
    	}
    	if (cmd == SIOCGIFNAME)
    
    		return dev_ifname(net, (struct ifreq __user *)arg);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
    		return -EFAULT;
    
    	ifr.ifr_name[IFNAMSIZ-1] = 0;
    
    	colon = strchr(ifr.ifr_name, ':');
    	if (colon)
    		*colon = 0;
    
    	/*
    	 *	See which interface the caller is talking about.
    	 */
    
    	switch (cmd) {
    
    	/*
    	 *	These ioctl calls:
    	 *	- can be done by all.
    	 *	- atomic and do not require locking.
    	 *	- return a value
    	 */
    	case SIOCGIFFLAGS:
    	case SIOCGIFMETRIC:
    	case SIOCGIFMTU:
    	case SIOCGIFHWADDR:
    	case SIOCGIFSLAVE:
    	case SIOCGIFMAP:
    	case SIOCGIFINDEX:
    	case SIOCGIFTXQLEN:
    		dev_load(net, ifr.ifr_name);
    
    		rcu_read_lock();
    
    		ret = dev_ifsioc_locked(net, &ifr, cmd);
    
    		rcu_read_unlock();
    
    		if (!ret) {
    			if (colon)
    				*colon = ':';
    			if (copy_to_user(arg, &ifr,
    					 sizeof(struct ifreq)))
    				ret = -EFAULT;
    		}
    		return ret;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	case SIOCETHTOOL:
    		dev_load(net, ifr.ifr_name);
    		rtnl_lock();
    		ret = dev_ethtool(net, &ifr);
    		rtnl_unlock();
    		if (!ret) {
    			if (colon)
    				*colon = ':';
    			if (copy_to_user(arg, &ifr,
    					 sizeof(struct ifreq)))
    				ret = -EFAULT;
    		}
    		return ret;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/*
    	 *	These ioctl calls:
    	 *	- require superuser power.
    	 *	- require strict serialization.
    	 *	- return a value
    	 */
    	case SIOCGMIIPHY:
    	case SIOCGMIIREG:
    	case SIOCSIFNAME:
    
    		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
    
    			return -EPERM;
    		dev_load(net, ifr.ifr_name);
    		rtnl_lock();
    		ret = dev_ifsioc(net, &ifr, cmd);
    		rtnl_unlock();
    		if (!ret) {
    			if (colon)
    				*colon = ':';
    			if (copy_to_user(arg, &ifr,
    					 sizeof(struct ifreq)))
    				ret = -EFAULT;
    		}
    		return ret;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/*
    	 *	These ioctl calls:
    	 *	- require superuser power.
    	 *	- require strict serialization.
    	 *	- do not return a value
    	 */
    
    	case SIOCSIFMAP:
    	case SIOCSIFTXQLEN:
    		if (!capable(CAP_NET_ADMIN))
    			return -EPERM;
    		/* fall through */
    	/*
    	 *	These ioctl calls:
    	 *	- require local superuser power.
    	 *	- require strict serialization.
    	 *	- do not return a value
    	 */
    
    	case SIOCSIFFLAGS:
    	case SIOCSIFMETRIC:
    	case SIOCSIFMTU:
    	case SIOCSIFHWADDR:
    	case SIOCSIFSLAVE:
    	case SIOCADDMULTI:
    	case SIOCDELMULTI:
    	case SIOCSIFHWBROADCAST:
    	case SIOCSMIIREG:
    	case SIOCBONDENSLAVE:
    	case SIOCBONDRELEASE:
    	case SIOCBONDSETHWADDR:
    	case SIOCBONDCHANGEACTIVE:
    	case SIOCBRADDIF:
    	case SIOCBRDELIF:
    	case SIOCSHWTSTAMP:
    
    		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
    
    			return -EPERM;
    		/* fall through */
    	case SIOCBONDSLAVEINFOQUERY:
    	case SIOCBONDINFOQUERY:
    		dev_load(net, ifr.ifr_name);
    		rtnl_lock();
    		ret = dev_ifsioc(net, &ifr, cmd);
    		rtnl_unlock();
    		return ret;
    
    	case SIOCGIFMEM:
    		/* Get the per device memory space. We can add this but
    		 * currently do not support it */
    	case SIOCSIFMEM:
    		/* Set the per device memory buffer space.
    		 * Not applicable in our case */
    	case SIOCSIFLINK:
    
    
    	/*
    	 *	Unknown or private ioctl.
    	 */
    	default:
    		if (cmd == SIOCWANDEV ||
    		    (cmd >= SIOCDEVPRIVATE &&
    		     cmd <= SIOCDEVPRIVATE + 15)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			rtnl_lock();
    
    			ret = dev_ifsioc(net, &ifr, cmd);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			rtnl_unlock();
    
    			if (!ret && copy_to_user(arg, &ifr,
    						 sizeof(struct ifreq)))
    				ret = -EFAULT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return ret;
    
    		}
    		/* Take care of Wireless Extensions */
    		if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
    			return wext_handle_ioctl(net, &ifr, cmd, arg);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    }
    
    
    /**
     *	dev_new_index	-	allocate an ifindex
    
     *	@net: the applicable net namespace
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *
     *	Returns a suitable unique value for a new device interface
     *	number.  The caller must hold the rtnl semaphore or the
     *	dev_base_lock to be sure it remains unique.
     */
    
    static int dev_new_index(struct net *net)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	int ifindex = net->ifindex;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	for (;;) {
    		if (++ifindex <= 0)
    			ifindex = 1;
    
    		if (!__dev_get_by_index(net, ifindex))
    
    			return net->ifindex = ifindex;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    }
    
    /* Delayed registration/unregisteration */
    
    static LIST_HEAD(net_todo_list);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    static void net_set_todo(struct net_device *dev)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	list_add_tail(&dev->todo_list, &net_todo_list);
    }
    
    
    static void rollback_registered_many(struct list_head *head)
    
    	struct net_device *dev, *tmp;
    
    	BUG_ON(dev_boot_phase);
    	ASSERT_RTNL();
    
    
    	list_for_each_entry_safe(dev, tmp, head, unreg_list) {
    
    		/* Some devices call without registering
    
    		 * for initialization unwind. Remove those
    		 * devices and proceed with the remaining.
    
    		 */
    		if (dev->reg_state == NETREG_UNINITIALIZED) {
    
    			pr_debug("unregister_netdevice: device %s/%p never was registered\n",
    				 dev->name, dev);
    
    			list_del(&dev->unreg_list);
    			continue;
    
    		dev->dismantle = true;
    
    		BUG_ON(dev->reg_state != NETREG_REGISTERED);
    
    	/* If device is running, close it first. */
    	dev_close_many(head);
    
    	list_for_each_entry(dev, head, unreg_list) {
    
    		/* And unlink it from device chain. */
    		unlist_netdevice(dev);
    
    		dev->reg_state = NETREG_UNREGISTERING;
    	}
    
    	list_for_each_entry(dev, head, unreg_list) {
    		/* Shutdown queueing discipline. */
    		dev_shutdown(dev);
    
    		/* Notify protocols, that we are about to destroy
    		   this device. They should clean all the things.
    		*/
    		call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
    
    		if (!dev->rtnl_link_ops ||
    		    dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
    			rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
    
    
    		/*
    		 *	Flush the unicast and multicast chains
    		 */
    
    		dev_mc_flush(dev);
    
    		if (dev->netdev_ops->ndo_uninit)
    			dev->netdev_ops->ndo_uninit(dev);
    
    		/* Notifier chain MUST detach us from master device. */
    		WARN_ON(dev->master);
    
    		/* Remove entries from kobject tree */
    		netdev_unregister_kobject(dev);
    	}
    
    	list_for_each_entry(dev, head, unreg_list)
    
    		dev_put(dev);
    }
    
    static void rollback_registered(struct net_device *dev)
    {
    	LIST_HEAD(single);
    
    	list_add(&dev->unreg_list, &single);
    	rollback_registered_many(&single);
    
    	list_del(&single);
    
    static netdev_features_t netdev_fix_features(struct net_device *dev,
    	netdev_features_t features)
    
    	/* Fix illegal checksum combinations */
    	if ((features & NETIF_F_HW_CSUM) &&
    	    (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
    
    		netdev_warn(dev, "mixed HW and IP checksum settings.\n");
    
    		features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
    	}
    
    
    	/* Fix illegal SG+CSUM combinations. */
    	if ((features & NETIF_F_SG) &&
    	    !(features & NETIF_F_ALL_CSUM)) {
    
    		netdev_dbg(dev,
    			"Dropping NETIF_F_SG since no checksum feature.\n");
    
    		features &= ~NETIF_F_SG;
    	}
    
    	/* TSO requires that SG is present as well. */
    
    	if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
    
    		netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
    
    		features &= ~NETIF_F_ALL_TSO;
    
    	/* TSO ECN requires that TSO is present as well. */
    	if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
    		features &= ~NETIF_F_TSO_ECN;
    
    
    	/* Software GSO depends on SG. */
    	if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
    
    		netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
    
    		features &= ~NETIF_F_GSO;
    	}
    
    
    	/* UFO needs SG and checksumming */
    
    	if (features & NETIF_F_UFO) {
    
    		/* maybe split UFO into V4 and V6? */
    		if (!((features & NETIF_F_GEN_CSUM) ||
    		    (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))
    			    == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
    
    				"Dropping NETIF_F_UFO since no checksum offload features.\n");
    
    			features &= ~NETIF_F_UFO;
    		}
    
    		if (!(features & NETIF_F_SG)) {
    
    				"Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n");
    
    			features &= ~NETIF_F_UFO;
    		}
    	}
    
    	return features;
    }
    
    
    int __netdev_update_features(struct net_device *dev)
    
    	features = netdev_get_wanted_features(dev);
    
    	if (dev->netdev_ops->ndo_fix_features)
    		features = dev->netdev_ops->ndo_fix_features(dev, features);
    
    	/* driver might be less strict about feature dependencies */
    	features = netdev_fix_features(dev, features);
    
    	if (dev->features == features)
    
    	netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
    		&dev->features, &features);
    
    
    	if (dev->netdev_ops->ndo_set_features)
    		err = dev->netdev_ops->ndo_set_features(dev, features);
    
    
    		netdev_err(dev,
    
    			"set_features() failed (%d); wanted %pNF, left %pNF\n",
    			err, &features, &dev->features);
    
    /**
     *	netdev_update_features - recalculate device features
     *	@dev: the device to check
     *
     *	Recalculate dev->features set and send notifications if it
     *	has changed. Should be called after driver or hardware dependent
     *	conditions might have changed that influence the features.
     */
    
    void netdev_update_features(struct net_device *dev)
    {
    	if (__netdev_update_features(dev))
    		netdev_features_change(dev);
    
    }
    EXPORT_SYMBOL(netdev_update_features);
    
    
    /**
     *	netdev_change_features - recalculate device features
     *	@dev: the device to check
     *
     *	Recalculate dev->features set and send notifications even
     *	if they have not changed. Should be called instead of
     *	netdev_update_features() if also dev->vlan_features might
     *	have changed to allow the changes to be propagated to stacked
     *	VLAN devices.
     */
    void netdev_change_features(struct net_device *dev)
    {
    	__netdev_update_features(dev);
    	netdev_features_change(dev);
    }
    EXPORT_SYMBOL(netdev_change_features);
    
    
    /**
     *	netif_stacked_transfer_operstate -	transfer operstate
     *	@rootdev: the root or lower level device to transfer state from
     *	@dev: the device to transfer operstate to
     *
     *	Transfer operational state from root to device. This is normally
     *	called when a stacking relationship exists between the root
     *	device and the device(a leaf device).
     */
    void netif_stacked_transfer_operstate(const struct net_device *rootdev,
    					struct net_device *dev)
    {
    	if (rootdev->operstate == IF_OPER_DORMANT)
    		netif_dormant_on(dev);
    	else
    		netif_dormant_off(dev);
    
    	if (netif_carrier_ok(rootdev)) {
    		if (!netif_carrier_ok(dev))
    			netif_carrier_on(dev);
    	} else {
    		if (netif_carrier_ok(dev))
    			netif_carrier_off(dev);
    	}
    }
    EXPORT_SYMBOL(netif_stacked_transfer_operstate);
    
    
    Tom Herbert's avatar
    Tom Herbert committed
    #ifdef CONFIG_RPS
    
    static int netif_alloc_rx_queues(struct net_device *dev)
    {
    	unsigned int i, count = dev->num_rx_queues;
    
    	struct netdev_rx_queue *rx;
    
    	BUG_ON(count < 1);
    
    	rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL);
    	if (!rx) {
    
    		pr_err("netdev: Unable to allocate %u rx queues\n", count);
    
    		return -ENOMEM;
    
    	dev->_rx = rx;
    
    	for (i = 0; i < count; i++)
    
    		rx[i].dev = dev;
    
    Tom Herbert's avatar
    Tom Herbert committed
    #endif
    
    Changli Gao's avatar
    Changli Gao committed
    static void netdev_init_one_queue(struct net_device *dev,
    				  struct netdev_queue *queue, void *_unused)
    {
    	/* Initialize queue lock */
    	spin_lock_init(&queue->_xmit_lock);
    	netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
    	queue->xmit_lock_owner = -1;
    
    	netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
    
    Changli Gao's avatar
    Changli Gao committed
    	queue->dev = dev;
    
    Tom Herbert's avatar
    Tom Herbert committed
    #ifdef CONFIG_BQL
    	dql_init(&queue->dql, HZ);
    #endif
    
    static int netif_alloc_netdev_queues(struct net_device *dev)
    {
    	unsigned int count = dev->num_tx_queues;
    	struct netdev_queue *tx;
    
    	BUG_ON(count < 1);
    
    	tx = kcalloc(count, sizeof(struct netdev_queue), GFP_KERNEL);
    	if (!tx) {
    
    		pr_err("netdev: Unable to allocate %u tx queues\n", count);
    
    		return -ENOMEM;
    	}
    	dev->_tx = tx;
    
    	netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
    	spin_lock_init(&dev->tx_global_lock);
    
    Changli Gao's avatar
    Changli Gao committed
    
    	return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     *	register_netdevice	- register a network device
     *	@dev: device to register
     *
     *	Take a completed network device structure and add it to the kernel
     *	interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
     *	chain. 0 is returned on success. A negative errno code is returned
     *	on a failure to set up the device, or if the name is a duplicate.
     *
     *	Callers must hold the rtnl semaphore. You may want
     *	register_netdev() instead of this.
     *
     *	BUGS:
     *	The locking appears insufficient to guarantee two parallel registers
     *	will not get the same name.
     */
    
    int register_netdevice(struct net_device *dev)
    {
    	int ret;
    
    	struct net *net = dev_net(dev);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	BUG_ON(dev_boot_phase);
    	ASSERT_RTNL();
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* When net_device's are persistent, this will be fatal. */
    	BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	spin_lock_init(&dev->addr_list_lock);
    
    	netdev_set_addr_lockdep_class(dev);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	dev->iflink = -1;
    
    
    	ret = dev_get_valid_name(net, dev, dev->name);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* Init, if this function is available */
    
    	if (dev->netdev_ops->ndo_init) {
    		ret = dev->netdev_ops->ndo_init(dev);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (ret) {
    			if (ret > 0)
    				ret = -EIO;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    	}
    
    	ret = -EBUSY;
    	if (!dev->ifindex)
    		dev->ifindex = dev_new_index(net);
    	else if (__dev_get_by_index(net, dev->ifindex))
    		goto err_uninit;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (dev->iflink == -1)
    		dev->iflink = dev->ifindex;
    
    
    	/* Transfer changeable features to wanted_features and enable
    	 * software offloads (GSO and GRO).
    	 */
    	dev->hw_features |= NETIF_F_SOFT_FEATURES;
    
    	dev->features |= NETIF_F_SOFT_FEATURES;
    	dev->wanted_features = dev->features & dev->hw_features;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Turn on no cache copy if HW is doing checksum */
    
    	if (!(dev->flags & IFF_LOOPBACK)) {
    		dev->hw_features |= NETIF_F_NOCACHE_COPY;
    		if (dev->features & NETIF_F_ALL_CSUM) {
    			dev->wanted_features |= NETIF_F_NOCACHE_COPY;
    			dev->features |= NETIF_F_NOCACHE_COPY;
    		}
    
    	/* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
    
    	dev->vlan_features |= NETIF_F_HIGHDMA;
    
    	ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
    	ret = notifier_to_errno(ret);
    	if (ret)
    		goto err_uninit;
    
    
    	ret = netdev_register_kobject(dev);
    
    	dev->reg_state = NETREG_REGISTERED;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/*
    	 *	Default initial state at registry is that the
    	 *	device is present.
    	 */
    
    	set_bit(__LINK_STATE_PRESENT, &dev->state);
    
    
    	linkwatch_init_dev(dev);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	dev_init_scheduler(dev);
    	dev_hold(dev);
    
    	add_device_randomness(dev->dev_addr, dev->addr_len);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Notify protocols, that a new device appeared. */
    
    	ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
    
    	ret = notifier_to_errno(ret);
    
    	if (ret) {
    		rollback_registered(dev);
    		dev->reg_state = NETREG_UNREGISTERED;
    	}
    
    	/*
    	 *	Prevent userspace races by waiting until the network
    	 *	device is fully setup before sending notifications.
    	 */
    
    	if (!dev->rtnl_link_ops ||
    	    dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
    		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    out:
    	return ret;
    
    	if (dev->netdev_ops->ndo_uninit)
    		dev->netdev_ops->ndo_uninit(dev);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    EXPORT_SYMBOL(register_netdevice);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    /**
     *	init_dummy_netdev	- init a dummy network device for NAPI
     *	@dev: device to init
     *
     *	This takes a network device structure and initialize the minimum
     *	amount of fields so it can be used to schedule NAPI polls without
     *	registering a full blown interface. This is to be used by drivers
     *	that need to tie several hardware interfaces to a single NAPI
     *	poll scheduler due to HW limitations.
     */
    int init_dummy_netdev(struct net_device *dev)
    {
    	/* Clear everything. Note we don't initialize spinlocks
    	 * are they aren't supposed to be taken by any of the
    	 * NAPI code and this dummy netdev is supposed to be
    	 * only ever used for NAPI polls
    	 */
    	memset(dev, 0, sizeof(struct net_device));
    
    	/* make sure we BUG if trying to hit standard
    	 * register/unregister code path
    	 */
    	dev->reg_state = NETREG_DUMMY;
    
    	/* NAPI wants this */
    	INIT_LIST_HEAD(&dev->napi_list);
    
    	/* a dummy interface is started by default */
    	set_bit(__LINK_STATE_PRESENT, &dev->state);
    	set_bit(__LINK_STATE_START, &dev->state);
    
    
    	/* Note : We dont allocate pcpu_refcnt for dummy devices,
    	 * because users of this 'device' dont need to change
    	 * its refcount.
    	 */
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     *	register_netdev	- register a network device
     *	@dev: device to register
     *
     *	Take a completed network device structure and add it to the kernel
     *	interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
     *	chain. 0 is returned on success. A negative errno code is returned
     *	on a failure to set up the device, or if the name is a duplicate.
     *
    
     *	This is a wrapper around register_netdevice that takes the rtnl semaphore
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *	and expands the device name if you passed a format string to
     *	alloc_netdev.
     */
    int register_netdev(struct net_device *dev)
    {
    	int err;
    
    	rtnl_lock();
    	err = register_netdevice(dev);
    	rtnl_unlock();
    	return err;
    }
    EXPORT_SYMBOL(register_netdev);
    
    
    int netdev_refcnt_read(const struct net_device *dev)
    {
    	int i, refcnt = 0;
    
    	for_each_possible_cpu(i)
    		refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
    	return refcnt;
    }
    EXPORT_SYMBOL(netdev_refcnt_read);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * netdev_wait_allrefs - wait until all references are gone.
    
     * @dev: target net_device
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *
     * This is called when unregistering network devices.
     *
     * Any protocol or device that holds a reference should register
     * for netdevice notification, and cleanup and put back the
     * reference if they receive an UNREGISTER event.
     * We can get stuck here if buggy protocols don't correctly
    
     * call dev_put.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    static void netdev_wait_allrefs(struct net_device *dev)
    {
    	unsigned long rebroadcast_time, warning_time;
    
    	int refcnt;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	rebroadcast_time = warning_time = jiffies;
    
    	refcnt = netdev_refcnt_read(dev);
    
    	while (refcnt != 0) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
    
    			rtnl_lock();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    			/* Rebroadcast unregister notification */
    
    			call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    			rcu_barrier();
    
    			call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
    				     &dev->state)) {
    				/* We must not have linkwatch events
    				 * pending on unregister. If this
    				 * happens, we simply run the queue
    				 * unscheduled, resulting in a noop
    				 * for this device.
    				 */
    				linkwatch_run_queue();
    			}
    
    
    			__rtnl_unlock();
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    			rebroadcast_time = jiffies;
    		}
    
    		msleep(250);
    
    
    		refcnt = netdev_refcnt_read(dev);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (time_after(jiffies, warning_time + 10 * HZ)) {
    
    			pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
    				 dev->name, refcnt);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			warning_time = jiffies;
    		}
    	}
    }
    
    /* The sequence is:
     *
     *	rtnl_lock();
     *	...
     *	register_netdevice(x1);
     *	register_netdevice(x2);
     *	...
     *	unregister_netdevice(y1);
     *	unregister_netdevice(y2);
     *      ...
     *	rtnl_unlock();
     *	free_netdev(y1);
     *	free_netdev(y2);
     *
    
     * We are invoked by rtnl_unlock().
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * This allows us to deal with problems:
    
     * 1) We can delete sysfs objects which invoke hotplug
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *    without deadlocking with linkwatch via keventd.
     * 2) Since we run with the RTNL semaphore not held, we can sleep
     *    safely in order to wait for the netdev refcnt to drop to zero.
    
     *
     * We must not return until all unregister events added during
     * the interval the lock was held have been completed.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    void netdev_run_todo(void)
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Snapshot list, allow later requests */
    
    	list_replace_init(&net_todo_list, &list);
    
    
    	__rtnl_unlock();