send packet to Linux networking stack - Linux

This is a discussion on send packet to Linux networking stack - Linux ; Hello All, I am running a module which will receive the CPU packet from one of my ASIC chip. After receiving the packet from hardware i need to convert to SKB format and send it to the Linux networking stack. ...

+ Reply to Thread
Results 1 to 12 of 12

Thread: send packet to Linux networking stack

  1. send packet to Linux networking stack

    Hello All,

    I am running a module which will receive the CPU packet from one of my
    ASIC chip. After receiving the packet from hardware i need to convert
    to SKB format and send it to the Linux networking stack.
    Which API call i should call from my module to send those packet to
    linux networking queue.
    I used netif_rx call to send those packet but I am not able to check
    whter it reaches to Linux networking stack.

    Could anyone throw some light on this.

    Thanks a million
    Kamaraj

  2. Re: send packet to Linux networking stack

    On Thu, 9 Oct 2008 05:12:24 -0700 (PDT), Kamaraj
    wrote:

    > Which API call i should call from my module to send those packet to
    > linux networking queue.


    I recently worked on a project where I had to send a custom ethernet
    frame to the networking stack from a driver (i.e. from within the
    kernel). Note that this was an ethernet frame, not an IP datagram.
    I did this:

    /* Send an ethernet frame.
    */
    static void frame_tx (void)
    {
    custom_hdr *hdr;
    struct sk_buff *skb = alloc_skb (sizeof (custom_hdr), GFP_ATOMIC);

    if (skb)
    {
    skb->nh.raw = skb->mac.raw = skb->data;
    skb->dev = state.net_dev;
    skb->protocol = __constant_htons (CUSTOM_ETH_P);
    skb->priority = 0;
    skb_put (skb, sizeof (custom_hdr));
    skb->ip_summed = CHECKSUM_NONE;
    skb->next = skb->prev = NULL;

    hdr = (custom_hdr *) skb->mac.raw;

    /* building contents of frame deleted */

    dev_queue_xmit (skb);
    }
    }


    > I used netif_rx call to send those packet but I am not able to check
    > whter it reaches to Linux networking stack.


    You could use tcpdump or wireshark to see if it appears on the wire.


    --
    -| Comrade Bob Hauck
    -| Proud part owner of the People's Mortgage & Insurance Companies
    -| http://www.haucks.org/

  3. Re: send packet to Linux networking stack

    Kamaraj wrote:
    > Hello All,
    >
    > I am running a module which will receive the CPU packet from one of my
    > ASIC chip. After receiving the packet from hardware i need to convert
    > to SKB format and send it to the Linux networking stack.
    > Which API call i should call from my module to send those packet to
    > linux networking queue.
    > I used netif_rx call to send those packet but I am not able to check
    > whter it reaches to Linux networking stack.
    >
    > Could anyone throw some light on this.


    netif_rx() is the appropriate API. If your driver is coded properly,
    you should be able to use tcpdump() from userspace to listen in on all
    packets received on your interface.

    Chris

  4. Re: send packet to Linux networking stack

    Bob Hauck wrote in
    news:slrngesa4p.5ca.postmaster@bigbird.haucks.org:

    > On Thu, 9 Oct 2008 05:12:24 -0700 (PDT), Kamaraj
    > wrote:
    >
    >> Which API call i should call from my module to send those packet to
    >> linux networking queue.

    >
    > I recently worked on a project where I had to send a custom ethernet
    > frame to the networking stack from a driver (i.e. from within the
    > kernel). Note that this was an ethernet frame, not an IP datagram.
    > I did this:


    > struct sk_buff *skb = alloc_skb (sizeof (custom_hdr), GFP_ATOMIC);

    [snip]
    > dev_queue_xmit (skb);


    You are confused (or at least your terminology is :-). By doing this,
    you were *not* sending a packet "to the networking stack". Instead you
    were placing it in queue to be transmitted outbound via your device and
    in fact you were actually *bypassing* the stack by doing so.

    The networking stack -- aka TCP/IP stack aka protocol stack -- refers to
    the layered stack of protocol handlers that reside within the kernel
    (see for example the wikipedia article:
    http://en.wikipedia.org/wiki/Protocol_stack). Hence, to speak of
    sending a packet to the network stack from the driver, which is the
    bottom end of the stack, implies that the packet was received from the
    wire (or is to be treated as if it did).

    >> I used netif_rx call to send those packet but I am not able to check
    >> whter it reaches to Linux networking stack.

    >
    > You could use tcpdump or wireshark to see if it appears on the wire.


    A packet sent by the driver to the "networking stack" would only be
    visible to tcpdump/wireshark running on the same host. Likewise, a
    packet sent on the wire by the driver would be visible only on another
    host.

    GH

  5. Re: send packet to Linux networking stack

    On Thu, 9 Oct 2008 22:40:53 +0200 (CEST), Gil Hamilton
    wrote:

    > Bob Hauck wrote in
    > news:slrngesa4p.5ca.postmaster@bigbird.haucks.org:
    >
    >> On Thu, 9 Oct 2008 05:12:24 -0700 (PDT), Kamaraj
    >> wrote:
    >>
    >>> Which API call i should call from my module to send those packet to
    >>> linux networking queue.

    >>
    >> I recently worked on a project where I had to send a custom ethernet
    >> frame to the networking stack from a driver (i.e. from within the
    >> kernel). Note that this was an ethernet frame, not an IP datagram.
    >> I did this:

    >
    >> struct sk_buff *skb = alloc_skb (sizeof (custom_hdr), GFP_ATOMIC);

    > [snip]
    >> dev_queue_xmit (skb);

    >
    > You are confused (or at least your terminology is :-).


    Thanks ever so much for clearing things up for me!


    > Instead you were placing it in queue to be transmitted outbound via
    > your device and in fact you were actually *bypassing* the stack by
    > doing so.


    I wasn't clear on what the OP meant. What kind of packet was he trying
    to send? He talks of the "networking queue". What does he mean by
    that? Does he mean the IP stack, or the send queue of the ethernet
    driver? I submit that neither one of us knows, and I gave an example of
    what I thought he meant.

    I'm sorry you didn't like my answer. You get what you pay for I guess.
    Do you have help to offer, or just pedantry and Wikipedia articles?


    --
    -| Comrade Bob Hauck
    -| Proud part owner of the People's Mortgage & Insurance Companies
    -| http://www.haucks.org/

  6. Re: send packet to Linux networking stack

    On Oct 10, 6:38*am, Bob Hauck
    wrote:
    > On Thu, 9 Oct 2008 22:40:53 +0200 (CEST), Gil Hamilton
    >
    >
    >
    >
    >
    > wrote:
    > > Bob Hauck wrote in
    > >news:slrngesa4p.5ca.postmaster@bigbird.haucks.org:

    >
    > >> On Thu, 9 Oct 2008 05:12:24 -0700 (PDT), Kamaraj
    > >> wrote:

    >
    > >>> Which API call i should call from my module to send those packet to
    > >>> linux networking queue.

    >
    > >> I recently worked on a project where I had to send a custom ethernet
    > >> frame to the networking stack from a driver (i.e. from within the
    > >> kernel). *Note that this was an ethernet frame, not an IP datagram.
    > >> I did this:

    >
    > >> * * struct sk_buff *skb = alloc_skb (sizeof (custom_hdr), GFP_ATOMIC);

    > > [snip]
    > >> * * * * dev_queue_xmit (skb);

    >
    > > You are confused (or at least your terminology is :-). *

    >
    > Thanks ever so much for clearing things up for me!
    >
    > > Instead you were placing it in queue to be transmitted outbound via
    > > your device and in fact you were actually *bypassing* the stack by
    > > doing so.

    >
    > I wasn't clear on what the OP meant. *What kind of packet was he trying
    > to send? *He talks of the "networking queue". *What does he mean by
    > that? *Does he mean the IP stack, or the send queue of the ethernet
    > driver? *I submit that neither one of us knows, and I gave an example of
    > what I thought he meant.
    >
    > I'm sorry you didn't like my answer. *You get what you pay for I guess.*
    > Do you have help to offer, or just pedantry and Wikipedia articles?
    >
    > --
    > *-| Comrade Bob Hauck
    > *-| Proud part owner of the People's Mortgage & Insurance Companies
    > *-|http://www.haucks.org/- Hide quoted text -
    >
    > - Show quoted text -

    Thanks for all your comments !!!
    Yes. I would like to send the packet to the Linux IP stack.
    I thought netif_rx api send the skb buffer to the network interface
    scheduler and forward to the Linnux IP stack.
    As Hamiltion clearly mentioned dev_queue_xmit(skb) api sends out of
    the interface !!! it will not send it to the own interface !!

    Just prepare the skb and call the netif_rx() API from my kernel module
    to send the packet to own Linux IP stack?
    or do i need to initate the from Botton Half(deferred processing) and
    try to schedule a packet??
    Could you please clarify at this point ??

    Thanks,
    Kamaraj

  7. Re: send packet to Linux networking stack

    Bob Hauck wrote in
    news:slrngetcgv.t8n.postmaster@robin.haucks.org:

    > On Thu, 9 Oct 2008 22:40:53 +0200 (CEST), Gil Hamilton
    > wrote:


    >> Instead you were placing it in queue to be transmitted outbound via
    >> your device and in fact you were actually *bypassing* the stack by
    >> doing so.

    >
    > I wasn't clear on what the OP meant. What kind of packet was he

    trying
    > to send? He talks of the "networking queue". What does he mean by
    > that? Does he mean the IP stack, or the send queue of the ethernet
    > driver? I submit that neither one of us knows, and I gave an example

    of
    > what I thought he meant.
    >
    > I'm sorry you didn't like my answer. You get what you pay for I

    guess.
    > Do you have help to offer, or just pedantry and Wikipedia articles?


    I'm sorry you didn't like my answer. I wasn't trying to be a jerk.
    When you throw misleading info out onto usenet, you should not be
    surprised when someone offers to correct it. The OP said:

    >>> After receiving the packet from hardware i need to convert
    >>> to SKB format and send it to the Linux networking stack.


    How would calling dev_queue_xmit as you suggested be helpful in
    accomplishing that goal?

    GH

  8. Re: send packet to Linux networking stack

    pkamaraj@gmail.com wrote in news:08b0cd6f-a285-4af8-bb87-3871d6767f84
    @v30g2000hsa.googlegroups.com:

    > Yes. I would like to send the packet to the Linux IP stack.
    > I thought netif_rx api send the skb buffer to the network interface
    > scheduler and forward to the Linnux IP stack.


    > Just prepare the skb and call the netif_rx() API from my kernel module
    > to send the packet to own Linux IP stack?
    > or do i need to initate the from Botton Half(deferred processing) and
    > try to schedule a packet??
    > Could you please clarify at this point ??


    netif_rx is the traditional way to insert sk_buffs into the stack. So yes
    that should work and can be called either directly from an interrupt
    handler or from a deferred bottom half routine (such as a tasklet).

    That being said, NAPI is now the preferred network device interface. A
    NAPI driver would call netif_receive_skb (instead of netif_rx) from its
    poll routine. The big advantage of NAPI is that it provides interrupt
    moderation in high speed network drivers. If your driver is expecting to
    receive large numbers of packets, NAPI is the way to go. You might find
    this article helpful: http://lwn.net/Articles/30107/

    The traditional mechanism should work fine if you only expect to receive
    packets at low rates.

    GH

  9. Re: send packet to Linux networking stack

    On Fri, 10 Oct 2008 14:56:57 +0200 (CEST), Gil Hamilton
    wrote:

    >>>> After receiving the packet from hardware i need to convert
    >>>> to SKB format and send it to the Linux networking stack.

    >
    > How would calling dev_queue_xmit as you suggested be helpful in
    > accomplishing that goal?


    It wouldn't. I obviously misunderstood the question. I think it would
    have been sufficient to point that out as opposed to supposing I need to
    read Wikipedia to understand what a network stack is.


    --
    -| Comrade Bob Hauck
    -| Proud part owner of the People's Mortgage & Insurance Companies
    -| http://www.haucks.org/

  10. Re: send packet to Linux networking stack

    Chris Friesen wrote:
    > Kamaraj wrote:
    >> Hello All,
    >>
    >> I am running a module which will receive the CPU packet from one of my
    >> ASIC chip. After receiving the packet from hardware i need to convert
    >> to SKB format and send it to the Linux networking stack.
    >> Which API call i should call from my module to send those packet to
    >> linux networking queue.
    >> I used netif_rx call to send those packet but I am not able to check
    >> whter it reaches to Linux networking stack.
    >>
    >> Could anyone throw some light on this.

    >
    > netif_rx() is the appropriate API. If your driver is coded properly,
    > you should be able to use tcpdump() from userspace to listen in on all
    > packets received on your interface.
    >
    > Chris



    The tun and tap drivers could help, as they
    are interfaces from network stack link and
    packet layers to and from character devices.

    --

    Tauno Voipio
    tauno voipio (at) iki fi

  11. Re: send packet to Linux networking stack

    On Oct 10, 6:15*pm, Gil Hamilton wrote:
    > pkama...@gmail.com wrote in news:08b0cd6f-a285-4af8-bb87-3871d6767f84
    > @v30g2000hsa.googlegroups.com:
    >
    > > Yes. I would like to send the packet to the Linux IP stack.
    > > I thought netif_rx api send the skb buffer to the network interface
    > > scheduler and forward to the Linnux IP stack.
    > > Just prepare the skb and call the netif_rx() API from my kernel module
    > > to send the packet to own Linux IP stack?
    > > or do i need to initate the from Botton Half(deferred processing) and
    > > try to schedule a packet??
    > > Could you please clarify at this point ??

    >
    > netif_rx is the traditional way to insert sk_buffs into the stack. *So yes
    > that should work and can be called either directly from an interrupt
    > handler or from a deferred bottom half routine (such as a tasklet).
    >
    > That being said, NAPI is now the preferred network device interface. *A
    > NAPI driver would call netif_receive_skb (instead of netif_rx) from its
    > poll routine. *The big advantage of NAPI is that it provides interrupt
    > moderation in high speed network drivers. *If your driver is expecting to
    > receive large numbers of packets, NAPI is the way to go. *You might find
    > this article helpful:http://lwn.net/Articles/30107/
    >
    > The traditional mechanism should work fine if you only expect to receive
    > packets at low rates.
    >
    > GH


    unsigned char arp_frame[] =
    {
    // Remarks :
    // Target IP Default : 192.168.1.9 which needs to change on the fly.
    //--- ethernet header --------------------------------------------
    0xff,0xff,0xff,0xff,0xff,0xff,
    0x00,0x01,0x02,0x03,0x04,0x05,
    0x08,0x06, //ARP_TYPE
    //--- arp header ------------------------------------------------
    0x00,0x01, //hardware type 10MB Ethernet
    0x08,0x00, // protocol type - IP
    0x06, // length of hardware address
    0x04, // length of protocol address
    0x0,0x1, // opcode - arp request
    0x00,0x01,0x02,0x03,0x04,0x05, //sender's hardware address
    192,168,1,11, //sender's IP
    0,0,0,0,0,0, // target hardware
    192,168,1,99 // target's IP
    };

    int init_module(void)
    {
    struct net_device *dev=NULL;
    struct sk_buff *skb=NULL;
    int type;
    unsigned char *ptr= NULL;
    ptr = &arp_frame;
    int count,status;
    int pkt_len=0;
    if ((skb = (struct sk_buff *) alloc_skb (AAL_MDU_MAX_PKT_SIZE,
    GFP_ATOMIC)) == NULL)
    {
    printk ("SKB Alloc Failed");
    return -1;
    }
    dev = (struct net_device *) dev_get_by_name (AAL_MDU_DEV_IF_NAME);
    if (!dev)
    {
    printk("Could Not find device Name %s\n", AAL_MDU_DEV_IF_NAME);
    return (-1);
    }
    pkt_len = sizeof(arp_frame);
    memcpy(ptr, arp_frame,pkt_len);
    skb->dev = dev;
    skb->data = ptr;
    type = (ptr) + 12;
    skb->len = pkt_len;
    skb->tail = skb->data + skb->len;
    skb->nh.raw = skb->data;
    skb->protocol = __constant_htons (type);

    for(count=0;count < 10; count ++) {
    status = netif_rx_ni(skb);
    if(status == NET_RX_DROP) {
    printk("Pkt Drop \r\n");
    return 0;
    }
    printk("pkt_sent \r\n");
    }
    return 0;
    }

    void cleanup_module(void)
    {
    printk("Goodbye world 1.\n");
    }

    Here is the above code !! Could anyone figureout why its not working
    if i send the ARP frame to the upper linux stack !!

    Thanks a lot .
    Kamaraj

  12. Re: send packet to Linux networking stack

    Kamaraj wrote in news:be0c5a25-a8f4-42d8-bc78-
    6f543639c90d@v15g2000hsa.googlegroups.com:
    > unsigned char arp_frame[] {

    [snip]
    > };
    >
    > int init_module(void)
    > {


    > ptr = &arp_frame;


    > if ((skb = (struct sk_buff *) alloc_skb (AAL_MDU_MAX_PKT_SIZE,
    > GFP_ATOMIC)) == NULL)


    > pkt_len = sizeof(arp_frame);
    > memcpy(ptr, arp_frame,pkt_len);


    You just set ptr to "&arp_frame" (same as "arp_frame" or "&arp_frame
    [0]"), so this memcpy is just copying arp_frame into arp_frame which is
    at best a waste of time.

    > skb->dev = dev;
    > skb->data = ptr;


    You should not be changing skb->data to point to your own memory. The
    point of alloc_skb is to allocate an sk_buff structure and associated
    packet buffer space -- that buffer space is recorded in skb->data.
    Rather than trying to change skb->data, you should be copying the
    arp_frame data into the buffer it points to. Then use skb_put(pkt_len)
    to update the len and tail members.

    > type = (ptr) + 12;

    ....
    > skb->protocol = __constant_htons (type);


    You're filling in protocol with (part of) a memory address -- probably
    not what you intended.

    > skb->nh.raw = skb->data;

    You don't need to set nh.raw -- the stack will figure that out for
    itself.

    A good reference is this snippet from drivers/net/pci-skeleton.c where
    it's delivering a packet to the stack:

    skb = dev_alloc_skb (pkt_size + 2);
    if (skb) {
    skb->dev = dev;
    skb_reserve (skb, 2); /* 16 byte align the IP fields. */

    eth_copy_and_sum (skb, &rx_ring[ring_offset + 4], pkt_size, 0);
    skb_put (skb, pkt_size);

    skb->protocol = eth_type_trans (skb, dev);
    netif_rx (skb);
    ....
    } else {
    ....

    GH

+ Reply to Thread