Linux-driven Virtual NIC __linux

Source: Internet
Author: User

Write the network card driver before I sum up some of the views of the individual: in fact, write drive is not so difficult to think about, here I objectively evaluate the core layer and application layer difference:

Underlying:

A friend who works at the kernel level should have this feeling, just start to learn when it is really difficult, that is to say, I take the Linux driver, write a complete drive, you have to install a virtual machine run Linux, used to compile the driver, the virtual machine needs to be installed some libraries and tools, The driver must run on a complete system, so first you have to build the whole system, you have to understand the hardware timing and so on, these things are really a headache for beginners, but you will find that you are really a driver of the developer after you have a kind of enlightened feeling, originally write driver so simple, The framework is unchanged. So the software developers who work at the bottom don't feel like they're too strong, just a little bit higher.

Application layer:

The application layer is relatively simple to start with, mainly in the following aspects of the embodiment:

First, the use of very few tools, write applications are basically integrated development environment, then a tool, the compilation of successful basic can be used.

Second, debugging the code is the most convenient, add some print statements, and then run to find the logical structure of the error. Very time saving

Third, there is a mistake, online a paste, basically solve the problem.

Difficult point: Write an application logical structure thinking is very strong, your code is also relatively much more daily contact things may not be the same, need to constantly update their knowledge.

The above is only a personal point of view, if you have different views can leave a message.

Writing this code encountered a problem, has not been resolved, the problem is described as follows: I ping other IP address is able to ping, is not able to ping himself, I do not know the reason, there are reasons for friends, hope to be able to share.



Virtual NIC driver Summary as follows: *
* Reference drivers\net\cs89x0.c
*/
static struct Net_device *vnet_dev;


static void Emulator_rx_packet (struct sk_buff *skb struct net_device)
{
/* Reference LDD3 * *
unsigned char *type;
struct IPHDR *ih;
__be32 *saddr, *DADDR, TMP;
unsigned char Tmp_dev_addr[eth_alen];
struct ETHHDR *ethhdr;

struct Sk_buff *rx_skb;

Read/save data from hardware
/* Swap the "source/destination" MAC address * *
ETHHDR = (struct ETHHDR *) skb->data;
memcpy (Tmp_dev_addr, Ethhdr->h_dest, Eth_alen);
memcpy (Ethhdr->h_dest, Ethhdr->h_source, Eth_alen);
memcpy (Ethhdr->h_source, tmp_dev_addr, Eth_alen);


/* Swap the "source/destination" IP address * *
IH = (struct IPHDR *) (skb->data + sizeof (struct ETHHDR));
SADDR = &ih->saddr;
DADDR = &ih->daddr;


TMP = *SADDR;
*SADDR = *daddr;
*DADDR = tmp;

((U8 *) saddr) [2] ^= 1; /* Change the third octet (class C) * *
((U8 *) daddr) [2] ^= 1;
Type = skb->data + sizeof (struct ETHHDR) + sizeof (struct IPHDR);
PRINTK ("tx Package type =%02x\n", *type);
Modify type, originally 0x8 to express ping
*type = 0; /* 0 means reply * *

Ih->check = 0; /* and rebuild the checksum (IP needs it) * *
Ih->check = Ip_fast_csum ((unsigned char *) IH,IH->IHL);

Construct a Sk_buff
RX_SKB = DEV_ALLOC_SKB (Skb->len + 2);
Skb_reserve (RX_SKB, 2); /* Align IP on 16B boundary * *
memcpy (Skb_put (RX_SKB, Skb->len), Skb->data, Skb->len);


/* Write metadata, and then pass to the receive level * *
Rx_skb->dev = Dev;
Rx_skb->protocol = Eth_type_trans (rx_skb, Dev);
rx_skb->ip_summed = checksum_unnecessary; /* don ' t check it/*
dev->stats.rx_packets++;
Dev->stats.rx_bytes + + skb->len;


Submit Sk_buff
Netif_rx (RX_SKB);
}

static int virt_net_send_packet (struct sk_buff *skb, struct net_device)
{
static int cnt = 0;
PRINTK ("Virt_net_send_packet cnt =%d\n", ++cnt);


* * For the real network card, the data in the SKB through the network card sent out * *
Netif_stop_queue (Dev); /* Stop the queue for this network adapter * *
/* ... * * * SKB data to the network card


/* Construct a false Sk_buff, report * *
Emulator_rx_packet (SKB, Dev);

DEV_KFREE_SKB (SKB); /* Release SKB * *
Netif_wake_queue (Dev); /* All data sent out, wake up the network card queue * *

/* UPDATE STATISTICS * *
dev->stats.tx_packets++;
Dev->stats.tx_bytes + + skb->len;

return 0;
}

static int virt_net_init (void)
{
/* 1. Assign a NET_DEVICE structure body * *
Vnet_dev = Alloc_netdev (0, "vnet%d", Ether_setup); * Alloc_etherdev * *

/* 2. Set */
Vnet_dev->hard_start_xmit = Virt_net_send_packet;

/* Set MAC Address * *
Vnet_dev->dev_addr[0] = 0x08;
VNET_DEV->DEV_ADDR[1] = 0x89;
VNET_DEV->DEV_ADDR[2] = 0x89;
VNET_DEV->DEV_ADDR[3] = 0x89;
VNET_DEV->DEV_ADDR[4] = 0x89;
VNET_DEV->DEV_ADDR[5] = 0x11;


/* Set the following two items to ping * *
Vnet_dev->flags |= Iff_noarp;
Vnet_dev->features |= netif_f_no_csum;


/* 3. Registration * *
Register_netdevice (Vnet_dev);
Register_netdev (Vnet_dev);

return 0;
}


static void Virt_net_exit (void)
{
Unregister_netdev (Vnet_dev);
Free_netdev (Vnet_dev);
}



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.