Skip to content
Snippets Groups Projects
dev.c 81.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
    			BUG_TRAP(!dev->ip6_ptr);
    			BUG_TRAP(!dev->dn_ptr);
    
    
    			/* It must be the very last action, 
    			 * after this 'dev' may point to freed up memory.
    			 */
    			if (dev->destructor)
    				dev->destructor(dev);
    			break;
    
    		default:
    			printk(KERN_ERR "network todo '%s' but state %d\n",
    			       dev->name, dev->reg_state);
    			break;
    		}
    	}
    
    out:
    	up(&net_todo_run_mutex);
    }
    
    /**
     *	alloc_netdev - allocate network device
     *	@sizeof_priv:	size of private data to allocate space for
     *	@name:		device name format string
     *	@setup:		callback to initialize device
     *
     *	Allocates a struct net_device with private data area for driver use
     *	and performs basic initialization.
     */
    struct net_device *alloc_netdev(int sizeof_priv, const char *name,
    		void (*setup)(struct net_device *))
    {
    	void *p;
    	struct net_device *dev;
    	int alloc_size;
    
    	/* ensure 32-byte alignment of both the device and private area */
    	alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
    	alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
    
    	p = kmalloc(alloc_size, GFP_KERNEL);
    	if (!p) {
    		printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
    		return NULL;
    	}
    	memset(p, 0, alloc_size);
    
    	dev = (struct net_device *)
    		(((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
    	dev->padded = (char *)dev - (char *)p;
    
    	if (sizeof_priv)
    		dev->priv = netdev_priv(dev);
    
    	setup(dev);
    	strcpy(dev->name, name);
    	return dev;
    }
    EXPORT_SYMBOL(alloc_netdev);
    
    /**
     *	free_netdev - free network device
     *	@dev: device
     *
     *	This function does the last stage of destroying an allocated device 
     * 	interface. The reference to the device object is released.  
     *	If this is the last reference then it will be freed.
     */
    void free_netdev(struct net_device *dev)
    {
    #ifdef CONFIG_SYSFS
    	/*  Compatiablity with error handling in drivers */
    	if (dev->reg_state == NETREG_UNINITIALIZED) {
    		kfree((char *)dev - dev->padded);
    		return;
    	}
    
    	BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
    	dev->reg_state = NETREG_RELEASED;
    
    	/* will free via class release */
    	class_device_put(&dev->class_dev);
    #else
    	kfree((char *)dev - dev->padded);
    #endif
    }
     
    /* Synchronize with packet receive processing. */
    void synchronize_net(void) 
    {
    	might_sleep();
    	synchronize_kernel();
    }
    
    /**
     *	unregister_netdevice - remove device from the kernel
     *	@dev: device
     *
     *	This function shuts down a device interface and removes it
     *	from the kernel tables. On success 0 is returned, on a failure
     *	a negative errno code is returned.
     *
     *	Callers must hold the rtnl semaphore.  You may want
     *	unregister_netdev() instead of this.
     */
    
    int unregister_netdevice(struct net_device *dev)
    {
    	struct net_device *d, **dp;
    
    	BUG_ON(dev_boot_phase);
    	ASSERT_RTNL();
    
    	/* Some devices call without registering for initialization unwind. */
    	if (dev->reg_state == NETREG_UNINITIALIZED) {
    		printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
    				  "was registered\n", dev->name, dev);
    		return -ENODEV;
    	}
    
    	BUG_ON(dev->reg_state != NETREG_REGISTERED);
    
    	/* If device is running, close it first. */
    	if (dev->flags & IFF_UP)
    		dev_close(dev);
    
    	/* And unlink it from device chain. */
    	for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
    		if (d == dev) {
    			write_lock_bh(&dev_base_lock);
    			hlist_del(&dev->name_hlist);
    			hlist_del(&dev->index_hlist);
    			if (dev_tail == &dev->next)
    				dev_tail = dp;
    			*dp = d->next;
    			write_unlock_bh(&dev_base_lock);
    			break;
    		}
    	}
    	if (!d) {
    		printk(KERN_ERR "unregister net_device: '%s' not found\n",
    		       dev->name);
    		return -ENODEV;
    	}
    
    	dev->reg_state = NETREG_UNREGISTERING;
    
    	synchronize_net();
    
    	/* Shutdown queueing discipline. */
    	dev_shutdown(dev);
    
    	
    	/* Notify protocols, that we are about to destroy
    	   this device. They should clean all the things.
    	*/
    	notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
    	
    	/*
    	 *	Flush the multicast chain
    	 */
    	dev_mc_discard(dev);
    
    	if (dev->uninit)
    		dev->uninit(dev);
    
    	/* Notifier chain MUST detach us from master device. */
    	BUG_TRAP(!dev->master);
    
    	free_divert_blk(dev);
    
    	/* Finish processing unregister after unlock */
    	net_set_todo(dev);
    
    	synchronize_net();
    
    	dev_put(dev);
    	return 0;
    }
    
    /**
     *	unregister_netdev - remove device from the kernel
     *	@dev: device
     *
     *	This function shuts down a device interface and removes it
     *	from the kernel tables. On success 0 is returned, on a failure
     *	a negative errno code is returned.
     *
     *	This is just a wrapper for unregister_netdevice that takes
     *	the rtnl semaphore.  In general you want to use this and not
     *	unregister_netdevice.
     */
    void unregister_netdev(struct net_device *dev)
    {
    	rtnl_lock();
    	unregister_netdevice(dev);
    	rtnl_unlock();
    }
    
    EXPORT_SYMBOL(unregister_netdev);
    
    #ifdef CONFIG_HOTPLUG_CPU
    static int dev_cpu_callback(struct notifier_block *nfb,
    			    unsigned long action,
    			    void *ocpu)
    {
    	struct sk_buff **list_skb;
    	struct net_device **list_net;
    	struct sk_buff *skb;
    	unsigned int cpu, oldcpu = (unsigned long)ocpu;
    	struct softnet_data *sd, *oldsd;
    
    	if (action != CPU_DEAD)
    		return NOTIFY_OK;
    
    	local_irq_disable();
    	cpu = smp_processor_id();
    	sd = &per_cpu(softnet_data, cpu);
    	oldsd = &per_cpu(softnet_data, oldcpu);
    
    	/* Find end of our completion_queue. */
    	list_skb = &sd->completion_queue;
    	while (*list_skb)
    		list_skb = &(*list_skb)->next;
    	/* Append completion queue from offline CPU. */
    	*list_skb = oldsd->completion_queue;
    	oldsd->completion_queue = NULL;
    
    	/* Find end of our output_queue. */
    	list_net = &sd->output_queue;
    	while (*list_net)
    		list_net = &(*list_net)->next_sched;
    	/* Append output queue from offline CPU. */
    	*list_net = oldsd->output_queue;
    	oldsd->output_queue = NULL;
    
    	raise_softirq_irqoff(NET_TX_SOFTIRQ);
    	local_irq_enable();
    
    	/* Process offline CPU's input_pkt_queue */
    	while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
    		netif_rx(skb);
    
    	return NOTIFY_OK;
    }
    #endif /* CONFIG_HOTPLUG_CPU */
    
    
    /*
     *	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);
    
    	net_random_init();
    
    	if (dev_proc_init())
    		goto out;
    
    	if (netdev_sysfs_init())
    		goto out;
    
    	INIT_LIST_HEAD(&ptype_all);
    	for (i = 0; i < 16; i++) 
    		INIT_LIST_HEAD(&ptype_base[i]);
    
    	for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
    		INIT_HLIST_HEAD(&dev_name_head[i]);
    
    	for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
    		INIT_HLIST_HEAD(&dev_index_head[i]);
    
    	/*
    	 *	Initialise the packet receive queues.
    	 */
    
    	for (i = 0; i < NR_CPUS; i++) {
    		struct softnet_data *queue;
    
    		queue = &per_cpu(softnet_data, i);
    		skb_queue_head_init(&queue->input_pkt_queue);
    		queue->throttle = 0;
    		queue->cng_level = 0;
    		queue->avg_blog = 10; /* arbitrary non-zero */
    		queue->completion_queue = NULL;
    		INIT_LIST_HEAD(&queue->poll_list);
    		set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
    		queue->backlog_dev.weight = weight_p;
    		queue->backlog_dev.poll = process_backlog;
    		atomic_set(&queue->backlog_dev.refcnt, 1);
    	}
    
    #ifdef OFFLINE_SAMPLE
    	samp_timer.expires = jiffies + (10 * HZ);
    	add_timer(&samp_timer);
    #endif
    
    	dev_boot_phase = 0;
    
    	open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
    	open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
    
    	hotcpu_notifier(dev_cpu_callback, 0);
    	dst_init();
    	dev_mcast_init();
    	rc = 0;
    out:
    	return rc;
    }
    
    subsys_initcall(net_dev_init);
    
    EXPORT_SYMBOL(__dev_get_by_index);
    EXPORT_SYMBOL(__dev_get_by_name);
    EXPORT_SYMBOL(__dev_remove_pack);
    EXPORT_SYMBOL(__skb_linearize);
    EXPORT_SYMBOL(dev_add_pack);
    EXPORT_SYMBOL(dev_alloc_name);
    EXPORT_SYMBOL(dev_close);
    EXPORT_SYMBOL(dev_get_by_flags);
    EXPORT_SYMBOL(dev_get_by_index);
    EXPORT_SYMBOL(dev_get_by_name);
    EXPORT_SYMBOL(dev_ioctl);
    EXPORT_SYMBOL(dev_open);
    EXPORT_SYMBOL(dev_queue_xmit);
    EXPORT_SYMBOL(dev_remove_pack);
    EXPORT_SYMBOL(dev_set_allmulti);
    EXPORT_SYMBOL(dev_set_promiscuity);
    EXPORT_SYMBOL(dev_change_flags);
    EXPORT_SYMBOL(dev_set_mtu);
    EXPORT_SYMBOL(dev_set_mac_address);
    EXPORT_SYMBOL(free_netdev);
    EXPORT_SYMBOL(netdev_boot_setup_check);
    EXPORT_SYMBOL(netdev_set_master);
    EXPORT_SYMBOL(netdev_state_change);
    EXPORT_SYMBOL(netif_receive_skb);
    EXPORT_SYMBOL(netif_rx);
    EXPORT_SYMBOL(register_gifconf);
    EXPORT_SYMBOL(register_netdevice);
    EXPORT_SYMBOL(register_netdevice_notifier);
    EXPORT_SYMBOL(skb_checksum_help);
    EXPORT_SYMBOL(synchronize_net);
    EXPORT_SYMBOL(unregister_netdevice);
    EXPORT_SYMBOL(unregister_netdevice_notifier);
    EXPORT_SYMBOL(net_enable_timestamp);
    EXPORT_SYMBOL(net_disable_timestamp);
    EXPORT_SYMBOL(dev_get_flags);
    
    #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
    EXPORT_SYMBOL(br_handle_frame_hook);
    EXPORT_SYMBOL(br_fdb_get_hook);
    EXPORT_SYMBOL(br_fdb_put_hook);
    #endif
    
    #ifdef CONFIG_KMOD
    EXPORT_SYMBOL(dev_load);
    #endif
    
    EXPORT_PER_CPU_SYMBOL(softnet_data);