Skip to contentSkip to conversation

CATWOMAN: Network coding in batman-adv

RFC In review Martin Hundebøll 3 participants 12 messages Nov 19, 2012 – Nov 27, 2012 Kbuild

lore
Share & tools
lore
#0 lore raw

cover CATWOMAN: Network coding in batman-adv

I hereby bring you network coding in batman-adv. This allows a relay to send two packets (to two destinations) in a single transmission. Tests[1] have shown an increase in throughput up to 1.6 under the right circumstances.

For this to work, you will need to compile batman-adv with CONFIG_BATMAN_ADV_NC=y and a wireless interface with working promiscuous mode. The rest is taken care of by batman-adv and the following patches.

[1] http://www.open-mesh.org/projects/open-mesh/wiki/2011-08-18-network-coding-first-steps

Martin Hundebøll (6):
batman-adv: Add the initial code for network coding.
batman-adv: Detect coding nodes and remove these after timeout
batman-adv: Buffer unicast packets before forward.
batman-adv: Code and transmit packets if possible.
batman-adv: Save overheard and tx packets for decoding.
batman-adv: Receive coded packets and decode them.

 Makefile               |    2 +
 Makefile.kbuild        |    1 +
 bat_iv_ogm.c           |    5 +
 compat.c               |   10 +
 compat.h               |    1 +
 debugfs.c              |   18 +
 gen-compat-autoconf.sh |    1 +
 main.c                 |    6 +
 main.h                 |   13 +-
 network-coding.c       | 1818 ++++++++++++++++++++++++++++++++++++++++++++++++
 network-coding.h       |  122 ++++
 originator.c           |    6 +
 packet.h               |   33 +
 routing.c              |   26 +-
 send.c                 |    5 +
 soft-interface.c       |   14 +
 sysfs-class-net-mesh   |    8 +
 sysfs.c                |    6 +
 types.h                |  123 ++++
 19 files changed, 2209 insertions(+), 9 deletions(-)
 create mode 100644 network-coding.c
 create mode 100644 network-coding.h
Signature · 1 line
1.8.0
#1 lore raw

RFC 1/6 batman-adv: Add the initial code for network coding.

Network coding exploits the 802.11 shared medium to allow multiple packets to be send in a single transmission. In brief, a relay can XOR two packets, and send the coded packet to two destinations. The receivers can decode one of the original packets by XOR'ing the coded packet with the other original packet. This will lead to increased throughput in topologies where two packets cross one relay.

In a simple topology with three nodes, it takes four transmissions without network coding to get one packet from Node A to Node B and one from Node B to Node A:

 1.  Node A  ---- p1 --->  Node R                Node B
 2.  Node A                Node R  <--- p2 ----  Node B
 3.  Node A  <--- p2 ----  Node R                Node B
 4.  Node A                Node R  ---- p1 --->  Node B

With network coding, the relay only needs one transmission, which saves us one slot of valuable airtime:

 1.  Node A  ---- p1 --->  Node R                Node B
 2.  Node A                Node R  <--- p2 ----  Node B
 3.  Node A  <- p1 x p2 -  Node R  - p1 x p2 ->  Node B

The same principle holds for a topology including five nodes. Here the packets from Node A and Node B are overheard by Node C and Node D, respectively. This allows Node R to send a network coded packet to save one transmission:

Node A Node B

    |     \              /    |
    |      p1          p2     |
    |       \          /      |
    p1       > Node R <       p2
    |                         |
    |         /      \        |
    |    p1 x p2    p1 x p2   |
    v       /          \      v
           /            \
   Node C <              > Node D

More information is available on the open-mesh.org wiki[1].

This patch adds the initial code to support network coding in batman-adv. It sets up a worker thread to do house keeping and adds a sysfs file to enable/disable network coding. The feature is disabled by default, as it requires a wifi-driver with working promiscuous mode, and also because it adds a small delay at each hop.

[1] http://www.open-mesh.org/projects/batman-adv/wiki/Catwoman

---
 Makefile               |  2 ++
 Makefile.kbuild        |  1 +
 gen-compat-autoconf.sh |  1 +
 main.c                 |  6 ++++
 main.h                 |  4 ++-
 network-coding.c       | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++
 network-coding.h       | 47 +++++++++++++++++++++++++++++
 soft-interface.c       |  3 ++
 sysfs-class-net-mesh   |  8 +++++
 sysfs.c                |  6 ++++
 types.h                | 17 +++++++++++
 11 files changed, 176 insertions(+), 1 deletion(-)
 create mode 100644 network-coding.c
 create mode 100644 network-coding.h
Makefile +2 -0
index 4d70e40..d41f947 100644
@@ -25,6 +25,8 @@ export CONFIG_BATMAN_ADV_DEBUG=n
 export CONFIG_BATMAN_ADV_BLA=y
 # B.A.T.M.A.N. distributed ARP table:
 export CONFIG_BATMAN_ADV_DAT=y
+# B.A.T.M.A.N network coding (catwoman):
+export CONFIG_BATMAN_ADV_NC=n
 
 PWD:=$(shell pwd)
 KERNELPATH ?= /lib/modules/$(shell uname -r)/build
Makefile.kbuild +1 -0
index e45e3b4..4b8f192 100644
@@ -30,6 +30,7 @@ batman-adv-y += hard-interface.o
 batman-adv-y += hash.o
 batman-adv-y += icmp_socket.o
 batman-adv-y += main.o
+batman-adv-$(CONFIG_BATMAN_ADV_NC) += network-coding.o
 batman-adv-y += originator.o
 batman-adv-y += ring_buffer.o
 batman-adv-y += routing.o
