Linux network card data stream sending and receiving

Source: Internet
Author: User


Usually the network card driver code is relatively large, but can not be separated from sending and receiving, Master good backbone is good understanding and debugging.


Data sending:

Send function: Xxx_start_xmit ()

The upper package is sk_buff thrown into the kernel, buf stored in the Skb->data, through the Xxx_start_xmit (), sent out.
The following instance is sent out by writing to a device node, of course, the device must be a serial or SDIO transmission device,
You can also call the Write function directly to send the BUF out.

For example, Sdio writes:
Sdio_memcpy_toio (xxx->func, addr, buf, sizeof (BUF));

The filling of the net_device_ops structure problem realizes the operation of open and send in the inside:

static const struct Net_device_ops xxx_netdev_ops = {    . Ndo_open = Xxx_net_open,    . ndo_stop = Xxx_net_stop,    . Ndo_start_xmit = Xxx_start_xmit,    . ndo_get_stats =  xxx_get_stats,    . ndo_tx_timeout = Xxx_tx_timeout,};   static int xxx_start_xmit (struct sk_buff *skb, struct net_device *dev) {   ---------    nwrite = filep->f_op-> Write (Filep, Skb->data, Skb->len, &filep->f_pos);    Set_fs (OLD_FS);    if (Nwrite = = Skb->len) {        priv->stats.tx_packets++;        Priv->stats.tx_bytes +=skb->len;        Priv->netdev->trans_start = jiffies;    }    -----------}

Data Reception:

    at the moment I see the receive trigger is in the interrupt (low-speed serial port and Sdio card) read into the BUF after the asynchronous call NETIF_RX () submitted to the network stack,
The receiving data for the Sdio card is read BUF by the interrupt handler function that is called to the card after the host side receives the interrupt. Another is a soft interrupt to receive,
For example, USB and high-speed serial port data reception.
Netif_rx () is a standard network card to hand over the sk_buff data stream, usually the network card driver code is very large when you can not find the receiving function, search Netif_rx () is the best choice.


The following receive processing functions: first allocate SKB data space, then the read buf into the SKB, and then call Netif_rx (), the data into the network stack.

static int xxx_submit_skb (void *data) {    --------    SKB = NETDEV_ALLOC_SKB (Ndev, Pkt_len + net_ip_align);/* NET_IP_ ALIGN is 2 *    /if (!SKB) {        printk (kern_info "Error:out of Memory for Rx ' d frame\n");        priv->stats.rx_dropped++;        ret =-enomem;        goto error;    }     Skb_reserve (SKB, net_ip_align);    memcpy ((Skb_put (SKB, Pkt_len), Pkt_len), buf,sizeof (BUF);        Skb->dev = priv->netdev;    Skb->protocol = Eth_type_trans (SKB, Priv->netdev);    priv->stats.rx_packets++;    Priv->stats.rx_bytes + = Pkt_len;    ret = NETIF_RX (SKB);    if (ret! = 0)        printk (kern_info "%s:netif_rx error\n", __func__);    --------}



Linux network card data stream sending and receiving

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.