1 /* Copyright (C) 2012-2013 B.A.T.M.A.N. contributors:
3 * Martin Hundebøll, Jeppe Ledet-Pedersen
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include <linux/debugfs.h>
24 #include "network-coding.h"
26 #include "originator.h"
27 #include "hard-interface.h"
29 static struct lock_class_key batadv_nc_coding_hash_lock_class_key
;
31 static void batadv_nc_worker(struct work_struct
*work
);
34 * batadv_nc_start_timer - initialise the nc periodic worker
35 * @bat_priv: the bat priv with all the soft interface information
37 static void batadv_nc_start_timer(struct batadv_priv
*bat_priv
)
39 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->nc
.work
,
40 msecs_to_jiffies(10));
44 * batadv_nc_init - initialise coding hash table and start house keeping
45 * @bat_priv: the bat priv with all the soft interface information
47 int batadv_nc_init(struct batadv_priv
*bat_priv
)
49 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
51 if (bat_priv
->nc
.coding_hash
)
54 bat_priv
->nc
.coding_hash
= batadv_hash_new(128);
55 if (!bat_priv
->nc
.coding_hash
)
58 batadv_hash_set_lock_class(bat_priv
->nc
.coding_hash
,
59 &batadv_nc_coding_hash_lock_class_key
);
61 INIT_DELAYED_WORK(&bat_priv
->nc
.work
, batadv_nc_worker
);
62 batadv_nc_start_timer(bat_priv
);
71 * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
72 * @bat_priv: the bat priv with all the soft interface information
74 void batadv_nc_init_bat_priv(struct batadv_priv
*bat_priv
)
76 atomic_set(&bat_priv
->network_coding
, 1);
77 bat_priv
->nc
.min_tq
= 200;
78 bat_priv
->nc
.max_fwd_delay
= 10;
82 * batadv_nc_init_orig - initialise the nc fields of an orig_node
83 * @orig_node: the orig_node which is going to be initialised
85 void batadv_nc_init_orig(struct batadv_orig_node
*orig_node
)
87 INIT_LIST_HEAD(&orig_node
->in_coding_list
);
88 INIT_LIST_HEAD(&orig_node
->out_coding_list
);
89 spin_lock_init(&orig_node
->in_coding_list_lock
);
90 spin_lock_init(&orig_node
->out_coding_list_lock
);
94 * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
95 * its refcount on the orig_node
96 * @rcu: rcu pointer of the nc node
98 static void batadv_nc_node_free_rcu(struct rcu_head
*rcu
)
100 struct batadv_nc_node
*nc_node
;
102 nc_node
= container_of(rcu
, struct batadv_nc_node
, rcu
);
103 batadv_orig_node_free_ref(nc_node
->orig_node
);
108 * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
110 * @nc_node: the nc node to free
112 static void batadv_nc_node_free_ref(struct batadv_nc_node
*nc_node
)
114 if (atomic_dec_and_test(&nc_node
->refcount
))
115 call_rcu(&nc_node
->rcu
, batadv_nc_node_free_rcu
);
119 * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
121 * @nc_path: the nc node to free
123 static void batadv_nc_path_free_ref(struct batadv_nc_path
*nc_path
)
125 if (atomic_dec_and_test(&nc_path
->refcount
))
126 kfree_rcu(nc_path
, rcu
);
130 * batadv_nc_packet_free - frees nc packet
131 * @nc_packet: the nc packet to free
133 static void batadv_nc_packet_free(struct batadv_nc_packet
*nc_packet
)
136 kfree_skb(nc_packet
->skb
);
138 batadv_nc_path_free_ref(nc_packet
->nc_path
);
143 * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
144 * @bat_priv: the bat priv with all the soft interface information
145 * @nc_node: the nc node to check
147 * Returns true if the entry has to be purged now, false otherwise
149 static bool batadv_nc_to_purge_nc_node(struct batadv_priv
*bat_priv
,
150 struct batadv_nc_node
*nc_node
)
152 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
155 return batadv_has_timed_out(nc_node
->last_seen
, BATADV_NC_NODE_TIMEOUT
);
159 * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
160 * @bat_priv: the bat priv with all the soft interface information
161 * @nc_path: the nc path to check
163 * Returns true if the entry has to be purged now, false otherwise
165 static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv
*bat_priv
,
166 struct batadv_nc_path
*nc_path
)
168 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
171 /* purge the path when no packets has been added for 10 times the
174 return batadv_has_timed_out(nc_path
->last_valid
,
175 bat_priv
->nc
.max_fwd_delay
* 10);
179 * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
181 * @bat_priv: the bat priv with all the soft interface information
182 * @list: list of nc nodes
183 * @lock: nc node list lock
184 * @to_purge: function in charge to decide whether an entry has to be purged or
185 * not. This function takes the nc node as argument and has to return
186 * a boolean value: true if the entry has to be deleted, false
190 batadv_nc_purge_orig_nc_nodes(struct batadv_priv
*bat_priv
,
191 struct list_head
*list
,
193 bool (*to_purge
)(struct batadv_priv
*,
194 struct batadv_nc_node
*))
196 struct batadv_nc_node
*nc_node
, *nc_node_tmp
;
198 /* For each nc_node in list */
200 list_for_each_entry_safe(nc_node
, nc_node_tmp
, list
, list
) {
201 /* if an helper function has been passed as parameter,
202 * ask it if the entry has to be purged or not
204 if (to_purge
&& !to_purge(bat_priv
, nc_node
))
207 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
208 "Removing nc_node %pM -> %pM\n",
209 nc_node
->addr
, nc_node
->orig_node
->orig
);
210 list_del_rcu(&nc_node
->list
);
211 batadv_nc_node_free_ref(nc_node
);
213 spin_unlock_bh(lock
);
217 * batadv_nc_purge_orig - purges all nc node data attached of the given
219 * @bat_priv: the bat priv with all the soft interface information
220 * @orig_node: orig_node with the nc node entries to be purged
221 * @to_purge: function in charge to decide whether an entry has to be purged or
222 * not. This function takes the nc node as argument and has to return
223 * a boolean value: true is the entry has to be deleted, false
226 void batadv_nc_purge_orig(struct batadv_priv
*bat_priv
,
227 struct batadv_orig_node
*orig_node
,
228 bool (*to_purge
)(struct batadv_priv
*,
229 struct batadv_nc_node
*))
231 /* Check ingoing nc_node's of this orig_node */
232 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->in_coding_list
,
233 &orig_node
->in_coding_list_lock
,
236 /* Check outgoing nc_node's of this orig_node */
237 batadv_nc_purge_orig_nc_nodes(bat_priv
, &orig_node
->out_coding_list
,
238 &orig_node
->out_coding_list_lock
,
243 * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
244 * have timed out nc nodes
245 * @bat_priv: the bat priv with all the soft interface information
247 static void batadv_nc_purge_orig_hash(struct batadv_priv
*bat_priv
)
249 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
250 struct hlist_head
*head
;
251 struct batadv_orig_node
*orig_node
;
257 /* For each orig_node */
258 for (i
= 0; i
< hash
->size
; i
++) {
259 head
= &hash
->table
[i
];
262 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
)
263 batadv_nc_purge_orig(bat_priv
, orig_node
,
264 batadv_nc_to_purge_nc_node
);
270 * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
272 * @bat_priv: the bat priv with all the soft interface information
273 * @hash: hash table containing the nc paths to check
274 * @to_purge: function in charge to decide whether an entry has to be purged or
275 * not. This function takes the nc node as argument and has to return
276 * a boolean value: true is the entry has to be deleted, false
279 static void batadv_nc_purge_paths(struct batadv_priv
*bat_priv
,
280 struct batadv_hashtable
*hash
,
281 bool (*to_purge
)(struct batadv_priv
*,
282 struct batadv_nc_path
*))
284 struct hlist_head
*head
;
285 struct hlist_node
*node_tmp
;
286 struct batadv_nc_path
*nc_path
;
287 spinlock_t
*lock
; /* Protects lists in hash */
290 for (i
= 0; i
< hash
->size
; i
++) {
291 head
= &hash
->table
[i
];
292 lock
= &hash
->list_locks
[i
];
294 /* For each nc_path in this bin */
296 hlist_for_each_entry_safe(nc_path
, node_tmp
, head
, hash_entry
) {
297 /* if an helper function has been passed as parameter,
298 * ask it if the entry has to be purged or not
300 if (to_purge
&& !to_purge(bat_priv
, nc_path
))
303 /* purging an non-empty nc_path should never happen, but
304 * is observed under high CPU load. Delay the purging
305 * until next iteration to allow the packet_list to be
308 if (!unlikely(list_empty(&nc_path
->packet_list
))) {
309 net_ratelimited_function(printk
,
311 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
317 /* nc_path is unused, so remove it */
318 batadv_dbg(BATADV_DBG_NC
, bat_priv
,
319 "Remove nc_path %pM -> %pM\n",
320 nc_path
->prev_hop
, nc_path
->next_hop
);
321 hlist_del_rcu(&nc_path
->hash_entry
);
322 batadv_nc_path_free_ref(nc_path
);
324 spin_unlock_bh(lock
);
329 * batadv_nc_hash_key_gen - computes the nc_path hash key
330 * @key: buffer to hold the final hash key
331 * @src: source ethernet mac address going into the hash key
332 * @dst: destination ethernet mac address going into the hash key
334 static void batadv_nc_hash_key_gen(struct batadv_nc_path
*key
, const char *src
,
337 memcpy(key
->prev_hop
, src
, sizeof(key
->prev_hop
));
338 memcpy(key
->next_hop
, dst
, sizeof(key
->next_hop
));
342 * batadv_nc_hash_choose - compute the hash value for an nc path
343 * @data: data to hash
344 * @size: size of the hash table
346 * Returns the selected index in the hash table for the given data.
348 static uint32_t batadv_nc_hash_choose(const void *data
, uint32_t size
)
350 const struct batadv_nc_path
*nc_path
= data
;
353 hash
= batadv_hash_bytes(hash
, &nc_path
->prev_hop
,
354 sizeof(nc_path
->prev_hop
));
355 hash
= batadv_hash_bytes(hash
, &nc_path
->next_hop
,
356 sizeof(nc_path
->next_hop
));
359 hash
^= (hash
>> 11);
360 hash
+= (hash
<< 15);
366 * batadv_nc_hash_compare - comparing function used in the network coding hash
368 * @node: node in the local table
369 * @data2: second object to compare the node to
371 * Returns 1 if the two entry are the same, 0 otherwise
373 static int batadv_nc_hash_compare(const struct hlist_node
*node
,
376 const struct batadv_nc_path
*nc_path1
, *nc_path2
;
378 nc_path1
= container_of(node
, struct batadv_nc_path
, hash_entry
);
381 /* Return 1 if the two keys are identical */
382 if (memcmp(nc_path1
->prev_hop
, nc_path2
->prev_hop
,
383 sizeof(nc_path1
->prev_hop
)) != 0)
386 if (memcmp(nc_path1
->next_hop
, nc_path2
->next_hop
,
387 sizeof(nc_path1
->next_hop
)) != 0)
394 * batadv_nc_hash_find - search for an existing nc path and return it
395 * @hash: hash table containing the nc path
398 * Returns the nc_path if found, NULL otherwise.
400 static struct batadv_nc_path
*
401 batadv_nc_hash_find(struct batadv_hashtable
*hash
,
404 struct hlist_head
*head
;
405 struct batadv_nc_path
*nc_path
, *nc_path_tmp
= NULL
;
411 index
= batadv_nc_hash_choose(data
, hash
->size
);
412 head
= &hash
->table
[index
];
415 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
416 if (!batadv_nc_hash_compare(&nc_path
->hash_entry
, data
))
419 if (!atomic_inc_not_zero(&nc_path
->refcount
))
422 nc_path_tmp
= nc_path
;
431 * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
432 * @nc_packet: the nc packet to send
434 static void batadv_nc_send_packet(struct batadv_nc_packet
*nc_packet
)
436 batadv_send_skb_packet(nc_packet
->skb
,
437 nc_packet
->neigh_node
->if_incoming
,
438 nc_packet
->nc_path
->next_hop
);
439 nc_packet
->skb
= NULL
;
440 batadv_nc_packet_free(nc_packet
);
444 * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
445 * @bat_priv: the bat priv with all the soft interface information
446 * @nc_path: the nc path the packet belongs to
447 * @nc_packet: the nc packet to be checked
449 * Checks whether the given nc packet has hit its forward timeout. If so, the
450 * packet is no longer delayed, immediately sent and the entry deleted from the
451 * queue. Has to be called with the appropriate locks.
453 * Returns false as soon as the entry in the fifo queue has not been timed out
454 * yet and true otherwise.
456 static bool batadv_nc_fwd_flush(struct batadv_priv
*bat_priv
,
457 struct batadv_nc_path
*nc_path
,
458 struct batadv_nc_packet
*nc_packet
)
460 unsigned long timeout
= bat_priv
->nc
.max_fwd_delay
;
462 /* Packets are added to tail, so the remaining packets did not time
463 * out and we can stop processing the current queue
465 if (atomic_read(&bat_priv
->mesh_state
) == BATADV_MESH_ACTIVE
&&
466 !batadv_has_timed_out(nc_packet
->timestamp
, timeout
))
470 batadv_inc_counter(bat_priv
, BATADV_CNT_FORWARD
);
471 batadv_add_counter(bat_priv
, BATADV_CNT_FORWARD_BYTES
,
472 nc_packet
->skb
->len
+ ETH_HLEN
);
473 list_del(&nc_packet
->list
);
474 batadv_nc_send_packet(nc_packet
);
480 * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
482 * @bat_priv: the bat priv with all the soft interface information
483 * @hash: to be processed hash table
484 * @process_fn: Function called to process given nc packet. Should return true
485 * to encourage this function to proceed with the next packet.
486 * Otherwise the rest of the current queue is skipped.
489 batadv_nc_process_nc_paths(struct batadv_priv
*bat_priv
,
490 struct batadv_hashtable
*hash
,
491 bool (*process_fn
)(struct batadv_priv
*,
492 struct batadv_nc_path
*,
493 struct batadv_nc_packet
*))
495 struct hlist_head
*head
;
496 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
497 struct batadv_nc_path
*nc_path
;
504 /* Loop hash table bins */
505 for (i
= 0; i
< hash
->size
; i
++) {
506 head
= &hash
->table
[i
];
508 /* Loop coding paths */
510 hlist_for_each_entry_rcu(nc_path
, head
, hash_entry
) {
512 spin_lock_bh(&nc_path
->packet_list_lock
);
513 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
514 &nc_path
->packet_list
, list
) {
515 ret
= process_fn(bat_priv
, nc_path
, nc_packet
);
519 spin_unlock_bh(&nc_path
->packet_list_lock
);
526 * batadv_nc_worker - periodic task for house keeping related to network coding
527 * @work: kernel work struct
529 static void batadv_nc_worker(struct work_struct
*work
)
531 struct delayed_work
*delayed_work
;
532 struct batadv_priv_nc
*priv_nc
;
533 struct batadv_priv
*bat_priv
;
534 unsigned long timeout
;
536 delayed_work
= container_of(work
, struct delayed_work
, work
);
537 priv_nc
= container_of(delayed_work
, struct batadv_priv_nc
, work
);
538 bat_priv
= container_of(priv_nc
, struct batadv_priv
, nc
);
540 batadv_nc_purge_orig_hash(bat_priv
);
541 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
542 batadv_nc_to_purge_nc_path_coding
);
544 timeout
= bat_priv
->nc
.max_fwd_delay
;
546 if (batadv_has_timed_out(bat_priv
->nc
.timestamp_fwd_flush
, timeout
)) {
547 batadv_nc_process_nc_paths(bat_priv
, bat_priv
->nc
.coding_hash
,
548 batadv_nc_fwd_flush
);
549 bat_priv
->nc
.timestamp_fwd_flush
= jiffies
;
552 /* Schedule a new check */
553 batadv_nc_start_timer(bat_priv
);
557 * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
559 * @bat_priv: the bat priv with all the soft interface information
560 * @orig_node: neighboring orig node which may be used as nc candidate
561 * @ogm_packet: incoming ogm packet also used for the checks
564 * 1) The OGM must have the most recent sequence number.
565 * 2) The TTL must be decremented by one and only one.
566 * 3) The OGM must be received from the first hop from orig_node.
567 * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
569 static bool batadv_can_nc_with_orig(struct batadv_priv
*bat_priv
,
570 struct batadv_orig_node
*orig_node
,
571 struct batadv_ogm_packet
*ogm_packet
)
573 if (orig_node
->last_real_seqno
!= ogm_packet
->seqno
)
575 if (orig_node
->last_ttl
!= ogm_packet
->header
.ttl
+ 1)
577 if (!batadv_compare_eth(ogm_packet
->orig
, ogm_packet
->prev_sender
))
579 if (ogm_packet
->tq
< bat_priv
->nc
.min_tq
)
586 * batadv_nc_find_nc_node - search for an existing nc node and return it
587 * @orig_node: orig node originating the ogm packet
588 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
589 * (can be equal to orig_node)
590 * @in_coding: traverse incoming or outgoing network coding list
592 * Returns the nc_node if found, NULL otherwise.
594 static struct batadv_nc_node
595 *batadv_nc_find_nc_node(struct batadv_orig_node
*orig_node
,
596 struct batadv_orig_node
*orig_neigh_node
,
599 struct batadv_nc_node
*nc_node
, *nc_node_out
= NULL
;
600 struct list_head
*list
;
603 list
= &orig_neigh_node
->in_coding_list
;
605 list
= &orig_neigh_node
->out_coding_list
;
607 /* Traverse list of nc_nodes to orig_node */
609 list_for_each_entry_rcu(nc_node
, list
, list
) {
610 if (!batadv_compare_eth(nc_node
->addr
, orig_node
->orig
))
613 if (!atomic_inc_not_zero(&nc_node
->refcount
))
617 nc_node_out
= nc_node
;
626 * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
628 * @bat_priv: the bat priv with all the soft interface information
629 * @orig_node: orig node originating the ogm packet
630 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
631 * (can be equal to orig_node)
632 * @in_coding: traverse incoming or outgoing network coding list
634 * Returns the nc_node if found or created, NULL in case of an error.
636 static struct batadv_nc_node
637 *batadv_nc_get_nc_node(struct batadv_priv
*bat_priv
,
638 struct batadv_orig_node
*orig_node
,
639 struct batadv_orig_node
*orig_neigh_node
,
642 struct batadv_nc_node
*nc_node
;
643 spinlock_t
*lock
; /* Used to lock list selected by "int in_coding" */
644 struct list_head
*list
;
646 /* Check if nc_node is already added */
647 nc_node
= batadv_nc_find_nc_node(orig_node
, orig_neigh_node
, in_coding
);
653 nc_node
= kzalloc(sizeof(*nc_node
), GFP_ATOMIC
);
657 if (!atomic_inc_not_zero(&orig_neigh_node
->refcount
))
660 /* Initialize nc_node */
661 INIT_LIST_HEAD(&nc_node
->list
);
662 memcpy(nc_node
->addr
, orig_node
->orig
, ETH_ALEN
);
663 nc_node
->orig_node
= orig_neigh_node
;
664 atomic_set(&nc_node
->refcount
, 2);
666 /* Select ingoing or outgoing coding node */
668 lock
= &orig_neigh_node
->in_coding_list_lock
;
669 list
= &orig_neigh_node
->in_coding_list
;
671 lock
= &orig_neigh_node
->out_coding_list_lock
;
672 list
= &orig_neigh_node
->out_coding_list
;
675 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_node %pM -> %pM\n",
676 nc_node
->addr
, nc_node
->orig_node
->orig
);
678 /* Add nc_node to orig_node */
680 list_add_tail_rcu(&nc_node
->list
, list
);
681 spin_unlock_bh(lock
);
691 * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
692 * (best called on incoming OGMs)
693 * @bat_priv: the bat priv with all the soft interface information
694 * @orig_node: orig node originating the ogm packet
695 * @orig_neigh_node: neighboring orig node from which we received the ogm packet
696 * (can be equal to orig_node)
697 * @ogm_packet: incoming ogm packet
698 * @is_single_hop_neigh: orig_node is a single hop neighbor
700 void batadv_nc_update_nc_node(struct batadv_priv
*bat_priv
,
701 struct batadv_orig_node
*orig_node
,
702 struct batadv_orig_node
*orig_neigh_node
,
703 struct batadv_ogm_packet
*ogm_packet
,
704 int is_single_hop_neigh
)
706 struct batadv_nc_node
*in_nc_node
= NULL
, *out_nc_node
= NULL
;
708 /* Check if network coding is enabled */
709 if (!atomic_read(&bat_priv
->network_coding
))
712 /* accept ogms from 'good' neighbors and single hop neighbors */
713 if (!batadv_can_nc_with_orig(bat_priv
, orig_node
, ogm_packet
) &&
714 !is_single_hop_neigh
)
717 /* Add orig_node as in_nc_node on hop */
718 in_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_node
,
719 orig_neigh_node
, true);
723 in_nc_node
->last_seen
= jiffies
;
725 /* Add hop as out_nc_node on orig_node */
726 out_nc_node
= batadv_nc_get_nc_node(bat_priv
, orig_neigh_node
,
731 out_nc_node
->last_seen
= jiffies
;
735 batadv_nc_node_free_ref(in_nc_node
);
737 batadv_nc_node_free_ref(out_nc_node
);
741 * batadv_nc_get_path - get existing nc_path or allocate a new one
742 * @bat_priv: the bat priv with all the soft interface information
743 * @hash: hash table containing the nc path
744 * @src: ethernet source address - first half of the nc path search key
745 * @dst: ethernet destination address - second half of the nc path search key
747 * Returns pointer to nc_path if the path was found or created, returns NULL
750 static struct batadv_nc_path
*batadv_nc_get_path(struct batadv_priv
*bat_priv
,
751 struct batadv_hashtable
*hash
,
756 struct batadv_nc_path
*nc_path
, nc_path_key
;
758 batadv_nc_hash_key_gen(&nc_path_key
, src
, dst
);
760 /* Search for existing nc_path */
761 nc_path
= batadv_nc_hash_find(hash
, (void *)&nc_path_key
);
764 /* Set timestamp to delay removal of nc_path */
765 nc_path
->last_valid
= jiffies
;
769 /* No existing nc_path was found; create a new */
770 nc_path
= kzalloc(sizeof(*nc_path
), GFP_ATOMIC
);
775 /* Initialize nc_path */
776 INIT_LIST_HEAD(&nc_path
->packet_list
);
777 spin_lock_init(&nc_path
->packet_list_lock
);
778 atomic_set(&nc_path
->refcount
, 2);
779 nc_path
->last_valid
= jiffies
;
780 memcpy(nc_path
->next_hop
, dst
, ETH_ALEN
);
781 memcpy(nc_path
->prev_hop
, src
, ETH_ALEN
);
783 batadv_dbg(BATADV_DBG_NC
, bat_priv
, "Adding nc_path %pM -> %pM\n",
787 /* Add nc_path to hash table */
788 hash_added
= batadv_hash_add(hash
, batadv_nc_hash_compare
,
789 batadv_nc_hash_choose
, &nc_path_key
,
790 &nc_path
->hash_entry
);
792 if (hash_added
< 0) {
801 * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
802 * selection of a receiver with slightly lower TQ than the other
803 * @tq: to be weighted tq value
805 static uint8_t batadv_nc_random_weight_tq(uint8_t tq
)
807 uint8_t rand_val
, rand_tq
;
809 get_random_bytes(&rand_val
, sizeof(rand_val
));
811 /* randomize the estimated packet loss (max TQ - estimated TQ) */
812 rand_tq
= rand_val
* (BATADV_TQ_MAX_VALUE
- tq
);
814 /* normalize the randomized packet loss */
815 rand_tq
/= BATADV_TQ_MAX_VALUE
;
817 /* convert to (randomized) estimated tq again */
818 return BATADV_TQ_MAX_VALUE
- rand_tq
;
822 * batadv_nc_memxor - XOR destination with source
823 * @dst: byte array to XOR into
824 * @src: byte array to XOR from
825 * @len: length of destination array
827 static void batadv_nc_memxor(char *dst
, const char *src
, unsigned int len
)
831 for (i
= 0; i
< len
; ++i
)
836 * batadv_nc_code_packets - code a received unicast_packet with an nc packet
837 * into a coded_packet and send it
838 * @bat_priv: the bat priv with all the soft interface information
839 * @skb: data skb to forward
840 * @ethhdr: pointer to the ethernet header inside the skb
841 * @nc_packet: structure containing the packet to the skb can be coded with
842 * @neigh_node: next hop to forward packet to
844 * Returns true if both packets are consumed, false otherwise.
846 static bool batadv_nc_code_packets(struct batadv_priv
*bat_priv
,
848 struct ethhdr
*ethhdr
,
849 struct batadv_nc_packet
*nc_packet
,
850 struct batadv_neigh_node
*neigh_node
)
852 uint8_t tq_weighted_neigh
, tq_weighted_coding
;
853 struct sk_buff
*skb_dest
, *skb_src
;
854 struct batadv_unicast_packet
*packet1
;
855 struct batadv_unicast_packet
*packet2
;
856 struct batadv_coded_packet
*coded_packet
;
857 struct batadv_neigh_node
*neigh_tmp
, *router_neigh
;
858 struct batadv_neigh_node
*router_coding
= NULL
;
859 uint8_t *first_source
, *first_dest
, *second_source
, *second_dest
;
860 __be32 packet_id1
, packet_id2
;
864 int unicast_size
= sizeof(*packet1
);
865 int coded_size
= sizeof(*coded_packet
);
866 int header_add
= coded_size
- unicast_size
;
868 router_neigh
= batadv_orig_node_get_router(neigh_node
->orig_node
);
872 neigh_tmp
= nc_packet
->neigh_node
;
873 router_coding
= batadv_orig_node_get_router(neigh_tmp
->orig_node
);
877 tq_weighted_neigh
= batadv_nc_random_weight_tq(router_neigh
->tq_avg
);
878 tq_weighted_coding
= batadv_nc_random_weight_tq(router_coding
->tq_avg
);
880 /* Select one destination for the MAC-header dst-field based on
881 * weighted TQ-values.
883 if (tq_weighted_neigh
>= tq_weighted_coding
) {
884 /* Destination from nc_packet is selected for MAC-header */
885 first_dest
= nc_packet
->nc_path
->next_hop
;
886 first_source
= nc_packet
->nc_path
->prev_hop
;
887 second_dest
= neigh_node
->addr
;
888 second_source
= ethhdr
->h_source
;
889 packet1
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
890 packet2
= (struct batadv_unicast_packet
*)skb
->data
;
891 packet_id1
= nc_packet
->packet_id
;
892 packet_id2
= batadv_skb_crc32(skb
,
893 skb
->data
+ sizeof(*packet2
));
895 /* Destination for skb is selected for MAC-header */
896 first_dest
= neigh_node
->addr
;
897 first_source
= ethhdr
->h_source
;
898 second_dest
= nc_packet
->nc_path
->next_hop
;
899 second_source
= nc_packet
->nc_path
->prev_hop
;
900 packet1
= (struct batadv_unicast_packet
*)skb
->data
;
901 packet2
= (struct batadv_unicast_packet
*)nc_packet
->skb
->data
;
902 packet_id1
= batadv_skb_crc32(skb
,
903 skb
->data
+ sizeof(*packet1
));
904 packet_id2
= nc_packet
->packet_id
;
907 /* Instead of zero padding the smallest data buffer, we
908 * code into the largest.
910 if (skb
->len
<= nc_packet
->skb
->len
) {
911 skb_dest
= nc_packet
->skb
;
915 skb_src
= nc_packet
->skb
;
918 /* coding_len is used when decoding the packet shorter packet */
919 coding_len
= skb_src
->len
- unicast_size
;
921 if (skb_linearize(skb_dest
) < 0 || skb_linearize(skb_src
) < 0)
924 skb_push(skb_dest
, header_add
);
926 coded_packet
= (struct batadv_coded_packet
*)skb_dest
->data
;
927 skb_reset_mac_header(skb_dest
);
929 coded_packet
->header
.packet_type
= BATADV_CODED
;
930 coded_packet
->header
.version
= BATADV_COMPAT_VERSION
;
931 coded_packet
->header
.ttl
= packet1
->header
.ttl
;
933 /* Info about first unicast packet */
934 memcpy(coded_packet
->first_source
, first_source
, ETH_ALEN
);
935 memcpy(coded_packet
->first_orig_dest
, packet1
->dest
, ETH_ALEN
);
936 coded_packet
->first_crc
= packet_id1
;
937 coded_packet
->first_ttvn
= packet1
->ttvn
;
939 /* Info about second unicast packet */
940 memcpy(coded_packet
->second_dest
, second_dest
, ETH_ALEN
);
941 memcpy(coded_packet
->second_source
, second_source
, ETH_ALEN
);
942 memcpy(coded_packet
->second_orig_dest
, packet2
->dest
, ETH_ALEN
);
943 coded_packet
->second_crc
= packet_id2
;
944 coded_packet
->second_ttl
= packet2
->header
.ttl
;
945 coded_packet
->second_ttvn
= packet2
->ttvn
;
946 coded_packet
->coded_len
= htons(coding_len
);
948 /* This is where the magic happens: Code skb_src into skb_dest */
949 batadv_nc_memxor(skb_dest
->data
+ coded_size
,
950 skb_src
->data
+ unicast_size
, coding_len
);
952 /* Update counters accordingly */
953 if (BATADV_SKB_CB(skb_src
)->decoded
&&
954 BATADV_SKB_CB(skb_dest
)->decoded
) {
955 /* Both packets are recoded */
956 count
= skb_src
->len
+ ETH_HLEN
;
957 count
+= skb_dest
->len
+ ETH_HLEN
;
958 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE
, 2);
959 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
, count
);
960 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
961 !BATADV_SKB_CB(skb_dest
)->decoded
) {
962 /* Both packets are newly coded */
963 count
= skb_src
->len
+ ETH_HLEN
;
964 count
+= skb_dest
->len
+ ETH_HLEN
;
965 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE
, 2);
966 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
, count
);
967 } else if (BATADV_SKB_CB(skb_src
)->decoded
&&
968 !BATADV_SKB_CB(skb_dest
)->decoded
) {
969 /* skb_src recoded and skb_dest is newly coded */
970 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
971 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
972 skb_src
->len
+ ETH_HLEN
);
973 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
974 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
975 skb_dest
->len
+ ETH_HLEN
);
976 } else if (!BATADV_SKB_CB(skb_src
)->decoded
&&
977 BATADV_SKB_CB(skb_dest
)->decoded
) {
978 /* skb_src is newly coded and skb_dest is recoded */
979 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_CODE
);
980 batadv_add_counter(bat_priv
, BATADV_CNT_NC_CODE_BYTES
,
981 skb_src
->len
+ ETH_HLEN
);
982 batadv_inc_counter(bat_priv
, BATADV_CNT_NC_RECODE
);
983 batadv_add_counter(bat_priv
, BATADV_CNT_NC_RECODE_BYTES
,
984 skb_dest
->len
+ ETH_HLEN
);
987 /* skb_src is now coded into skb_dest, so free it */
990 /* avoid duplicate free of skb from nc_packet */
991 nc_packet
->skb
= NULL
;
992 batadv_nc_packet_free(nc_packet
);
994 /* Send the coded packet and return true */
995 batadv_send_skb_packet(skb_dest
, neigh_node
->if_incoming
, first_dest
);
999 batadv_neigh_node_free_ref(router_neigh
);
1001 batadv_neigh_node_free_ref(router_coding
);
1006 * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
1007 * @skb: data skb to forward
1008 * @dst: destination mac address of the other skb to code with
1009 * @src: source mac address of skb
1011 * Whenever we network code a packet we have to check whether we received it in
1012 * a network coded form. If so, we may not be able to use it for coding because
1013 * some neighbors may also have received (overheard) the packet in the network
1014 * coded form without being able to decode it. It is hard to know which of the
1015 * neighboring nodes was able to decode the packet, therefore we can only
1016 * re-code the packet if the source of the previous encoded packet is involved.
1017 * Since the source encoded the packet we can be certain it has all necessary
1018 * decode information.
1020 * Returns true if coding of a decoded packet is allowed.
1022 static bool batadv_nc_skb_coding_possible(struct sk_buff
*skb
,
1023 uint8_t *dst
, uint8_t *src
)
1025 if (BATADV_SKB_CB(skb
)->decoded
&& !batadv_compare_eth(dst
, src
))
1032 * batadv_nc_path_search - Find the coding path matching in_nc_node and
1033 * out_nc_node to retrieve a buffered packet that can be used for coding.
1034 * @bat_priv: the bat priv with all the soft interface information
1035 * @in_nc_node: pointer to skb next hop's neighbor nc node
1036 * @out_nc_node: pointer to skb source's neighbor nc node
1037 * @skb: data skb to forward
1038 * @eth_dst: next hop mac address of skb
1040 * Returns true if coding of a decoded skb is allowed.
1042 static struct batadv_nc_packet
*
1043 batadv_nc_path_search(struct batadv_priv
*bat_priv
,
1044 struct batadv_nc_node
*in_nc_node
,
1045 struct batadv_nc_node
*out_nc_node
,
1046 struct sk_buff
*skb
,
1049 struct batadv_nc_path
*nc_path
, nc_path_key
;
1050 struct batadv_nc_packet
*nc_packet_out
= NULL
;
1051 struct batadv_nc_packet
*nc_packet
, *nc_packet_tmp
;
1052 struct batadv_hashtable
*hash
= bat_priv
->nc
.coding_hash
;
1058 /* Create almost path key */
1059 batadv_nc_hash_key_gen(&nc_path_key
, in_nc_node
->addr
,
1061 idx
= batadv_nc_hash_choose(&nc_path_key
, hash
->size
);
1063 /* Check for coding opportunities in this nc_path */
1065 hlist_for_each_entry_rcu(nc_path
, &hash
->table
[idx
], hash_entry
) {
1066 if (!batadv_compare_eth(nc_path
->prev_hop
, in_nc_node
->addr
))
1069 if (!batadv_compare_eth(nc_path
->next_hop
, out_nc_node
->addr
))
1072 spin_lock_bh(&nc_path
->packet_list_lock
);
1073 if (list_empty(&nc_path
->packet_list
)) {
1074 spin_unlock_bh(&nc_path
->packet_list_lock
);
1078 list_for_each_entry_safe(nc_packet
, nc_packet_tmp
,
1079 &nc_path
->packet_list
, list
) {
1080 if (!batadv_nc_skb_coding_possible(nc_packet
->skb
,
1085 /* Coding opportunity is found! */
1086 list_del(&nc_packet
->list
);
1087 nc_packet_out
= nc_packet
;
1091 spin_unlock_bh(&nc_path
->packet_list_lock
);
1096 return nc_packet_out
;
1100 * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
1101 * skb's sender (may be equal to the originator).
1102 * @bat_priv: the bat priv with all the soft interface information
1103 * @skb: data skb to forward
1104 * @eth_dst: next hop mac address of skb
1105 * @eth_src: source mac address of skb
1106 * @in_nc_node: pointer to skb next hop's neighbor nc node
1108 * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
1110 static struct batadv_nc_packet
*
1111 batadv_nc_skb_src_search(struct batadv_priv
*bat_priv
,
1112 struct sk_buff
*skb
,
1115 struct batadv_nc_node
*in_nc_node
)
1117 struct batadv_orig_node
*orig_node
;
1118 struct batadv_nc_node
*out_nc_node
;
1119 struct batadv_nc_packet
*nc_packet
= NULL
;
1121 orig_node
= batadv_orig_hash_find(bat_priv
, eth_src
);
1126 list_for_each_entry_rcu(out_nc_node
,
1127 &orig_node
->out_coding_list
, list
) {
1128 /* Check if the skb is decoded and if recoding is possible */
1129 if (!batadv_nc_skb_coding_possible(skb
,
1130 out_nc_node
->addr
, eth_src
))
1133 /* Search for an opportunity in this nc_path */
1134 nc_packet
= batadv_nc_path_search(bat_priv
, in_nc_node
,
1135 out_nc_node
, skb
, eth_dst
);
1141 batadv_orig_node_free_ref(orig_node
);
1146 * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
1147 * @skb: data skb to forward
1148 * @neigh_node: next hop to forward packet to
1149 * @ethhdr: pointer to the ethernet header inside the skb
1151 * Loops through list of neighboring nodes the next hop has a good connection to
1152 * (receives OGMs with a sufficient quality). We need to find a neighbor of our
1153 * next hop that potentially sent a packet which our next hop also received
1154 * (overheard) and has stored for later decoding.
1156 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1158 static bool batadv_nc_skb_dst_search(struct sk_buff
*skb
,
1159 struct batadv_neigh_node
*neigh_node
,
1160 struct ethhdr
*ethhdr
)
1162 struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1163 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1164 struct batadv_orig_node
*orig_node
= neigh_node
->orig_node
;
1165 struct batadv_nc_node
*nc_node
;
1166 struct batadv_nc_packet
*nc_packet
= NULL
;
1169 list_for_each_entry_rcu(nc_node
, &orig_node
->in_coding_list
, list
) {
1170 /* Search for coding opportunity with this in_nc_node */
1171 nc_packet
= batadv_nc_skb_src_search(bat_priv
, skb
,
1173 ethhdr
->h_source
, nc_node
);
1175 /* Opportunity was found, so stop searching */
1184 /* Code and send packets */
1185 if (batadv_nc_code_packets(bat_priv
, skb
, ethhdr
, nc_packet
,
1189 /* out of mem ? Coding failed - we have to free the buffered packet
1190 * to avoid memleaks. The skb passed as argument will be dealt with
1191 * by the calling function.
1193 batadv_nc_send_packet(nc_packet
);
1198 * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
1199 * @skb: skb to add to path
1200 * @nc_path: path to add skb to
1201 * @neigh_node: next hop to forward packet to
1202 * @packet_id: checksum to identify packet
1204 * Returns true if the packet was buffered or false in case of an error.
1206 static bool batadv_nc_skb_add_to_path(struct sk_buff
*skb
,
1207 struct batadv_nc_path
*nc_path
,
1208 struct batadv_neigh_node
*neigh_node
,
1211 struct batadv_nc_packet
*nc_packet
;
1213 nc_packet
= kzalloc(sizeof(*nc_packet
), GFP_ATOMIC
);
1217 /* Initialize nc_packet */
1218 nc_packet
->timestamp
= jiffies
;
1219 nc_packet
->packet_id
= packet_id
;
1220 nc_packet
->skb
= skb
;
1221 nc_packet
->neigh_node
= neigh_node
;
1222 nc_packet
->nc_path
= nc_path
;
1224 /* Add coding packet to list */
1225 spin_lock_bh(&nc_path
->packet_list_lock
);
1226 list_add_tail(&nc_packet
->list
, &nc_path
->packet_list
);
1227 spin_unlock_bh(&nc_path
->packet_list_lock
);
1233 * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
1235 * @skb: data skb to forward
1236 * @neigh_node: next hop to forward packet to
1237 * @ethhdr: pointer to the ethernet header inside the skb
1239 * Returns true if the skb was consumed (encoded packet sent) or false otherwise
1241 bool batadv_nc_skb_forward(struct sk_buff
*skb
,
1242 struct batadv_neigh_node
*neigh_node
,
1243 struct ethhdr
*ethhdr
)
1245 const struct net_device
*netdev
= neigh_node
->if_incoming
->soft_iface
;
1246 struct batadv_priv
*bat_priv
= netdev_priv(netdev
);
1247 struct batadv_unicast_packet
*packet
;
1248 struct batadv_nc_path
*nc_path
;
1252 /* Check if network coding is enabled */
1253 if (!atomic_read(&bat_priv
->network_coding
))
1256 /* We only handle unicast packets */
1257 payload
= skb_network_header(skb
);
1258 packet
= (struct batadv_unicast_packet
*)payload
;
1259 if (packet
->header
.packet_type
!= BATADV_UNICAST
)
1262 /* Try to find a coding opportunity and send the skb if one is found */
1263 if (batadv_nc_skb_dst_search(skb
, neigh_node
, ethhdr
))
1266 /* Find or create a nc_path for this src-dst pair */
1267 nc_path
= batadv_nc_get_path(bat_priv
,
1268 bat_priv
->nc
.coding_hash
,
1275 /* Add skb to nc_path */
1276 packet_id
= batadv_skb_crc32(skb
, payload
+ sizeof(*packet
));
1277 if (!batadv_nc_skb_add_to_path(skb
, nc_path
, neigh_node
, packet_id
))
1280 /* Packet is consumed */
1284 batadv_nc_path_free_ref(nc_path
);
1286 /* Packet is not consumed */
1291 * batadv_nc_free - clean up network coding memory
1292 * @bat_priv: the bat priv with all the soft interface information
1294 void batadv_nc_free(struct batadv_priv
*bat_priv
)
1296 cancel_delayed_work_sync(&bat_priv
->nc
.work
);
1297 batadv_nc_purge_paths(bat_priv
, bat_priv
->nc
.coding_hash
, NULL
);
1298 batadv_hash_destroy(bat_priv
->nc
.coding_hash
);
1302 * batadv_nc_nodes_seq_print_text - print the nc node information
1303 * @seq: seq file to print on
1306 int batadv_nc_nodes_seq_print_text(struct seq_file
*seq
, void *offset
)
1308 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1309 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1310 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1311 struct batadv_hard_iface
*primary_if
;
1312 struct hlist_head
*head
;
1313 struct batadv_orig_node
*orig_node
;
1314 struct batadv_nc_node
*nc_node
;
1317 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1321 /* Traverse list of originators */
1322 for (i
= 0; i
< hash
->size
; i
++) {
1323 head
= &hash
->table
[i
];
1325 /* For each orig_node in this bin */
1327 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1328 seq_printf(seq
, "Node: %pM\n", orig_node
->orig
);
1330 seq_printf(seq
, " Ingoing: ");
1331 /* For each in_nc_node to this orig_node */
1332 list_for_each_entry_rcu(nc_node
,
1333 &orig_node
->in_coding_list
,
1335 seq_printf(seq
, "%pM ",
1337 seq_printf(seq
, "\n");
1339 seq_printf(seq
, " Outgoing: ");
1340 /* For out_nc_node to this orig_node */
1341 list_for_each_entry_rcu(nc_node
,
1342 &orig_node
->out_coding_list
,
1344 seq_printf(seq
, "%pM ",
1346 seq_printf(seq
, "\n\n");
1353 batadv_hardif_free_ref(primary_if
);
1358 * batadv_nc_init_debugfs - create nc folder and related files in debugfs
1359 * @bat_priv: the bat priv with all the soft interface information
1361 int batadv_nc_init_debugfs(struct batadv_priv
*bat_priv
)
1363 struct dentry
*nc_dir
, *file
;
1365 nc_dir
= debugfs_create_dir("nc", bat_priv
->debug_dir
);
1369 file
= debugfs_create_u8("min_tq", S_IRUGO
| S_IWUSR
, nc_dir
,
1370 &bat_priv
->nc
.min_tq
);
1374 file
= debugfs_create_u32("max_fwd_delay", S_IRUGO
| S_IWUSR
, nc_dir
,
1375 &bat_priv
->nc
.max_fwd_delay
);