gen-compat-autoconf.sh +1 -0
index 7ea42aa..78573e4 100755
@@ -39,6 +39,7 @@ gen_config() {
 gen_config 'CONFIG_BATMAN_ADV_DEBUG' ${CONFIG_BATMAN_ADV_DEBUG:="n"} >> "${TMP}"
 gen_config 'CONFIG_BATMAN_ADV_BLA' ${CONFIG_BATMAN_ADV_BLA:="y"} >> "${TMP}"
 gen_config 'CONFIG_BATMAN_ADV_DAT' ${CONFIG_BATMAN_ADV_DAT:="y"} >> "${TMP}"
+gen_config 'CONFIG_BATMAN_ADV_NC' ${CONFIG_BATMAN_ADV_NC:="n"} >> "${TMP}"
 
 # only regenerate compat-autoconf.h when config was changed
 diff "${TMP}" "${TARGET}" > /dev/null 2>&1 || cp "${TMP}" "${TARGET}"
main.c +6 -0
index 253e240..7971214 100644
@@ -35,6 +35,7 @@
 #include "vis.h"
 #include "hash.h"
 #include "bat_algo.h"
+#include "network-coding.h"
 
 
 /* List manipulations on hardif_list have to be rtnl_lock()'ed,
@@ -135,6 +136,10 @@ int batadv_mesh_init(struct net_device *soft_iface)
 	if (ret < 0)
 		goto err;
 
+	ret = batadv_network_coding_init(bat_priv);
+	if (ret < 0)
+		goto err;
+
 	atomic_set(&bat_priv->gw.reselect, 0);
 	atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
 
@@ -157,6 +162,7 @@ void batadv_mesh_free(struct net_device *soft_iface)
 
 	batadv_gw_node_purge(bat_priv);
 	batadv_originator_free(bat_priv);
+	batadv_network_coding_free(bat_priv);
 
 	batadv_tt_free(bat_priv);
 
main.h +3 -1
index bfae503..1b0cd41 100644
@@ -186,6 +186,7 @@ __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr);
  * @BATADV_DBG_TT: translation table messages
  * @BATADV_DBG_BLA: bridge loop avoidance messages
  * @BATADV_DBG_DAT: ARP snooping and DAT related messages
+ * @BATADV_DBG_NC: network coding related messages
  * @BATADV_DBG_ALL: the union of all the above log levels
  */
 enum batadv_dbg_level {
@@ -194,7 +195,8 @@ enum batadv_dbg_level {
 	BATADV_DBG_TT	  = BIT(2),
 	BATADV_DBG_BLA    = BIT(3),
 	BATADV_DBG_DAT    = BIT(4),
-	BATADV_DBG_ALL    = 31,
+	BATADV_DBG_NC	  = BIT(5),
+	BATADV_DBG_ALL    = 63,
 };
 
 #ifdef CONFIG_BATMAN_ADV_DEBUG
network-coding.c new file +82 -0

82 lines not on this page · raw message

network-coding.h new file +47 -0

47 lines not on this page · raw message

soft-interface.c +3 -0
index 8000639..8733558 100644
@@ -36,6 +36,7 @@
 #include <linux/if_vlan.h>
 #include "unicast.h"
 #include "bridge_loop_avoidance.h"
+#include "network-coding.h"
 
 
 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
@@ -514,6 +515,8 @@ struct net_device *batadv_softif_create(const char *name)
 	if (ret < 0)
 		goto unreg_soft_iface;
 
+	batadv_nc_init_bat_priv(bat_priv);
+
 	ret = batadv_sysfs_add_meshif(soft_iface);
 	if (ret < 0)
 		goto unreg_soft_iface;
sysfs-class-net-mesh +8 -0
index bc41da6..c75e594 100644
@@ -74,6 +74,14 @@ Description:
                 Defines the interval in milliseconds in which batman
                 sends its protocol messages.
 
+What:           /sys/class/net/<mesh_iface>/mesh/network_coding
+Date:           Nov 2012
+Contact:        Martin Hundeboll <[email protected]>
+Description:
+                Controls whether Network Coding (using some magic
+                to send fewer wifi packets but still the same
+                content) is enabled or not.
+
 What:           /sys/class/net/<mesh_iface>/mesh/routing_algo
 Date:           Dec 2011
 Contact:        Marek Lindner <[email protected]>
sysfs.c +6 -0
index 84a55cb..e48a62b 100644
@@ -442,6 +442,9 @@ static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
 #ifdef CONFIG_BATMAN_ADV_DEBUG
 BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
 #endif
+#ifdef CONFIG_BATMAN_ADV_NC
+BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR, NULL);
+#endif
 
 static struct batadv_attribute *batadv_mesh_attrs[] = {
 	&batadv_attr_aggregated_ogms,
@@ -464,6 +467,9 @@ static struct batadv_attribute *batadv_mesh_attrs[] = {
 #ifdef CONFIG_BATMAN_ADV_DEBUG
 	&batadv_attr_log_level,
 #endif
+#ifdef CONFIG_BATMAN_ADV_NC
+	&batadv_attr_network_coding,
+#endif
 	NULL,
 };
 
types.h +17 -0
index ae9ac9a..5451fc8 100644
@@ -265,6 +265,19 @@ struct batadv_priv_dat {
 };
 #endif
 
+/**
+ * struct batadv_priv_nc - per mesh interface network coding private data
+ * @work: work queue callback item for cleanup
+ */
+struct batadv_priv_nc {
+	struct delayed_work work;
+};
+
+/**
+ * struct batadv_priv - per mesh interface private data
+ * @network_coding: enable/disable network coding
+ * @batadv_priv_nc: network coding private data
+ */
 struct batadv_priv {
 	atomic_t mesh_state;
 	struct net_device_stats stats;
@@ -308,6 +321,10 @@ struct batadv_priv {
 #ifdef CONFIG_BATMAN_ADV_DAT
 	struct batadv_priv_dat dat;
 #endif
+#ifdef CONFIG_BATMAN_ADV_NC
+	atomic_t network_coding; /* boolean */
+	struct batadv_priv_nc nc;
+#endif /* CONFIG_BATMAN_ADV_NC */
 };
 
 struct batadv_socket_client {
Signature · 1 line
1.8.0
#4 lore raw

RFC 2/6 batman-adv: Detect coding nodes and remove these after timeout

In reply to Martin Hundebøll

To use network coding efficiently, a relay must know when neighbor nodes are likely to have enough information to be able to decode a network coded packet. This is detected by using OGMs from batman-adv to discover when one neighbor is in range of another neighbor. The relay check the TLL to detect when an OGM is forwarded from one neighbor by another neighbor, and thereby knows that the two neighbors are in range and thus overhear packets sent by each other.

This information is saved in the orig_node struct to be used when searching for coding opportunities. Two lists are added to the orig_node struct: One for neighbors that can hear the orig_node (outgoing nc_nodes) and one for neighbors that the orig_node can hear (incoming nc_nodes).

Information about nc_nodes is kept for 10 seconds and is available through debugfs in batman_adv/nc_nodes.

---
 bat_iv_ogm.c     |   5 +
 debugfs.c        |  18 +++
 main.h           |   2 +
 network-coding.c | 416 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 network-coding.h |  47 +++++++
 originator.c     |   6 +
 types.h          |  27 ++++
 7 files changed, 521 insertions(+)
bat_iv_ogm.c +5 -0
index 662743e..4431fd2 100644
@@ -27,6 +27,7 @@
 #include "hard-interface.h"
 #include "send.h"
 #include "bat_algo.h"
+#include "network-coding.h"
 
 static struct batadv_neigh_node *
 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
@@ -1194,6 +1195,10 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
 	if (!orig_neigh_node)
 		goto out;
 
+	/* Update nc_nodes of the originator */
+	batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
+				 batadv_ogm_packet, is_single_hop_neigh);
+
 	orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
 
 	/* drop packet if sender is not a direct neighbor and if we
debugfs.c +18 -0
index ae79b19..7876940 100644
@@ -32,6 +32,7 @@
 #include "icmp_socket.h"
 #include "bridge_loop_avoidance.h"
 #include "distributed-arp-table.h"
+#include "network-coding.h"
 
 static struct dentry *batadv_debugfs;
 
@@ -321,6 +322,14 @@ struct batadv_debuginfo {
 	const struct file_operations fops;
 };
 
+#ifdef CONFIG_BATMAN_ADV_NC
+static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
+{
+	struct net_device *net_dev = (struct net_device *)inode->i_private;
+	return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
+}
+#endif
+
 #define BATADV_DEBUGINFO(_name, _mode, _open)		\
 struct batadv_debuginfo batadv_debuginfo_##_name = {	\
 	.attr = { .name = __stringify(_name),		\
@@ -361,6 +370,9 @@ static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
 static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
 			batadv_transtable_local_open);
 static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
+#ifdef CONFIG_BATMAN_ADV_NC
+static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open);
+#endif
 
 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
 	&batadv_debuginfo_originators,
@@ -375,6 +387,9 @@ static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
 #endif
 	&batadv_debuginfo_transtable_local,
 	&batadv_debuginfo_vis_data,
+#ifdef CONFIG_BATMAN_ADV_NC
+	&batadv_debuginfo_nc_nodes,
+#endif
 	NULL,
 };
 
@@ -446,6 +461,9 @@ int batadv_debugfs_add_meshif(struct net_device *dev)
 		}
 	}
 
+	if (batadv_nc_init_debugfs(bat_priv) < 0)
+		goto rem_attr;
+
 	return 0;
 rem_attr:
 	debugfs_remove_recursive(bat_priv->debug_dir);
main.h +2 -0
index 1b0cd41..bc05146 100644
@@ -103,6 +103,8 @@
 #define BATADV_RESET_PROTECTION_MS 30000
 #define BATADV_EXPECTED_SEQNO_RANGE	65536
 
+#define BATADV_NC_NODE_TIMEOUT 10000 /* Milliseconds */
+
 enum batadv_mesh_state {
 	BATADV_MESH_INACTIVE,
 	BATADV_MESH_ACTIVE,
network-coding.c +416 -0

444 lines not on this page · raw message

network-coding.h +47 -0

65 lines not on this page · raw message

originator.c +6 -0
index 8c32cf1..5d56ce6 100644
@@ -28,6 +28,7 @@
 #include "unicast.h"
 #include "soft-interface.h"
 #include "bridge_loop_avoidance.h"
+#include "network-coding.h"
 
 static void batadv_purge_orig(struct work_struct *work);
 
@@ -139,6 +140,9 @@ static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
 
 	spin_unlock_bh(&orig_node->neigh_list_lock);
 
+	/* Free nc_nodes */
+	batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
+
 	batadv_frag_list_free(&orig_node->frag_list);
 	batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
 				  "originator timed out");
@@ -217,6 +221,8 @@ struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
 	spin_lock_init(&orig_node->neigh_list_lock);
 	spin_lock_init(&orig_node->tt_buff_lock);
 
+	batadv_nc_init_orig(orig_node);
+
 	/* extra reference for return */
 	atomic_set(&orig_node->refcount, 2);
 
types.h +27 -0
index 5451fc8..beb84f4 100644
@@ -122,6 +122,12 @@ struct batadv_orig_node {
 	spinlock_t tt_list_lock; /* protects tt_list */
 	atomic_t bond_candidates;
 	struct list_head bond_list;
+#ifdef CONFIG_BATMAN_ADV_NC
+	struct list_head in_coding_list;
+	struct list_head out_coding_list;
+	spinlock_t in_coding_list_lock; /* Protects in_coding_list */
+	spinlock_t out_coding_list_lock; /* Protects out_coding_list */
+#endif
 };
 
 struct batadv_gw_node {
@@ -268,9 +274,12 @@ struct batadv_priv_dat {
 /**
  * struct batadv_priv_nc - per mesh interface network coding private data
  * @work: work queue callback item for cleanup
+ * @min_tq: only consider neighbors for encoding if neigh_tq > min_tq
  */
 struct batadv_priv_nc {
 	struct delayed_work work;
+	struct dentry *debug_dir;
+	u8 min_tq;
 };
 
 /**
@@ -414,6 +423,24 @@ struct batadv_tt_roam_node {
 	struct list_head list;
 };
 
+/**
+ * struct batadv_nc_node - network coding node
+ * @list: next and prev pointer for the list handling
+ * @addr: the node's mac address
+ * @refcount: number of contexts the object is used by
+ * @rcu: struct used for freeing in an RCU-safe manner
+ * @orig_node: pointer to corresponding orig node struct
+ * @last_seen: timestamp of last ogm received from this node
+ */
+struct batadv_nc_node {
+	struct list_head list;
+	uint8_t addr[ETH_ALEN];
+	atomic_t refcount;
+	struct rcu_head rcu;
+	struct batadv_orig_node *orig_node;
+	unsigned long last_seen;
+};
+
 /*	forw_packet - structure for forw_list maintaining packets to be
  *	              send/forwarded
  */
Signature · 1 line
1.8.0
#8 lore raw

RFC 3/6 batman-adv: Buffer unicast packets before forward.

In reply to Martin Hundebøll

Two be able to network code two packets, one packet must be buffered until the next is available. This is done in a "coding buffer", which is essentially a hash table with lists of packets. Each entry in the hash table corresponds to a specific src-dst pair, which has a linked list of packets that are buffered.

This patch adds skbs to the buffer just before forwarding them. The buffer is traversed every 10 ms, where timed skbs are removed from the buffer and transmitted.

---
 compat.c         |  10 ++
 compat.h         |   1 +
 main.h           |   1 +
 network-coding.c | 493 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 network-coding.h |  10 ++
 routing.c        |  18 +-
 types.h          |  49 ++++++
 7 files changed, 574 insertions(+), 8 deletions(-)
compat.c +10 -0
index c8006c6..5664d90 100644
@@ -74,4 +74,14 @@ void batadv_free_rcu_dat_entry(struct rcu_head *rcu)
 }
 #endif
 
+#ifdef CONFIG_BATMAN_ADV_NC
+void batadv_free_rcu_nc_path(struct rcu_head *rcu)
+{
+	struct batadv_nc_path *nc_path;
+
+	nc_path = container_of(rcu, struct batadv_nc_path, rcu);
+	kfree(nc_path);
+}
+#endif
+
 #endif /* < KERNEL_VERSION(3, 0, 0) */
compat.h +1 -0
index 8780b43..11cab85 100644
@@ -145,6 +145,7 @@ void batadv_free_rcu_neigh_node(struct rcu_head *rcu);
 void batadv_free_rcu_tt_local_entry(struct rcu_head *rcu);
 void batadv_free_rcu_backbone_gw(struct rcu_head *rcu);
 void batadv_free_rcu_dat_entry(struct rcu_head *rcu);
+void batadv_free_rcu_nc_path(struct rcu_head *rcu);
 
 static inline void skb_reset_mac_len(struct sk_buff *skb)
 {
main.h +1 -0
index bc05146..8d2ea26 100644
@@ -104,6 +104,7 @@
 #define BATADV_EXPECTED_SEQNO_RANGE	65536
 
 #define BATADV_NC_NODE_TIMEOUT 10000 /* Milliseconds */
+#define BATADV_NC_PATH_TIMEOUT 10000 /* Milliseconds */
 
 enum batadv_mesh_state {
 	BATADV_MESH_INACTIVE,
network-coding.c +492 -1

565 lines not on this page · raw message

network-coding.h +10 -0
index 6d82079..48c1219 100644
@@ -34,6 +34,9 @@ void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
 					   struct batadv_nc_node *));
 void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv);
 void batadv_nc_init_orig(struct batadv_orig_node *orig_node);
+bool batadv_nc_skb_forward(struct sk_buff *skb,
+			   struct batadv_neigh_node *neigh_node,
+			   struct ethhdr *ethhdr);
 int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset);
 int batadv_nc_init_debugfs(struct batadv_priv *bat_priv);
 
@@ -78,6 +81,13 @@ static inline void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
 	return;
 }
 
+static inline bool batadv_nc_skb_forward(struct sk_buff *skb,
+					 struct batadv_neigh_node *neigh_node,
+					 struct ethhdr *ethhdr)
+{
+	return false;
+}
+
 static inline int batadv_nc_nodes_seq_print_text(struct seq_file *seq,
 						 void *offset)
 {
routing.c +11 -7
index 1aa1722..d7bd4d1 100644
@@ -29,6 +29,7 @@
 #include "unicast.h"
 #include "bridge_loop_avoidance.h"
 #include "distributed-arp-table.h"
+#include "network-coding.h"
 
 static int batadv_route_unicast_packet(struct sk_buff *skb,
 				       struct batadv_hard_iface *recv_if);
@@ -855,14 +856,17 @@ static int batadv_route_unicast_packet(struct sk_buff *skb,
 	/* decrement ttl */
 	unicast_packet->header.ttl--;
 
-	/* Update stats counter */
-	batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
-	batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
-			   skb->len + ETH_HLEN);
-
-	/* route it */
-	if (batadv_send_skb_to_orig(skb, orig_node, recv_if))
+	/* network code packet if possible */
+	if (batadv_nc_skb_forward(skb, neigh_node, ethhdr)) {
 		ret = NET_RX_SUCCESS;
+	} else if (batadv_send_skb_to_orig(skb, orig_node, recv_if)) {
+		ret = NET_RX_SUCCESS;
+
+		/* Update stats counter */
+		batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
+		batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
+				   skb->len + ETH_HLEN);
+	}
 
 out:
 	if (neigh_node)
types.h +49 -0

66 lines not on this page · raw message

Signature · 1 line
1.8.0
#9 lore raw

RFC 4/6 batman-adv: Code and transmit packets if possible.

In reply to Martin Hundebøll

Before adding forward-skbs to the coding buffer, the buffer is searched for a potential coding opportunity. If one is found, the two packets are network coded and transmitted right away. If not, the forward-skb is added to the buffer.

Network coded packets are transmitted with information about the two receivers and the two coded packets. The first receiver is given by the MAC header, while the second is given in the payload/bat-header. The second receiver uses promiscuous mode to receive the packet and check the second destination.

---
 main.h           |   6 +
 network-coding.c | 394 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 packet.h         |  33 +++++
 soft-interface.c |   6 +
 types.h          |  16 +++
 5 files changed, 455 insertions(+)
main.h +6 -0
index 8d2ea26..9c123af 100644
@@ -304,4 +304,10 @@ static inline uint64_t batadv_sum_counter(struct batadv_priv *bat_priv,
 	return sum;
 }
 
+/* Define a macro to reach the control buffer of the skb. The members of the
+ * control buffer are defined in struct bat_skb_cb in types.h.
+ * The macro is inspired by the similar macro TCP_SKB_CB() in tcp.h.
+ */
+#define BATADV_SKB_CB(__skb)       ((struct batadv_skb_cb *)&((__skb)->cb[0]))
+
 #endif /* _NET_BATMAN_ADV_MAIN_H_ */
network-coding.c +394 -0

406 lines not on this page · raw message

packet.h +33 -0
index 1c5454d..9f633cf 100644
@@ -32,6 +32,7 @@ enum batadv_packettype {
 	BATADV_TT_QUERY		= 0x07,
 	BATADV_ROAM_ADV		= 0x08,
 	BATADV_UNICAST_4ADDR	= 0x09,
+	BATADV_CODED		= 0x0a,
 };
 
 /**
@@ -280,4 +281,36 @@ struct batadv_tt_change {
 	uint8_t addr[ETH_ALEN];
 } __packed;
 
+/**
+ * struct batadv_coded_packet - network coded packet
+ * @header: common batman packet header and ttl of first included packet
+ * @reserved: Align following fields to 2-byte boundaries
+ * @first_source: original source of first included packet
+ * @first_orig_dest: original destinal of first included packet
+ * @first_crc: checksum of first included packet
+ * @first_ttvn: tt-version number of first included packet
+ * @second_ttl: ttl of second packet
+ * @second_dest: second receiver of this coded packet
+ * @second_source: original source of second included packet
+ * @second_orig_dest: original destination of second included packet
+ * @second_crc: checksum of second included packet
+ * @second_ttvn: tt version number of second included packet
+ * @coded_len: length of network coded part of the payload
+ */
+struct batadv_coded_packet {
+	struct batadv_header header;
+	uint8_t  first_ttvn;
+	/* uint8_t  first_dest[ETH_ALEN]; - saved in mac header destination */
+	uint8_t  first_source[ETH_ALEN];
+	uint8_t  first_orig_dest[ETH_ALEN];
+	__be32   first_crc;
+	uint8_t  second_ttl;
+	uint8_t  second_ttvn;
+	uint8_t  second_dest[ETH_ALEN];
+	uint8_t  second_source[ETH_ALEN];
+	uint8_t  second_orig_dest[ETH_ALEN];
+	__be32   second_crc;
+	uint16_t coded_len;
+};
+
 #endif /* _NET_BATMAN_ADV_PACKET_H_ */
soft-interface.c +6 -0
index 8733558..e29cce0 100644
@@ -635,6 +635,12 @@ static const struct {
 	{ "dat_put_rx" },
 	{ "dat_cached_reply_tx" },
 #endif
+#ifdef CONFIG_BATMAN_ADV_NC
+	{ "nc_code" },
+	{ "nc_code_bytes" },
+	{ "nc_recode" },
+	{ "nc_recode_bytes" },
+#endif
 };
 
 static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
types.h +16 -0
index b34fde7..a44e948 100644
@@ -192,6 +192,12 @@ enum batadv_counters {
 	BATADV_CNT_DAT_PUT_RX,
 	BATADV_CNT_DAT_CACHED_REPLY_TX,
 #endif
+#ifdef CONFIG_BATMAN_ADV_NC
+	BATADV_CNT_NC_CODE,
+	BATADV_CNT_NC_CODE_BYTES,
+	BATADV_CNT_NC_RECODE,
+	BATADV_CNT_NC_RECODE_BYTES,
+#endif
 	BATADV_CNT_NUM,
 };
 
@@ -490,6 +496,16 @@ struct batadv_nc_packet {
 	struct batadv_nc_path *nc_path;
 };
 
+/**
+ * batadv_skb_cb - control buffer structure used to store private data relevant
+ *  to batman-adv in the skb->cb buffer in skbs.
+ * @decoded: Marks a skb as decoded, which is checked when searching for coding
+ *  opportunities in network-coding.c
+ */
+struct batadv_skb_cb {
+	bool decoded;
+};
+
 /*	forw_packet - structure for forw_list maintaining packets to be
  *	              send/forwarded
  */
Signature · 1 line
1.8.0
#10 lore raw

RFC 5/6 batman-adv: Save overheard and tx packets for decoding.

In reply to Martin Hundebøll

To be able to decode a network coded packet, a node must already know one of the two coded packets. This is done by buffering skbs before transmission and buffering packets sniffed with promiscuous mode from other hosts.

Packets are kept in a buffer similar to the one with forward-skbs: A hash table, where each entry, which corresponds to a src-dst pair, has a linked list packets.

---
 network-coding.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 network-coding.h |  18 +++++
 routing.c        |   8 ++-
 send.c           |   5 ++
 soft-interface.c |   1 +
 types.h          |  10 +++
 6 files changed, 247 insertions(+), 2 deletions(-)
network-coding.c +206 -1
index 2524934..1edd2d4 100644
@@ -27,6 +27,7 @@
 #include "hard-interface.h"
 
 static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
+static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
 
 static void batadv_nc_worker(struct work_struct *work);
 
@@ -49,8 +50,9 @@ static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
 int batadv_network_coding_init(struct batadv_priv *bat_priv)
 {
 	bat_priv->nc.timestamp_fwd_flush = jiffies;
+	bat_priv->nc.timestamp_sniffed_purge = jiffies;
 
-	if (bat_priv->nc.coding_hash)
+	if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
 		return 0;
 
 	bat_priv->nc.coding_hash = batadv_hash_new(128);
@@ -60,6 +62,13 @@ int batadv_network_coding_init(struct batadv_priv *bat_priv)
 	batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
 				   &batadv_nc_coding_hash_lock_class_key);
 
+	bat_priv->nc.decoding_hash = batadv_hash_new(128);
+	if (!bat_priv->nc.decoding_hash)
+		goto err;
+
+	batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
+				   &batadv_nc_decoding_hash_lock_class_key);
+
 	batadv_nc_start_timer(bat_priv);
 	return 0;
 
