batman-adv: receive packets directly using skbs
PATCH 1/1 batman-adv: receive packets directly using skbs
This patch removes the (ugly and racy) packet receiving thread and the kernel socket usage. Instead, packets are received directly by registering the ethernet type and handling skbs instead of self-allocated buffers.
Some consequences and comments:
* we don't copy the payload data when forwarding/sending/receiving data anymore. This should boost performance.
* packets from/to different interfaces can be (theoretically) processed simultaneously. Only the big originator hash lock might be in the way.
* this might introduce new race conditions.
* aggregation and vis code still use packet buffers and are not (yet) converted.
This is the first version of this patch to be released, i would consider it experimental and would hereby like to as for reviews before committing it. Some things you might want to test:
* performace differences (are there any?)
* do all components still work? (vis, batctl ping, ...)
* do high load situations or multiple interfaces cause problems
* any memory leaks i might have overlooked?
I did some tests on my 9 node qemu environment and could confirm that usual sending/receiving, forwarding, vis, batctl ping etc works. However i can not talk about the performance from this setup.
---
batman-adv-kernelland/types.h +1 -1
@@ -39,7 +39,6 @@char if_active;char addr_str[ETH_STR_LEN ];struct net_device *net_dev; -struct socket *raw_sock;atomic_t seqno;unsigned char *packet_buff;int packet_len;
@@ -113,6 +112,7 @@struct hlist_node list;unsigned long send_time;uint8_t own; +struct sk_buff *skb;unsigned char *packet_buff;uint16_t packet_len;uint32_t direct_link_flags;
batman-adv-kernelland/send.c +49 -29
140 lines not on this page · raw message
batman-adv-kernelland/send.h +4 -1
@@ -22,6 +22,9 @@#include "types.h" void send_own_packet_work (struct work_struct *work); +int send_skb_packet (struct sk_buff *skb, +struct batman_if *batman_if, +uint8_t *dst_addr);void send_raw_packet (unsigned char *pack_buff,int pack_buff_len,struct batman_if *batman_if,uint8_t *dst_addr);void schedule_own_packet (struct batman_if *batman_if);
@@ -30,7 +33,7 @@struct batman_packet *batman_packet, uint8_t directlink, int hna_buff_len, struct batman_if *if_outgoing); -void add_bcast_packet_to_list (unsigned char *packet_buff,int packet_len); +void add_bcast_packet_to_list (struct sk_buff *skb);void send_outstanding_bcast_packet (struct work_struct *work);void send_outstanding_bat_packet (struct work_struct *work);void purge_outstanding_packets (void );
batman-adv-kernelland/soft-interface.c +47 -38
161 lines not on this page · raw message
batman-adv-kernelland/hard-interface.c +106 -42
193 lines not on this page · raw message
batman-adv-kernelland/soft-interface.h +1 -1
@@ -28,6 +28,6 @@int interface_set_mac_addr (struct net_device *dev,void *addr);int interface_change_mtu (struct net_device *dev,int new_mtu);int interface_tx (struct sk_buff *skb,struct net_device *dev); -void interface_rx (struct net_device *dev,void *packet,int packet_len); +void interface_rx (struct sk_buff *skb,int hdr_size);extern unsigned char mainIfAddr[];
batman-adv-kernelland/hard-interface.h +4 -0
@@ -32,5 +32,9 @@char hardif_get_active_if_num (void );void hardif_check_interfaces_status (void );void hardif_check_interfaces_status_wq (struct work_struct *work); +int batman_skb_recv (struct sk_buff *skb, +struct net_device *dev, +struct packet_type *ptype, +struct net_device *orig_dev);int hardif_min_mtu (void );void update_min_mtu (void );
batman-adv-kernelland/main.c +9 -20
@@ -50,11 +50,14 @@struct net_device *soft_device; -static struct task_struct *kthread_task; -unsigned char broadcastAddr[] = {0xff ,0xff ,0xff ,0xff ,0xff ,0xff };atomic_t module_state; +static struct packet_type batman_adv_packet_type__read_mostly = { + .type =cpu_to_be16 (ETH_P_BATMAN ), + .func = batman_skb_recv, +}; +struct workqueue_struct *bat_event_workqueue;#ifdef CONFIG_BATMAN_ADV_DEBUG
@@ -113,6 +116,7 @@ }register_netdevice_notifier (&hard_if_notifier); +dev_add_pack (&batman_adv_packet_type);printk (KERN_INFO "batman-adv:B.A.T.M.A.N. advanced %s%s (compatibility version %i) loaded \n" ,SOURCE_VERSION ,REVISION_VERSION_STR ,COMPAT_VERSION );
@@ -135,6 +139,8 @@ soft_device =NULL ; } +dev_remove_pack (&batman_adv_packet_type); +unregister_netdevice_notifier (&hard_if_notifier);cleanup_procfs ();
@@ -162,16 +168,6 @@if (vis_init () <1 ) goto err; -/* (re)start kernel thread for packet processing */ -if (!kthread_task) { - kthread_task =kthread_run (packet_recv_thread,NULL ,"batman-adv" ); - -if (IS_ERR (kthread_task)) { -printk (KERN_ERR "batman-adv:Unable to start packet receive thread\n" ); - kthread_task =NULL ; - } - } -update_min_mtu ();atomic_set (&module_state,MODULE_ACTIVE ); goto end;
@@ -193,15 +189,8 @@vis_quit (); -/* deactivate kernel thread for packet processing (if running) */ -if (kthread_task) { -atomic_set (&exit_cond,1 ); -wake_up_interruptible (&thread_wait); -kthread_stop (kthread_task); +/* TODO: unregister BATMAN pack */ - kthread_task =NULL ; - } -originator_free ();hna_local_free ();
batman-adv-kernelland/aggregation.c +1 -0
@@ -115,6 +115,7 @@ packet_buff, forw_packet_aggr->packet_len); + forw_packet_aggr->skb =NULL ; forw_packet_aggr->own = own_packet; forw_packet_aggr->if_incoming = if_incoming; forw_packet_aggr->num_packets =0 ;
batman-adv-kernelland/routing.c +135 -283
641 lines not on this page · raw message
batman-adv-kernelland/routing.h +8 -2
@@ -25,8 +25,6 @@extern atomic_t exit_cond;void slide_own_bcast_window (struct batman_if *batman_if); -void batman_data_ready (struct sock *sk,int len); -int packet_recv_thread (void *data); void receive_bat_packet(struct ethhdr *ethhdr, struct batman_packet *batman_packet, unsigned char *hna_buff, int hna_buff_len,
@@ -34,3 +32,11 @@void update_routes (struct orig_node *orig_node,struct neigh_node *neigh_node,unsigned char *hna_buff,int hna_buff_len); +int recv_icmp_packet (struct sk_buff *skb); +int recv_unicast_packet (struct sk_buff *skb); +int recv_bcast_packet (struct sk_buff *skb); +int recv_vis_packet (struct sk_buff *skb); +int recv_bat_packet (struct sk_buff *skb, +struct batman_if *batman_if); + +
Quoted 8 lines · Simon Wunderlich
batman-adv-kernelland/hard-interface.c +8
-0+/* receive a packet with the batman ethertype coming on a hard +* interface */ +int batman_skb_recv (struct sk_buff *skb,struct net_device *dev, +struct packet_type *ptype,struct net_device *orig_dev) +{ +struct batman_packet *batman_packet; +struct batman_if *batman_if; +struct net_device_stats *stats;
batman-adv-kernelland/hard-interface.c line 438
Quoted 10 lines
batman-adv-kernelland/hard-interface.c +10
-0+int ret; + + skb =skb_share_check (skb,GFP_ATOMIC ); + +if (skb ==NULL ) + goto err_free; + +/* packet should hold at least type and version */ +if (unlikely (skb->len <2 )) + goto err_free;
Len field includes the length of the data in the main socket buffer and possible fragments attached to the main socket buffer. This length is changed when you move from layer to layer. So it is ok to test for 2 here to check if it is enough data for version and type.
Quoted 13 lines
batman-adv-kernelland/hard-interface.c +13
-0+ + batman_if =find_batman_if (skb->dev); +if (!batman_if) + goto err_free; + + stats = &skb->dev->stats; + stats->rx_packets++; + stats->rx_bytes += skb->len; + + +/* push back, we want to look at the ethernet header as well. */ +skb_push (skb,sizeof (struct ethhdr )); +skb_reset_mac_header (skb);
batman-adv-kernelland/hard-interface.c line 461
Quoted 10 lines
batman-adv-kernelland/hard-interface.c +10
-0+ + batman_packet = (struct batman_packet *)(skb->data + +sizeof (struct ethhdr )); + +if (batman_packet->version !=COMPAT_VERSION ) { +bat_dbg (DBG_BATMAN , +"Drop packet: incompatible batman version (%i)\n" , + batman_packet->version); + goto err_free; + }
Here you try to to access data which may be fragmented over different buffers. This could for example happen if we send our data over a layer which fragments it.
In the "normal" scenario of sending it directly over ethernet this shouldn't happen, but that doesn't mean that nobody wants to do something "special".
There are different other positions like recv_bat_packet where this is ignored.
Maybe someone knows why this could never happen, but everything I have read was more like "you have to deal with it".
Quoted 61 lines
batman-adv-kernelland/routing.c +18
-16-static void recv_icmp_packet (struct ethhdr *ethhdr, -unsigned char *packet_buff, -int result, -struct batman_if *batman_if) +int recv_icmp_packet (struct sk_buff *skb) {struct icmp_packet *icmp_packet; +struct ethhdr *ethhdr;struct orig_node *orig_node; +int hdr_size =sizeof (struct icmp_packet ) +sizeof (struct ethhdr ); +int ret; +/* drop packet if it has not necessary minimum size */ +if (skb->len < hdr_size) +return NET_RX_DROP ; + + ethhdr = (struct ethhdr *)skb_mac_header (skb); +/* packet with unicast indication but broadcast recipient */ if (is_bcast (ethhdr->h_dest)) -return ; +return NET_RX_DROP ;/* packet with broadcast sender address */ if (is_bcast (ethhdr->h_source)) -return ; +return NET_RX_DROP ;/* not for me */ if (!is_my_mac (ethhdr->h_dest)) -return ; +return NET_RX_DROP ; -/* drop packet if it has not necessary minimum size */ -if (result <sizeof (struct ethhdr ) +sizeof (struct icmp_packet )) -return ; - icmp_packet = (struct icmp_packet *) - (packet_buff +sizeof (struct ethhdr )); + (skb->data +sizeof (struct ethhdr ));/* packet for me */ if (is_my_mac (icmp_packet->dst)) -recv_my_icmp_packet (ethhdr, icmp_packet, packet_buff, result); +return recv_my_icmp_packet (skb);/* TTL exceeded */ if (icmp_packet->ttl <2 ) { -recv_icmp_ttl_exceeded (icmp_packet, ethhdr, packet_buff, result, - batman_if); -return ; +return recv_icmp_ttl_exceeded (skb); } + ret =NET_RX_DROP ; +/* get routing information */ spin_lock (&orig_hash_lock); orig_node = ((struct orig_node *)
batman-adv-kernelland/routing.c line 721
Quoted 1 line
batman-adv-kernelland/routing.c +0
-0@@ -725,49 +721,51 @@ icmp_packet->ttl--;
You are modify the data inside the socket buffer (not the control structure but the real data), but I cannot see where you created a private copy of it. Data of cloned copies is not allowed to be changed. You must use (p)skb_copy (it clones the the data too and not only the sk_buff structure).
This is explained in "Understanding Linux Network Internals", "Christian Benvenuti", "December 2005, O'Reilly", "2.1.5.5. Cloning and copying buffers"
Best regards,
Sven
On 12/23/2009 02:20 AM, Sven Eckelmann wrote:
[snip]
Quoted 2 lines
This is explained in "Understanding Linux Network Internals", "Christian Benvenuti", "December 2005, O'Reilly", "2.1.5.5. Cloning and copying buffers"
Where did the 2.1.5.5 come from? My real paper book only has a Chapter 2, page 39 way of referencing this. Do you have an electronic version?
Gus
Quoted 6 lines · Gus Wirth
On 12/23/2009 02:20 AM, Sven Eckelmann wrote:
[snip]Quoted 2 lines
This is explained in "Understanding Linux Network Internals", "Christian Benvenuti", "December 2005, O'Reilly", "2.1.5.5. Cloning and copying buffers"
Where did the 2.1.5.5 come from?
Second chapter, first section, fifth subsection, fifth subsubsection. Simple counting :)
But yes, some eBooks from oreilly seem to have that numbers. Cannot tell for sure if they are the same books you can download from them, but my library provides them that way. Publisher for that ebook is Safari Books.
http://proquest.safaribooksonline.com/0596002556
Just noticed that the safari books image view (aka buggy flash viewer) also doesn't display the subsection numbers - but the HTML view does.
Best regards,
Sven