Linux Virtual NIC Implementation __linux

Source: Internet
Author: User
This document copyleft Rosetta all, the use of the GPL release, you can freely copy, reprint, reprint, please maintain the integrity of the document.
Reference: "Linux device driver third Edition", Snull source code, linux-2.6.10

has been a period of time to see Openswan source code, there is a problem puzzled me for a long time, that is, it is the IPSec virtual network card interface is how to achieve. At that time there is no thought, no idea, because do not know where to start to solve this problem, recently contacted the kernel module of the writing, and contact with the Openswan klips module, the original all these belong to the network Driver programming category. Now I can not wait to understand the implementation of it, and then have this study notes ...
This article is only a preliminary explanation of the virtual network card implementation process, the final implementation of a virtual network card, for specific body details and packet transmission and transmission, and so on are not involved. The implementation of Klips's ipsec0 is largely analogous to this process.
This document pays attention to the actual realization process, lacks the theory knowledge.
This document is based on the "Linux device driver third Edition" as a theoretical knowledge of the Snull source code as the Learning object. In order to covet labor, the source code to Snull source and linux-2.6.10 kernel source code.

First, the final effect, the realization of a virtual network card named Sn0 interface
[Root@xxx snull]# Cat/proc/net/dev
inter-| Receive
Face |bytes packets errs drop FIFO
lo:6528 76 0 0 0
eth0:148681882 216304 0 0 0
eth1:0 0 0 0 0
eth2:0 0 0 0 0
sit0:0 0 0 0 0
sn0:0 0 0 0 0
sn1:210 3 0 0 0

[Root@xxx snull]# ifconfig sn0 up
[Root@xxx snull]# ifconfig sn0
Sn0 Link encap:ethernet hwaddr 00:53:4e:55:4c:30
Inet6 ADDR:FE80::253:4EFF:FE55:4C30/64 Scope:link
Up broadcast RUNNING NOARP multicast mtu:1500 metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0  (0.0 b) TX bytes:70 (70.0 b)

Ii. Overall process
The kernel is first required to allocate a NET_DEVICE structure (hereinafter referred to as the dev structure) and initialize the dev structure.
struct Net_device *snull_devs[2]; Snull implements two virtual network cards, with a two size structure array to save the net_device structure, I only focus on one.
Snull_devs[0] = Alloc_netdev (sizeof (struct snull_priv), "sn%d",
Snull_init)//kernel allocates a dev, this dev is initialized by Snull_init. The dev pointer is then returned by the Alloc_netdev function.

for (i = 0; i < 2; i++)
if (result = Register_netdev (Snull_devs[i]))//dev The Register_netdev is registered to the kernel after the initialization of the structure, the device (virtual network card) can be operated on after registration.
PRINTK ("Snull:error%i registering device \"%s\ "\ n"),
result, snull_devs[i]->name);
Else
ret = 0;


III. initialization Process
To facilitate understanding of the initialization process, post a Alloc_netdev kernel implementation:
struct Net_device *alloc_netdev (int sizeof_priv, const char *mask,
void (*setup) (struct Net_device *))//The last parameter is a function pointer, which points to the incoming Snull_init
{
void *p;
struct Net_device *dev;
int alloc_size;

/* Ensure 32-byte alignment of both the device and private area * *

Alloc_size = (sizeof (struct net_device) + 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)//Allocate memory space for dev structure
dev->padded = (char *) Dev-(char *) p;

if (SIZEOF_PRIV)
Dev->priv = Netdev_priv (dev);

The setup (dev)//implementation invokes Snull_init (DEV) initialization of the dev structure.
strcpy (Dev->name, mask);

Return dev;//returns an initialized dev
}

Here's a look at this important initialization function snull_init (), some important function pointers for Dev are assigned here
void Snull_init (struct net_device *dev)
{
struct Snull_priv *priv;
/*
* Then, assign, fields in Dev, using Ether_setup () and some
* Hand Assignments
*/
Ether_setup (Dev); /* Assign some of the fields *///this function initializes many of the Dev's members

Dev->open = Snull_open; Open device
Dev->stop = Snull_release;
Dev->set_config = Snull_config;
Dev->hard_start_xmit = Snull_tx; Data Send function
Dev->do_ioctl = Snull_ioctl;
Dev->get_stats = Snull_stats;
DEV-&GT;CHANGE_MTU = SNULL_CHANGE_MTU;
Dev->rebuild_header = Snull_rebuild_header;
Dev->hard_header = snull_header;//Set up hardware headers, containing source and destination MAC addresses
Dev->tx_timeout = snull_tx_timeout;//number send timeout processing.
Dev->watchdog_timeo = timeout;
if (USE_NAPI) {
Dev->poll = Snull_poll;
Dev->weight = 2;
}
* Keep the default flags, just add noarp * *
Dev->flags |= Iff_noarp;
Dev->features |= netif_f_no_csum;
Dev->hard_header_cache = NULL; /* Disable Caching * *

/*
* Then, initialize the Priv field. This encloses the statistics
* and a few private fields.
*/
Priv = Netdev_priv (dev);
memset (priv, 0, sizeof (struct snull_priv));
Spin_lock_init (&priv->lock);
Snull_rx_ints (Dev, 1); * Enable receive interrupts * *
Snull_setup_pool (Dev);
}

The above content has already put the frame out, as to net structure member's understanding and the detail, the packet sends receives, the timeout transmission interrupt processing and the SK_BUFF data structure and so on
Information is available in reference to the given reference.
Similar to the implementation of the network driver source code also has the kernel source of loopback.c, Plip.c, e100.c.














Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.