@@ -76,6 +85,7 @@ void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
 	atomic_set(&bat_priv->network_coding, 1);
 	bat_priv->nc.min_tq = 200;
 	bat_priv->nc.max_fwd_delay = 10;
+	bat_priv->nc.max_buffer_time = 200;
 }
 
 /**
@@ -176,6 +186,26 @@ static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
 }
 
 /**
+ * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
+ * @bat_priv: the bat priv with all the soft interface information
+ * @nc_path: the nc path to check
+ *
+ * Returns true if the entry has to be purged now, false otherwise
+ */
+static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
+						struct batadv_nc_path *nc_path)
+{
+	if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
+		return true;
+
+	/* purge the path when no packets has been added for 10 times the
+	 * max_buffer time
+	 */
+	return batadv_has_timed_out(nc_path->last_valid,
+				    bat_priv->nc.max_buffer_time*10);
+}
+
+/**
  * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
  *  entries
  * @bat_priv: the bat priv with all the soft interface information
@@ -444,6 +474,42 @@ static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
 }
 
 /**
+ * batadv_nc_sniffed_purge - Checks whether the given sniffed (overheard) nc
+ *  packet has hit its buffering timeout. If so, the packet is no longer kept
+ *  and the entry deleted from the queue. Has to be called with the appropriate
+ *  locks.
+ * @bat_priv: the bat priv with all the soft interface information
+ * @nc_path: the nc path the packet belongs to
+ * @nc_packet: the nc packet to be checked
+ *
+ * Returns false as soon as the entry in the fifo queue has not been timed out
+ * yet and true otherwise.
+ */
+static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
+				    struct batadv_nc_path *nc_path,
+				    struct batadv_nc_packet *nc_packet)
+{
+	unsigned long timeout = bat_priv->nc.max_buffer_time;
+	bool res = false;
+
+	/* Packets are added to tail, so the remaining packets did not time
+	 * out and we can stop processing the current queue
+	 */
+	if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
+	    !batadv_has_timed_out(nc_packet->timestamp, timeout))
+		goto out;
+
+	/* purge nc packet */
+	list_del(&nc_packet->list);
+	batadv_nc_packet_free(nc_packet);
+
+	res = true;
+
+out:
+	return res;
+}
+
+/**
  * batadv_nc_fwd_flush - Checks whether the given nc packet has hit its forward
  *  timeout. If so, the packet is no longer delayed, immediately sent and the
  *  entry deleted from the queue. Has to be called with the appropriate locks.
@@ -545,6 +611,8 @@ static void batadv_nc_worker(struct work_struct *work)
 	batadv_nc_purge_orig_hash(bat_priv);
 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
 			      batadv_nc_to_purge_nc_path_coding);
+	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
+			      batadv_nc_to_purge_nc_path_decoding);
 
 	timeout = bat_priv->nc.max_fwd_delay;
 
@@ -554,6 +622,13 @@ static void batadv_nc_worker(struct work_struct *work)
 		bat_priv->nc.timestamp_fwd_flush = jiffies;
 	}
 
+	if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
+				 bat_priv->nc.max_buffer_time)) {
+		batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
+					   batadv_nc_sniffed_purge);
+		bat_priv->nc.timestamp_sniffed_purge = jiffies;
+	}
+
 	/* Schedule a new check */
 	batadv_nc_start_timer(bat_priv);
 }
@@ -1144,6 +1219,41 @@ batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
 }
 
 /**
+ * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
+ *  unicast skb before it is stored for use in later decoding
+ * @bat_priv: the bat priv with all the soft interface information
+ * @skb: data skb to store
+ * @eth_dst_new: new destination mac address of skb
+ */
+static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
+					      struct sk_buff *skb,
+					      uint8_t *eth_dst_new)
+{
+	struct ethhdr *ethhdr;
+
+	/* Copy skb header to change the mac header */
+	skb = pskb_copy(skb, GFP_ATOMIC);
+	if (!skb)
+		return;
+
+	/* Set the mac header as if we actually sent the packet uncoded */
+	ethhdr = (struct ethhdr *)skb_mac_header(skb);
+	memcpy(ethhdr->h_source, ethhdr->h_dest, ETH_ALEN);
+	memcpy(ethhdr->h_dest, eth_dst_new, ETH_ALEN);
+
+	/* Set data pointer to MAC header to mimic packets from our tx path */
+	skb_push(skb, ETH_HLEN);
+
+	/* Add the packet to the decoding packet pool */
+	batadv_nc_skb_store_for_decoding(bat_priv, skb);
+
+	/* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
+	 * our ref
+	 */
+	kfree_skb(skb);
+}
+
+/**
  * batadv_nc_skb_dst_search - Loops through list of neighboring nodes the next
  *  hop has a good connection to (receives OGMs with a sufficient quality). We
  *  need to find a neighbor of our next hop that potentially sent a packet which
@@ -1180,6 +1290,12 @@ static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
 	if (!nc_packet)
 		return false;
 
+	/* Save packets for later decoding */
+	batadv_nc_skb_store_before_coding(bat_priv, skb,
+					  neigh_node->addr);
+	batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
+					  nc_packet->neigh_node->addr);
+
 	/* Code and send packets */
 	if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
 				   neigh_node))
@@ -1287,14 +1403,98 @@ out:
 }
 
 /**
+ * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
+ *  when decoding coded packets
+ * @bat_priv: the bat priv with all the soft interface information
+ * @skb: data skb to store
+ */
+void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
+				      struct sk_buff *skb)
+{
+	struct batadv_unicast_packet *packet;
+	struct batadv_nc_path *nc_path;
+	struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
+	__be32 packet_id;
+	u8 *payload;
+
+	/* Check if network coding is enabled */
+	if (!atomic_read(&bat_priv->network_coding))
+		goto out;
+
+	/* Check for supported packet type */
+	payload = skb_network_header(skb);
+	packet = (struct batadv_unicast_packet *)payload;
+	if (packet->header.packet_type != BATADV_UNICAST)
+		goto out;
+
+	/* Find existing nc_path or create a new */
+	nc_path = batadv_nc_get_path(bat_priv,
+				     bat_priv->nc.decoding_hash,
+				     ethhdr->h_source,
+				     ethhdr->h_dest);
+
+	if (!nc_path)
+		goto out;
+
+	/* Clone skb and adjust skb->data to point at batman header */
+	skb = skb_clone(skb, GFP_ATOMIC);
+	if (unlikely(!skb))
+		goto free_nc_path;
+
+	if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
+		goto free_skb;
+
+	if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
+		goto free_skb;
+
+	/* Add skb to nc_path */
+	packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
+	if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
+		goto free_skb;
+
+	batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
+	return;
+
+free_skb:
+	kfree_skb(skb);
+free_nc_path:
+	batadv_nc_path_free_ref(nc_path);
+out:
+	return;
+}
+
+/**
+ * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
+ *  should be saved in the decoding buffer and, if so, store it there
+ * @bat_priv: the bat priv with all the soft interface information
+ * @skb: unicast skb to store
+ */
+void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
+					 struct sk_buff *skb)
+{
+	struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
+
+	if (batadv_is_my_mac(ethhdr->h_dest))
+		return;
+
+	/* Set data pointer to MAC header to mimic packets from our tx path */
+	skb_push(skb, ETH_HLEN);
+
+	batadv_nc_skb_store_for_decoding(bat_priv, skb);
+}
+
+/**
  * batadv_network_coding_free - clean up network coding memory
  * @bat_priv: the bat priv with all the soft interface information
  */
 void batadv_network_coding_free(struct batadv_priv *bat_priv)
 {
 	cancel_delayed_work_sync(&bat_priv->nc.work);
+
 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
 	batadv_hash_destroy(bat_priv->nc.coding_hash);
+	batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
+	batadv_hash_destroy(bat_priv->nc.decoding_hash);
 }
 
 /**
@@ -1376,6 +1576,11 @@ int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
 	if (!file)
 		goto out;
 
+	file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir,
+				  &bat_priv->nc.max_buffer_time);
+	if (!file)
+		goto out;
+
 	return 0;
 
 out:
network-coding.h +18 -0
index 48c1219..cc2cd2f 100644
@@ -37,6 +37,10 @@ void batadv_nc_init_orig(struct batadv_orig_node *orig_node);
 bool batadv_nc_skb_forward(struct sk_buff *skb,
 			   struct batadv_neigh_node *neigh_node,
 			   struct ethhdr *ethhdr);
+void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
+				      struct sk_buff *skb);
+void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
+					 struct sk_buff *skb);
 int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset);
 int batadv_nc_init_debugfs(struct batadv_priv *bat_priv);
 
@@ -88,6 +92,20 @@ static inline bool batadv_nc_skb_forward(struct sk_buff *skb,
 	return false;
 }
 
+static inline void
+batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
+				 struct sk_buff *skb)
+{
+	return;
+}
+
+static inline void
+batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
+				    struct sk_buff *skb)
+{
+	return;
+}
+
 static inline int batadv_nc_nodes_seq_print_text(struct seq_file *seq,
 						 void *offset)
 {
routing.c +7 -1
index d7bd4d1..59e1894 100644
@@ -1053,8 +1053,14 @@ int batadv_recv_unicast_packet(struct sk_buff *skb,
 	if (is4addr)
 		hdr_size = sizeof(*unicast_4addr_packet);
 
-	if (batadv_check_unicast_packet(skb, hdr_size) < 0)
+	if (batadv_check_unicast_packet(skb, hdr_size) < 0) {
+		/* Even though the packet is to be dropped, we might save
+		 * it to use for decoding a later received coded packet
+		 */
+		batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
+
 		return NET_RX_DROP;
+	}
 
 	if (!batadv_check_unicast_ttvn(bat_priv, skb))
 		return NET_RX_DROP;
send.c +5 -0
index c7f7023..67aa098 100644
@@ -27,6 +27,7 @@
 #include "vis.h"
 #include "gateway_common.h"
 #include "originator.h"
+#include "network-coding.h"
 
 static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
 
@@ -37,6 +38,7 @@ int batadv_send_skb_packet(struct sk_buff *skb,
 			   struct batadv_hard_iface *hard_iface,
 			   const uint8_t *dst_addr)
 {
+	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
 	struct ethhdr *ethhdr;
 
 	if (hard_iface->if_status != BATADV_IF_ACTIVE)
@@ -68,6 +70,9 @@ int batadv_send_skb_packet(struct sk_buff *skb,
 
 	skb->dev = hard_iface->net_dev;
 
+	/* Save a clone of the skb to use when decoding coded packets */
+	batadv_nc_skb_store_for_decoding(bat_priv, skb);
+
 	/* dev_queue_xmit() returns a negative result on error.	 However on
 	 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
 	 * (which is > 0). This will not be treated as an error.
soft-interface.c +1 -0
index e29cce0..3fc0bc0 100644
@@ -640,6 +640,7 @@ static const struct {
 	{ "nc_code_bytes" },
 	{ "nc_recode" },
 	{ "nc_recode_bytes" },
+	{ "nc_buffer" },
 #endif
 };
 
types.h +10 -0
index a44e948..90df980 100644
@@ -197,6 +197,7 @@ enum batadv_counters {
 	BATADV_CNT_NC_CODE_BYTES,
 	BATADV_CNT_NC_RECODE,
 	BATADV_CNT_NC_RECODE_BYTES,
+	BATADV_CNT_NC_BUFFER,
 #endif
 	BATADV_CNT_NUM,
 };
@@ -282,18 +283,27 @@ struct batadv_priv_dat {
  * @work: work queue callback item for cleanup
  * @min_tq: only consider neighbors for encoding if neigh_tq > min_tq
  * @max_fwd_delay: maximum packet forward delay to allow coding of packets
+ * @max_buffer_time: buffer time for sniffed packets used to decoding
  * @timestamp_fwd_flush: timestamp of last forward packet queue flush
+ * @timestamp_sniffed_purge: timestamp of last sniffed packet queue purge
  * @coding_hash: Hash table used to buffer skbs while waiting for another
  *  incoming skb to code it with. Skbs are added to the buffer just before being
  *  forwarded in routing.c
+ * @decoding_hash: Hash table used to buffer skbs that might be needed to decode
+ *  a received coded skb. The buffer is used for 1) skbs arriving on the
+ *  soft-interface; 2) skbs overheard on the hard-interface; and 3) skbs
+ *  forwarded by batman-adv.
  */
 struct batadv_priv_nc {
 	struct delayed_work work;
 	struct dentry *debug_dir;
 	u8 min_tq;
 	u32 max_fwd_delay;
+	u32 max_buffer_time;
 	unsigned long timestamp_fwd_flush;
+	unsigned long timestamp_sniffed_purge;
 	struct batadv_hashtable *coding_hash;
+	struct batadv_hashtable *decoding_hash;
 };
 
 /**
Signature · 1 line
1.8.0
#11 lore raw

RFC 6/6 batman-adv: Receive coded packets and decode them.

In reply to Martin Hundebøll

When receiving a network coded packet, the decoding buffer is searched for a packet to use for decoding. The source, destination, and crc32 from the coded packet is used to identify the wanted packet. The decoded packet is passed to the usual unicast receiver function, as had it never been network coded.

---
 network-coding.c | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 soft-interface.c |   4 +
 types.h          |   4 +
 3 files changed, 238 insertions(+)
network-coding.c +230 -0
index 1edd2d4..11233d5 100644
@@ -25,11 +25,14 @@
 #include "send.h"
 #include "originator.h"
 #include "hard-interface.h"
+#include "routing.h"
 
 static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
 static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
 
 static void batadv_nc_worker(struct work_struct *work);
+static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
+				       struct batadv_hard_iface *recv_if);
 
 /**
  * batadv_nc_start_timer - initialise the nc periodic worker
@@ -69,6 +72,11 @@ int batadv_network_coding_init(struct batadv_priv *bat_priv)
 	batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
 				   &batadv_nc_decoding_hash_lock_class_key);
 
+	/* Register our packet type */
+	if (batadv_recv_handler_register(BATADV_CODED,
+					 batadv_nc_recv_coded_packet) < 0)
+		goto err;
+
 	batadv_nc_start_timer(bat_priv);
 	return 0;
 
@@ -1484,11 +1492,233 @@ void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
 }
 
 /**
+ * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
+ *  in nc_packet
+ * @skb: unicast skb to decode
+ * @nc_packet: decode data needed to decode the skb
+ *
+ * Returns pointer to decoded unicast packet if the packet was decoded or NULL
+ * in case of an error.
+ */
+static struct batadv_unicast_packet *
+batadv_nc_skb_decode_packet(struct sk_buff *skb,
+			    struct batadv_nc_packet *nc_packet)
+{
+	const int h_size = sizeof(struct batadv_unicast_packet);
+	const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
+	struct batadv_unicast_packet *unicast_packet;
+	struct batadv_coded_packet coded_packet_tmp;
+	struct ethhdr *ethhdr, ethhdr_tmp;
+	uint8_t *orig_dest, ttl, ttvn;
+	unsigned int coding_len;
+
+	/* Save headers temporarily */
+	memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
+	memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
+
+	if (skb_cow(skb, 0) < 0)
+		return NULL;
+
+	if (unlikely(!skb_pull_rcsum(skb, h_diff)))
+		return NULL;
+
+	/* Data points to batman header, so set mac header 14 bytes before
+	 * and network to data
+	 */
+	skb_set_mac_header(skb, -ETH_HLEN);
+	skb_reset_network_header(skb);
+
+	/* Reconstruct original mac header */
+	ethhdr = (struct ethhdr *)skb_mac_header(skb);
+	memcpy(ethhdr, &ethhdr_tmp, sizeof(*ethhdr));
+
+	/* Select the correct unicast header information based on the location
+	 * of our mac address in the coded_packet header
+	 */
+	if (batadv_is_my_mac(coded_packet_tmp.second_dest)) {
+		/* If we are the second destination the packet was overheard,
+		 * so the Ethernet address must be copied to h_dest and
+		 * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
+		 */
+		memcpy(ethhdr->h_dest, coded_packet_tmp.second_dest, ETH_ALEN);
+		skb->pkt_type = PACKET_HOST;
+
+		orig_dest = coded_packet_tmp.second_orig_dest;
+		ttl = coded_packet_tmp.second_ttl;
+		ttvn = coded_packet_tmp.second_ttvn;
+	} else {
+		orig_dest = coded_packet_tmp.first_orig_dest;
+		ttl = coded_packet_tmp.header.ttl;
+		ttvn = coded_packet_tmp.first_ttvn;
+	}
+
+	coding_len = ntohs(coded_packet_tmp.coded_len);
+
+	if (coding_len > skb->len)
+		return NULL;
+
+	/* Here the magic is reversed:
+	 *   extract the missing packet from the received coded packet
+	 */
+	batadv_memxor(skb->data + h_size,
+		      nc_packet->skb->data + h_size,
+		      coding_len);
+
+	/* Resize decoded skb if decoded with larger packet */
+	if (nc_packet->skb->len > coding_len + h_size)
+		pskb_trim_rcsum(skb, coding_len + h_size);
+
+	/* Create decoded unicast packet */
+	unicast_packet = (struct batadv_unicast_packet *)skb->data;
+	unicast_packet->header.packet_type = BATADV_UNICAST;
+	unicast_packet->header.version = BATADV_COMPAT_VERSION;
+	unicast_packet->header.ttl = ttl;
+	memcpy(unicast_packet->dest, orig_dest, ETH_ALEN);
+	unicast_packet->ttvn = ttvn;
+
+	batadv_nc_packet_free(nc_packet);
+	return unicast_packet;
+}
+
+/**
+ * batadv_nc_find_decoding_packet - search through buffered decoding data to
+ *  find the data needed to decode the coded packet
+ * @bat_priv: the bat priv with all the soft interface information
+ * @ethhdr: pointer to the ethernet header inside the coded packet
+ * @coded: coded packet we try to find decode data for
+ *
+ * Returns pointer to nc packet if the needed data was found or NULL otherwise.
+ */
+static struct batadv_nc_packet *
+batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
+			       struct ethhdr *ethhdr,
+			       struct batadv_coded_packet *coded)
+{
+	struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
+	struct hlist_node *hnode;
+	struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
+	struct batadv_nc_path *nc_path, nc_path_key;
+	uint8_t *dest, *source;
+	__be32 packet_id;
+	int index;
+
+	if (!hash)
+		return NULL;
+
+	/* Select the correct packet id based on the location of our mac-addr */
+	dest = ethhdr->h_source;
+	if (!batadv_is_my_mac(coded->second_dest)) {
+		source = coded->second_source;
+		packet_id = coded->second_crc;
+	} else {
+		source = coded->first_source;
+		packet_id = coded->first_crc;
+	}
+
+	batadv_nc_hash_key_gen(&nc_path_key, source, dest);
+	index = batadv_nc_hash_choose(&nc_path_key, hash->size);
+
+	/* Search for matching coding path */
+	rcu_read_lock();
+	hlist_for_each_entry_rcu(nc_path, hnode, &hash->table[index],
+				 hash_entry) {
+		/* Find matching nc_packet */
+		spin_lock_bh(&nc_path->packet_list_lock);
+		list_for_each_entry(tmp_nc_packet,
+				    &nc_path->packet_list, list) {
+			if (packet_id == tmp_nc_packet->packet_id) {
+				list_del(&tmp_nc_packet->list);
+
+				nc_packet = tmp_nc_packet;
+				break;
+			}
+		}
+		spin_unlock_bh(&nc_path->packet_list_lock);
+
+		if (nc_packet)
+			break;
+	}
+	rcu_read_unlock();
+
+	if (!nc_packet)
+		batadv_dbg(BATADV_DBG_NC, bat_priv,
+			   "No decoding packet found for %u\n", packet_id);
+
+	return nc_packet;
+}
+
+/**
+ * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
+ *  resulting unicast packet
+ * @skb: incoming coded packet
+ * @recv_if: pointer to interface this packet was received on
+ */
+static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
+				       struct batadv_hard_iface *recv_if)
+{
+	struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
+	struct batadv_unicast_packet *unicast_packet;
+	struct batadv_coded_packet *coded_packet;
+	struct batadv_nc_packet *nc_packet;
+	struct ethhdr *ethhdr;
+	int hdr_size = sizeof(*coded_packet);
+
+	/* Check if network coding is enabled */
+	if (!atomic_read(&bat_priv->network_coding))
+		return NET_RX_DROP;
+
+	/* Make sure we can access (and remove) header */
+	if (unlikely(!pskb_may_pull(skb, hdr_size)))
+		return NET_RX_DROP;
+
+	coded_packet = (struct batadv_coded_packet *)skb->data;
+	ethhdr = (struct ethhdr *)skb_mac_header(skb);
+
+	/* Verify frame is destined for us */
+	if (!batadv_is_my_mac(ethhdr->h_dest) &&
+	    !batadv_is_my_mac(coded_packet->second_dest))
+		return NET_RX_DROP;
+
+	/* Update stat counter */
+	if (batadv_is_my_mac(coded_packet->second_dest))
+		batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
+
+	nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
+						   coded_packet);
+	if (!nc_packet) {
+		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
+		return NET_RX_DROP;
+	}
+
+	/* Make skb's linear, because decoding accesses the entire buffer */
+	if (skb_linearize(skb) < 0)
+		return NET_RX_DROP;
+
+	if (skb_linearize(nc_packet->skb) < 0)
+		return NET_RX_DROP;
+
+	/* Decode the packet */
+	unicast_packet = batadv_nc_skb_decode_packet(skb, nc_packet);
+	if (!unicast_packet) {
+		batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
+		return NET_RX_DROP;
+	}
+
+	/* Mark packet as decoded to do correct recoding when forwarding */
+	BATADV_SKB_CB(skb)->decoded = true;
+	batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
+	batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
+			   skb->len + ETH_HLEN);
+	return batadv_recv_unicast_packet(skb, recv_if);
+}
+
+/**
  * batadv_network_coding_free - clean up network coding memory
  * @bat_priv: the bat priv with all the soft interface information
  */
 void batadv_network_coding_free(struct batadv_priv *bat_priv)
 {
+	batadv_recv_handler_unregister(BATADV_CODED);
 	cancel_delayed_work_sync(&bat_priv->nc.work);
 
 	batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
soft-interface.c +4 -0
index 3fc0bc0..387a9f2 100644
@@ -641,6 +641,10 @@ static const struct {
 	{ "nc_recode" },
 	{ "nc_recode_bytes" },
 	{ "nc_buffer" },
+	{ "nc_decode" },
+	{ "nc_decode_bytes" },
+	{ "nc_decode_failed" },
+	{ "nc_sniffed" },
 #endif
 };
 
types.h +4 -0
index 90df980..182daea 100644
@@ -198,6 +198,10 @@ enum batadv_counters {
 	BATADV_CNT_NC_RECODE,
 	BATADV_CNT_NC_RECODE_BYTES,
 	BATADV_CNT_NC_BUFFER,
+	BATADV_CNT_NC_DECODE,
+	BATADV_CNT_NC_DECODE_BYTES,
+	BATADV_CNT_NC_DECODE_FAILED,
+	BATADV_CNT_NC_SNIFFED,
 #endif
 	BATADV_CNT_NUM,
 };
Signature · 1 line
1.8.0
#2 lore raw

Re: [B.A.T.M.A.N.] [RFC 1/6] batman-adv: Add the initial code for network coding.

In reply to Martin Hundebøll

On Mon, Nov 19, 2012 at 02:21:59PM +0100, Martin Hundebøll wrote:
...

network-coding.c line 31

Quoted 7 lines
network-coding.c new file +7 -0
+/**
+ * batadv_nc_start_timer - initialise the nc periodic worker
+ * @bat_priv: the bat priv with all the soft interface information
+ */
+static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
+{
+	INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);

INIT_DELAYED_WORK is not really needed each and every time. It can be invoked when setting up the worker for the first time only. It's pure beautification in this case. There is a little performance implication but this does not hurt this code.

network-coding.c line 37

Quoted 6 lines
network-coding.c new file +6 -0
+	queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
+			   msecs_to_jiffies(10));
+}
+
+/**
+ * batadv_network_coding_init - initialise coding hash table and start

why not keeping the same namespace everywhere? The previous function name starts with batadv_nc_, now you are moving to batadv_network_coding_.

Review

Quoted 5 lines

+ * house keeping
+ * @bat_priv: the bat priv with all the soft interface information
+ */

(quoted diff) +2 -0
+int batadv_network_coding_init(struct batadv_priv *bat_priv)
+{

I think you could move the INIT_DELAYED_WORK here..

Quoted 4 lines
network-coding.c new file +4 -0
+	batadv_nc_start_timer(bat_priv);
+
+	return 0;
+}

[...]

Quoted 7 lines
network-coding.c new file +7 -0
+/**
+ * batadv_nc_worker - periodic task for house keeping related to network coding
+ * @work: kernel work struct
+ */
+static void batadv_nc_worker(struct work_struct *work)
+{
+	struct delayed_work *delayed_work;

network-coding.c line 73

Quoted 10 lines
network-coding.c new file +10 -0
+	struct batadv_priv_nc *priv_nc;
+	struct batadv_priv *bat_priv;
+
+	delayed_work = container_of(work, struct delayed_work, work);
+	priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
+	bat_priv = container_of(priv_nc, struct batadv_priv, nc);
+
+	/* Schedule a new check */
+	batadv_nc_start_timer(bat_priv);
+}

What's the meaning of this function? It just runs and reset the worker?

Quoted 6 lines
sysfs-class-net-mesh +0 -0
index bc41da6..c75e594 100644
@@ -74,6 +74,14 @@ Description:
                 Defines the interval in milliseconds in which batman

sysfs-class-net-mesh Description: · line 84

Quoted 10 lines
sysfs-class-net-mesh +8 -0
                 sends its protocol messages.
 
+What:           /sys/class/net/<mesh_iface>/mesh/network_coding
+Date:           Nov 2012
+Contact:        Martin Hundeboll <[email protected]>
+Description:
+                Controls whether Network Coding (using some magic
+                to send fewer wifi packets but still the same
+                content) is enabled or not.
+

Marek spent a lot of time to sort this list by alphabetical order, make him happy :)

Quoted 3 lines
sysfs-class-net-mesh +0 -0
 What:           /sys/class/net/<mesh_iface>/mesh/routing_algo
 Date:           Dec 2011
 Contact:        Marek Lindner <[email protected]>

[...]

Quoted 9 lines
sysfs-class-net-mesh +5 -0
+/**
+ * struct batadv_priv - per mesh interface private data
+ * @network_coding: enable/disable network coding
+ * @batadv_priv_nc: network coding private data
+ */
 struct batadv_priv {
 	atomic_t mesh_state;
 	struct net_device_stats stats;

sysfs-class-net-mesh struct batadv_priv { · line 325

Quoted 5 lines
sysfs-class-net-mesh +2 -0
@@ -308,6 +321,10 @@ struct batadv_priv {
 #ifdef CONFIG_BATMAN_ADV_DAT
 	struct batadv_priv_dat dat;
 #endif
+#ifdef CONFIG_BATMAN_ADV_NC
+	atomic_t network_coding; /* boolean */

just another style note: why don't you put this boolean switch together with the others?

Quoted 7 lines
sysfs-class-net-mesh +2 -0
+	struct batadv_priv_nc nc;
+#endif /* CONFIG_BATMAN_ADV_NC */
 };
 
 struct batadv_socket_client {
Signature · 1 line
1.8.0

Cheers,

Signature · 4 lines
Antonio Quartulli

..each of us alone is worth nothing..
Ernesto "Che" Guevara
#5 lore raw

Re: [B.A.T.M.A.N.] [RFC 2/6] batman-adv: Detect coding nodes and remove these after timeout

In reply to Martin Hundebøll

On Mon, Nov 19, 2012 at 02:22:00PM +0100, Martin Hundebøll wrote:
[...]

network-coding.c line 21

Quoted 10 lines
network-coding.c +2 -0
index 8d930e7..29f7744 100644
@@ -17,8 +17,12 @@
  * 02110-1301, USA
  */
 
+#include <linux/debugfs.h>
+

Ok, I don't want to make you feel a cavy :) but it is better to correct this stuff when we can: kernel-includes (<..h>) should follow project-includes ("..h")

network-coding.c static void batadv_nc_worker(struct work_struct *work) · line 222

Quoted 12 lines
network-coding.c +4 -0
 #include "main.h"
 #include "network-coding.h"
+#include "originator.h"
+#include "hard-interface.h"
 
 /**
@@ -68,11 +218,197 @@ static void batadv_nc_worker(struct work_struct *work)
 	priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
 	bat_priv = container_of(priv_nc, struct batadv_priv, nc);
 
+	batadv_nc_purge_orig_hash(bat_priv);
+

ah, now this function makes sense (with respect to what I said in reply to 1/6). Would it be better to add the worker here instead of adding it in 1/6?

Quoted 3 lines
network-coding.c +0 -0
 	/* Schedule a new check */
 	batadv_nc_start_timer(bat_priv);
 }

[...]

network-coding.c struct batadv_priv_dat { · line 282

Quoted 10 lines
network-coding.c +3 -0
@@ -268,9 +274,12 @@ struct batadv_priv_dat {
 /**
  * struct batadv_priv_nc - per mesh interface network coding private data
  * @work: work queue callback item for cleanup
+ * @min_tq: only consider neighbors for encoding if neigh_tq > min_tq
  */
 struct batadv_priv_nc {
 	struct delayed_work work;
+	struct dentry *debug_dir;
+	u8 min_tq;

If I am not mistaken min_tq is never modified. Why do we need to store it in a bat_priv field? And being a constant is it worth a define? In this way it an be easily found and tuned like the other constant params.

Cheers,

Signature · 4 lines
Antonio Quartulli

..each of us alone is worth nothing..
Ernesto "Che" Guevara
#6 lore raw

Re: [B.A.T.M.A.N.] [RFC 2/6] batman-adv: Detect coding nodes and remove these after timeout

Quoted 10 lines · On Monday, November 26, 2012 08:41:17 Antonio Quartulli wrote:

types.h struct batadv_priv_dat { · line 282

Quoted 6 lines

struct batadv_priv_nc {

types.h +2 -0
 	struct delayed_work work;
 
+	struct dentry *debug_dir;
+	u8 min_tq;

If I am not mistaken min_tq is never modified. Why do we need to store it in a bat_priv field? And being a constant is it worth a define? In this way it an be easily found and tuned like the other constant params.

It can be modified via debugfs:

+ file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
+ &bat_priv->nc.min_tq);

Cheers,
Marek

#7 lore raw

Re: [B.A.T.M.A.N.] [RFC 2/6] batman-adv: Detect coding nodes and remove these after timeout

Quoted 14 lines · Marek Lindner
Quoted 10 lines · On Monday, November 26, 2012 08:41:17 Antonio Quartulli wrote:

types.h struct batadv_priv_dat { · line 282

Quoted 6 lines

struct batadv_priv_nc {

types.h +2 -0
 	struct delayed_work work;
 
+	struct dentry *debug_dir;
+	u8 min_tq;

If I am not mistaken min_tq is never modified. Why do we need to store it in a bat_priv field? And being a constant is it worth a define? In this way it an be easily found and tuned like the other constant params.

It can be modified via debugfs:

network-coding.c void batadv_network_coding_free(struct batadv_priv *bat_priv) · line 490

Quoted 2 lines
network-coding.c +2 -0
+       file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
+                                &bat_priv->nc.min_tq);

Oh, ok! I hadn't seen the "W" in the mode.
Thanks.

Cheers,

Signature · 4 lines
Antonio Quartulli

..each of us alone is worth nothing..
Ernesto "Che" Guevara
#3 lore raw

Re: [B.A.T.M.A.N.] [RFC 1/6] batman-adv: Add the initial code for network coding.

In reply to Antonio Quartulli
Quoted 16 lines · Antonio Quartulli
Quoted 14 lines

+/**

types.h struct batadv_priv { · line 325

Quoted 13 lines

+ * struct batadv_priv - per mesh interface private data
+ * @network_coding: enable/disable network coding
+ * @batadv_priv_nc: network coding private data
+ */
struct batadv_priv {

types.h +2 -0
 	atomic_t mesh_state;
 	struct net_device_stats stats;
@@ -308,6 +321,10 @@ struct batadv_priv {
 #ifdef CONFIG_BATMAN_ADV_DAT
 	struct batadv_priv_dat dat;
 #endif
+#ifdef CONFIG_BATMAN_ADV_NC
+	atomic_t network_coding; /* boolean */

just another style note: why don't you put this boolean switch together with the others?

Because of the sysfs-framework in batman-adv, that expects an atomic_t with the same name as the file to be in batadv_priv - just like with your DAT and Simon's BLAII :)

Signature · 8 lines
Kind Regards
Martin Hundebøll
Frederiks Allé 99, 1.th
8000 Aarhus C
Denmark

+45 61 65 54 61
[email protected]