network-coding.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /* Copyright (C) 2012-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Martin Hundebøll, Jeppe Ledet-Pedersen
  4. *
  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.
  8. *
  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.
  13. *
  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
  17. * 02110-1301, USA
  18. */
  19. #include <linux/debugfs.h>
  20. #include "main.h"
  21. #include "hash.h"
  22. #include "network-coding.h"
  23. #include "send.h"
  24. #include "originator.h"
  25. #include "hard-interface.h"
  26. static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
  27. static void batadv_nc_worker(struct work_struct *work);
  28. /**
  29. * batadv_nc_start_timer - initialise the nc periodic worker
  30. * @bat_priv: the bat priv with all the soft interface information
  31. */
  32. static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
  33. {
  34. queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
  35. msecs_to_jiffies(10));
  36. }
  37. /**
  38. * batadv_nc_init - initialise coding hash table and start house keeping
  39. * @bat_priv: the bat priv with all the soft interface information
  40. */
  41. int batadv_nc_init(struct batadv_priv *bat_priv)
  42. {
  43. bat_priv->nc.timestamp_fwd_flush = jiffies;
  44. if (bat_priv->nc.coding_hash)
  45. return 0;
  46. bat_priv->nc.coding_hash = batadv_hash_new(128);
  47. if (!bat_priv->nc.coding_hash)
  48. goto err;
  49. batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
  50. &batadv_nc_coding_hash_lock_class_key);
  51. INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
  52. batadv_nc_start_timer(bat_priv);
  53. return 0;
  54. err:
  55. return -ENOMEM;
  56. }
  57. /**
  58. * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
  59. * @bat_priv: the bat priv with all the soft interface information
  60. */
  61. void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
  62. {
  63. atomic_set(&bat_priv->network_coding, 1);
  64. bat_priv->nc.min_tq = 200;
  65. bat_priv->nc.max_fwd_delay = 10;
  66. }
  67. /**
  68. * batadv_nc_init_orig - initialise the nc fields of an orig_node
  69. * @orig_node: the orig_node which is going to be initialised
  70. */
  71. void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
  72. {
  73. INIT_LIST_HEAD(&orig_node->in_coding_list);
  74. INIT_LIST_HEAD(&orig_node->out_coding_list);
  75. spin_lock_init(&orig_node->in_coding_list_lock);
  76. spin_lock_init(&orig_node->out_coding_list_lock);
  77. }
  78. /**
  79. * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
  80. * its refcount on the orig_node
  81. * @rcu: rcu pointer of the nc node
  82. */
  83. static void batadv_nc_node_free_rcu(struct rcu_head *rcu)
  84. {
  85. struct batadv_nc_node *nc_node;
  86. nc_node = container_of(rcu, struct batadv_nc_node, rcu);
  87. batadv_orig_node_free_ref(nc_node->orig_node);
  88. kfree(nc_node);
  89. }
  90. /**
  91. * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
  92. * frees it
  93. * @nc_node: the nc node to free
  94. */
  95. static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node)
  96. {
  97. if (atomic_dec_and_test(&nc_node->refcount))
  98. call_rcu(&nc_node->rcu, batadv_nc_node_free_rcu);
  99. }
  100. /**
  101. * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
  102. * frees it
  103. * @nc_path: the nc node to free
  104. */
  105. static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path)
  106. {
  107. if (atomic_dec_and_test(&nc_path->refcount))
  108. kfree_rcu(nc_path, rcu);
  109. }
  110. /**
  111. * batadv_nc_packet_free - frees nc packet
  112. * @nc_packet: the nc packet to free
  113. */
  114. static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet)
  115. {
  116. if (nc_packet->skb)
  117. kfree_skb(nc_packet->skb);
  118. batadv_nc_path_free_ref(nc_packet->nc_path);
  119. kfree(nc_packet);
  120. }
  121. /**
  122. * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
  123. * @bat_priv: the bat priv with all the soft interface information
  124. * @nc_node: the nc node to check
  125. *
  126. * Returns true if the entry has to be purged now, false otherwise
  127. */
  128. static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
  129. struct batadv_nc_node *nc_node)
  130. {
  131. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  132. return true;
  133. return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
  134. }
  135. /**
  136. * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
  137. * @bat_priv: the bat priv with all the soft interface information
  138. * @nc_path: the nc path to check
  139. *
  140. * Returns true if the entry has to be purged now, false otherwise
  141. */
  142. static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
  143. struct batadv_nc_path *nc_path)
  144. {
  145. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  146. return true;
  147. /* purge the path when no packets has been added for 10 times the
  148. * max_fwd_delay time
  149. */
  150. return batadv_has_timed_out(nc_path->last_valid,
  151. bat_priv->nc.max_fwd_delay * 10);
  152. }
  153. /**
  154. * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
  155. * entries
  156. * @bat_priv: the bat priv with all the soft interface information
  157. * @list: list of nc nodes
  158. * @lock: nc node list lock
  159. * @to_purge: function in charge to decide whether an entry has to be purged or
  160. * not. This function takes the nc node as argument and has to return
  161. * a boolean value: true if the entry has to be deleted, false
  162. * otherwise
  163. */
  164. static void
  165. batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
  166. struct list_head *list,
  167. spinlock_t *lock,
  168. bool (*to_purge)(struct batadv_priv *,
  169. struct batadv_nc_node *))
  170. {
  171. struct batadv_nc_node *nc_node, *nc_node_tmp;
  172. /* For each nc_node in list */
  173. spin_lock_bh(lock);
  174. list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
  175. /* if an helper function has been passed as parameter,
  176. * ask it if the entry has to be purged or not
  177. */
  178. if (to_purge && !to_purge(bat_priv, nc_node))
  179. continue;
  180. batadv_dbg(BATADV_DBG_NC, bat_priv,
  181. "Removing nc_node %pM -> %pM\n",
  182. nc_node->addr, nc_node->orig_node->orig);
  183. list_del_rcu(&nc_node->list);
  184. batadv_nc_node_free_ref(nc_node);
  185. }
  186. spin_unlock_bh(lock);
  187. }
  188. /**
  189. * batadv_nc_purge_orig - purges all nc node data attached of the given
  190. * originator
  191. * @bat_priv: the bat priv with all the soft interface information
  192. * @orig_node: orig_node with the nc node entries to be purged
  193. * @to_purge: function in charge to decide whether an entry has to be purged or
  194. * not. This function takes the nc node as argument and has to return
  195. * a boolean value: true is the entry has to be deleted, false
  196. * otherwise
  197. */
  198. void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
  199. struct batadv_orig_node *orig_node,
  200. bool (*to_purge)(struct batadv_priv *,
  201. struct batadv_nc_node *))
  202. {
  203. /* Check ingoing nc_node's of this orig_node */
  204. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
  205. &orig_node->in_coding_list_lock,
  206. to_purge);
  207. /* Check outgoing nc_node's of this orig_node */
  208. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
  209. &orig_node->out_coding_list_lock,
  210. to_purge);
  211. }
  212. /**
  213. * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
  214. * have timed out nc nodes
  215. * @bat_priv: the bat priv with all the soft interface information
  216. */
  217. static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
  218. {
  219. struct batadv_hashtable *hash = bat_priv->orig_hash;
  220. struct hlist_head *head;
  221. struct batadv_orig_node *orig_node;
  222. uint32_t i;
  223. if (!hash)
  224. return;
  225. /* For each orig_node */
  226. for (i = 0; i < hash->size; i++) {
  227. head = &hash->table[i];
  228. rcu_read_lock();
  229. hlist_for_each_entry_rcu(orig_node, head, hash_entry)
  230. batadv_nc_purge_orig(bat_priv, orig_node,
  231. batadv_nc_to_purge_nc_node);
  232. rcu_read_unlock();
  233. }
  234. }
  235. /**
  236. * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
  237. * unused ones
  238. * @bat_priv: the bat priv with all the soft interface information
  239. * @hash: hash table containing the nc paths to check
  240. * @to_purge: function in charge to decide whether an entry has to be purged or
  241. * not. This function takes the nc node as argument and has to return
  242. * a boolean value: true is the entry has to be deleted, false
  243. * otherwise
  244. */
  245. static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
  246. struct batadv_hashtable *hash,
  247. bool (*to_purge)(struct batadv_priv *,
  248. struct batadv_nc_path *))
  249. {
  250. struct hlist_head *head;
  251. struct hlist_node *node_tmp;
  252. struct batadv_nc_path *nc_path;
  253. spinlock_t *lock; /* Protects lists in hash */
  254. uint32_t i;
  255. for (i = 0; i < hash->size; i++) {
  256. head = &hash->table[i];
  257. lock = &hash->list_locks[i];
  258. /* For each nc_path in this bin */
  259. spin_lock_bh(lock);
  260. hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
  261. /* if an helper function has been passed as parameter,
  262. * ask it if the entry has to be purged or not
  263. */
  264. if (to_purge && !to_purge(bat_priv, nc_path))
  265. continue;
  266. /* purging an non-empty nc_path should never happen, but
  267. * is observed under high CPU load. Delay the purging
  268. * until next iteration to allow the packet_list to be
  269. * emptied first.
  270. */
  271. if (!unlikely(list_empty(&nc_path->packet_list))) {
  272. net_ratelimited_function(printk,
  273. KERN_WARNING
  274. "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
  275. nc_path->prev_hop,
  276. nc_path->next_hop);
  277. continue;
  278. }
  279. /* nc_path is unused, so remove it */
  280. batadv_dbg(BATADV_DBG_NC, bat_priv,
  281. "Remove nc_path %pM -> %pM\n",
  282. nc_path->prev_hop, nc_path->next_hop);
  283. hlist_del_rcu(&nc_path->hash_entry);
  284. batadv_nc_path_free_ref(nc_path);
  285. }
  286. spin_unlock_bh(lock);
  287. }
  288. }
  289. /**
  290. * batadv_nc_hash_key_gen - computes the nc_path hash key
  291. * @key: buffer to hold the final hash key
  292. * @src: source ethernet mac address going into the hash key
  293. * @dst: destination ethernet mac address going into the hash key
  294. */
  295. static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
  296. const char *dst)
  297. {
  298. memcpy(key->prev_hop, src, sizeof(key->prev_hop));
  299. memcpy(key->next_hop, dst, sizeof(key->next_hop));
  300. }
  301. /**
  302. * batadv_nc_hash_choose - compute the hash value for an nc path
  303. * @data: data to hash
  304. * @size: size of the hash table
  305. *
  306. * Returns the selected index in the hash table for the given data.
  307. */
  308. static uint32_t batadv_nc_hash_choose(const void *data, uint32_t size)
  309. {
  310. const struct batadv_nc_path *nc_path = data;
  311. uint32_t hash = 0;
  312. hash = batadv_hash_bytes(hash, &nc_path->prev_hop,
  313. sizeof(nc_path->prev_hop));
  314. hash = batadv_hash_bytes(hash, &nc_path->next_hop,
  315. sizeof(nc_path->next_hop));
  316. hash += (hash << 3);
  317. hash ^= (hash >> 11);
  318. hash += (hash << 15);
  319. return hash % size;
  320. }
  321. /**
  322. * batadv_nc_hash_compare - comparing function used in the network coding hash
  323. * tables
  324. * @node: node in the local table
  325. * @data2: second object to compare the node to
  326. *
  327. * Returns 1 if the two entry are the same, 0 otherwise
  328. */
  329. static int batadv_nc_hash_compare(const struct hlist_node *node,
  330. const void *data2)
  331. {
  332. const struct batadv_nc_path *nc_path1, *nc_path2;
  333. nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
  334. nc_path2 = data2;
  335. /* Return 1 if the two keys are identical */
  336. if (memcmp(nc_path1->prev_hop, nc_path2->prev_hop,
  337. sizeof(nc_path1->prev_hop)) != 0)
  338. return 0;
  339. if (memcmp(nc_path1->next_hop, nc_path2->next_hop,
  340. sizeof(nc_path1->next_hop)) != 0)
  341. return 0;
  342. return 1;
  343. }
  344. /**
  345. * batadv_nc_hash_find - search for an existing nc path and return it
  346. * @hash: hash table containing the nc path
  347. * @data: search key
  348. *
  349. * Returns the nc_path if found, NULL otherwise.
  350. */
  351. static struct batadv_nc_path *
  352. batadv_nc_hash_find(struct batadv_hashtable *hash,
  353. void *data)
  354. {
  355. struct hlist_head *head;
  356. struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
  357. int index;
  358. if (!hash)
  359. return NULL;
  360. index = batadv_nc_hash_choose(data, hash->size);
  361. head = &hash->table[index];
  362. rcu_read_lock();
  363. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  364. if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
  365. continue;
  366. if (!atomic_inc_not_zero(&nc_path->refcount))
  367. continue;
  368. nc_path_tmp = nc_path;
  369. break;
  370. }
  371. rcu_read_unlock();
  372. return nc_path_tmp;
  373. }
  374. /**
  375. * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
  376. * @nc_packet: the nc packet to send
  377. */
  378. static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
  379. {
  380. batadv_send_skb_packet(nc_packet->skb,
  381. nc_packet->neigh_node->if_incoming,
  382. nc_packet->nc_path->next_hop);
  383. nc_packet->skb = NULL;
  384. batadv_nc_packet_free(nc_packet);
  385. }
  386. /**
  387. * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
  388. * @bat_priv: the bat priv with all the soft interface information
  389. * @nc_path: the nc path the packet belongs to
  390. * @nc_packet: the nc packet to be checked
  391. *
  392. * Checks whether the given nc packet has hit its forward timeout. If so, the
  393. * packet is no longer delayed, immediately sent and the entry deleted from the
  394. * queue. Has to be called with the appropriate locks.
  395. *
  396. * Returns false as soon as the entry in the fifo queue has not been timed out
  397. * yet and true otherwise.
  398. */
  399. static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
  400. struct batadv_nc_path *nc_path,
  401. struct batadv_nc_packet *nc_packet)
  402. {
  403. unsigned long timeout = bat_priv->nc.max_fwd_delay;
  404. /* Packets are added to tail, so the remaining packets did not time
  405. * out and we can stop processing the current queue
  406. */
  407. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  408. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  409. return false;
  410. /* Send packet */
  411. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  412. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  413. nc_packet->skb->len + ETH_HLEN);
  414. list_del(&nc_packet->list);
  415. batadv_nc_send_packet(nc_packet);
  416. return true;
  417. }
  418. /**
  419. * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
  420. * nc packets
  421. * @bat_priv: the bat priv with all the soft interface information
  422. * @hash: to be processed hash table
  423. * @process_fn: Function called to process given nc packet. Should return true
  424. * to encourage this function to proceed with the next packet.
  425. * Otherwise the rest of the current queue is skipped.
  426. */
  427. static void
  428. batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
  429. struct batadv_hashtable *hash,
  430. bool (*process_fn)(struct batadv_priv *,
  431. struct batadv_nc_path *,
  432. struct batadv_nc_packet *))
  433. {
  434. struct hlist_head *head;
  435. struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  436. struct batadv_nc_path *nc_path;
  437. bool ret;
  438. int i;
  439. if (!hash)
  440. return;
  441. /* Loop hash table bins */
  442. for (i = 0; i < hash->size; i++) {
  443. head = &hash->table[i];
  444. /* Loop coding paths */
  445. rcu_read_lock();
  446. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  447. /* Loop packets */
  448. spin_lock_bh(&nc_path->packet_list_lock);
  449. list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  450. &nc_path->packet_list, list) {
  451. ret = process_fn(bat_priv, nc_path, nc_packet);
  452. if (!ret)
  453. break;
  454. }
  455. spin_unlock_bh(&nc_path->packet_list_lock);
  456. }
  457. rcu_read_unlock();
  458. }
  459. }
  460. /**
  461. * batadv_nc_worker - periodic task for house keeping related to network coding
  462. * @work: kernel work struct
  463. */
  464. static void batadv_nc_worker(struct work_struct *work)
  465. {
  466. struct delayed_work *delayed_work;
  467. struct batadv_priv_nc *priv_nc;
  468. struct batadv_priv *bat_priv;
  469. unsigned long timeout;
  470. delayed_work = container_of(work, struct delayed_work, work);
  471. priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
  472. bat_priv = container_of(priv_nc, struct batadv_priv, nc);
  473. batadv_nc_purge_orig_hash(bat_priv);
  474. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
  475. batadv_nc_to_purge_nc_path_coding);
  476. timeout = bat_priv->nc.max_fwd_delay;
  477. if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
  478. batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
  479. batadv_nc_fwd_flush);
  480. bat_priv->nc.timestamp_fwd_flush = jiffies;
  481. }
  482. /* Schedule a new check */
  483. batadv_nc_start_timer(bat_priv);
  484. }
  485. /**
  486. * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
  487. * coding or not
  488. * @bat_priv: the bat priv with all the soft interface information
  489. * @orig_node: neighboring orig node which may be used as nc candidate
  490. * @ogm_packet: incoming ogm packet also used for the checks
  491. *
  492. * Returns true if:
  493. * 1) The OGM must have the most recent sequence number.
  494. * 2) The TTL must be decremented by one and only one.
  495. * 3) The OGM must be received from the first hop from orig_node.
  496. * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
  497. */
  498. static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
  499. struct batadv_orig_node *orig_node,
  500. struct batadv_ogm_packet *ogm_packet)
  501. {
  502. if (orig_node->last_real_seqno != ogm_packet->seqno)
  503. return false;
  504. if (orig_node->last_ttl != ogm_packet->header.ttl + 1)
  505. return false;
  506. if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
  507. return false;
  508. if (ogm_packet->tq < bat_priv->nc.min_tq)
  509. return false;
  510. return true;
  511. }
  512. /**
  513. * batadv_nc_find_nc_node - search for an existing nc node and return it
  514. * @orig_node: orig node originating the ogm packet
  515. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  516. * (can be equal to orig_node)
  517. * @in_coding: traverse incoming or outgoing network coding list
  518. *
  519. * Returns the nc_node if found, NULL otherwise.
  520. */
  521. static struct batadv_nc_node
  522. *batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
  523. struct batadv_orig_node *orig_neigh_node,
  524. bool in_coding)
  525. {
  526. struct batadv_nc_node *nc_node, *nc_node_out = NULL;
  527. struct list_head *list;
  528. if (in_coding)
  529. list = &orig_neigh_node->in_coding_list;
  530. else
  531. list = &orig_neigh_node->out_coding_list;
  532. /* Traverse list of nc_nodes to orig_node */
  533. rcu_read_lock();
  534. list_for_each_entry_rcu(nc_node, list, list) {
  535. if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
  536. continue;
  537. if (!atomic_inc_not_zero(&nc_node->refcount))
  538. continue;
  539. /* Found a match */
  540. nc_node_out = nc_node;
  541. break;
  542. }
  543. rcu_read_unlock();
  544. return nc_node_out;
  545. }
  546. /**
  547. * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
  548. * not found
  549. * @bat_priv: the bat priv with all the soft interface information
  550. * @orig_node: orig node originating the ogm packet
  551. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  552. * (can be equal to orig_node)
  553. * @in_coding: traverse incoming or outgoing network coding list
  554. *
  555. * Returns the nc_node if found or created, NULL in case of an error.
  556. */
  557. static struct batadv_nc_node
  558. *batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
  559. struct batadv_orig_node *orig_node,
  560. struct batadv_orig_node *orig_neigh_node,
  561. bool in_coding)
  562. {
  563. struct batadv_nc_node *nc_node;
  564. spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
  565. struct list_head *list;
  566. /* Check if nc_node is already added */
  567. nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
  568. /* Node found */
  569. if (nc_node)
  570. return nc_node;
  571. nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
  572. if (!nc_node)
  573. return NULL;
  574. if (!atomic_inc_not_zero(&orig_neigh_node->refcount))
  575. goto free;
  576. /* Initialize nc_node */
  577. INIT_LIST_HEAD(&nc_node->list);
  578. memcpy(nc_node->addr, orig_node->orig, ETH_ALEN);
  579. nc_node->orig_node = orig_neigh_node;
  580. atomic_set(&nc_node->refcount, 2);
  581. /* Select ingoing or outgoing coding node */
  582. if (in_coding) {
  583. lock = &orig_neigh_node->in_coding_list_lock;
  584. list = &orig_neigh_node->in_coding_list;
  585. } else {
  586. lock = &orig_neigh_node->out_coding_list_lock;
  587. list = &orig_neigh_node->out_coding_list;
  588. }
  589. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
  590. nc_node->addr, nc_node->orig_node->orig);
  591. /* Add nc_node to orig_node */
  592. spin_lock_bh(lock);
  593. list_add_tail_rcu(&nc_node->list, list);
  594. spin_unlock_bh(lock);
  595. return nc_node;
  596. free:
  597. kfree(nc_node);
  598. return NULL;
  599. }
  600. /**
  601. * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
  602. * (best called on incoming OGMs)
  603. * @bat_priv: the bat priv with all the soft interface information
  604. * @orig_node: orig node originating the ogm packet
  605. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  606. * (can be equal to orig_node)
  607. * @ogm_packet: incoming ogm packet
  608. * @is_single_hop_neigh: orig_node is a single hop neighbor
  609. */
  610. void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
  611. struct batadv_orig_node *orig_node,
  612. struct batadv_orig_node *orig_neigh_node,
  613. struct batadv_ogm_packet *ogm_packet,
  614. int is_single_hop_neigh)
  615. {
  616. struct batadv_nc_node *in_nc_node = NULL, *out_nc_node = NULL;
  617. /* Check if network coding is enabled */
  618. if (!atomic_read(&bat_priv->network_coding))
  619. goto out;
  620. /* accept ogms from 'good' neighbors and single hop neighbors */
  621. if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
  622. !is_single_hop_neigh)
  623. goto out;
  624. /* Add orig_node as in_nc_node on hop */
  625. in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
  626. orig_neigh_node, true);
  627. if (!in_nc_node)
  628. goto out;
  629. in_nc_node->last_seen = jiffies;
  630. /* Add hop as out_nc_node on orig_node */
  631. out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
  632. orig_node, false);
  633. if (!out_nc_node)
  634. goto out;
  635. out_nc_node->last_seen = jiffies;
  636. out:
  637. if (in_nc_node)
  638. batadv_nc_node_free_ref(in_nc_node);
  639. if (out_nc_node)
  640. batadv_nc_node_free_ref(out_nc_node);
  641. }
  642. /**
  643. * batadv_nc_get_path - get existing nc_path or allocate a new one
  644. * @bat_priv: the bat priv with all the soft interface information
  645. * @hash: hash table containing the nc path
  646. * @src: ethernet source address - first half of the nc path search key
  647. * @dst: ethernet destination address - second half of the nc path search key
  648. *
  649. * Returns pointer to nc_path if the path was found or created, returns NULL
  650. * on error.
  651. */
  652. static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
  653. struct batadv_hashtable *hash,
  654. uint8_t *src,
  655. uint8_t *dst)
  656. {
  657. int hash_added;
  658. struct batadv_nc_path *nc_path, nc_path_key;
  659. batadv_nc_hash_key_gen(&nc_path_key, src, dst);
  660. /* Search for existing nc_path */
  661. nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
  662. if (nc_path) {
  663. /* Set timestamp to delay removal of nc_path */
  664. nc_path->last_valid = jiffies;
  665. return nc_path;
  666. }
  667. /* No existing nc_path was found; create a new */
  668. nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
  669. if (!nc_path)
  670. return NULL;
  671. /* Initialize nc_path */
  672. INIT_LIST_HEAD(&nc_path->packet_list);
  673. spin_lock_init(&nc_path->packet_list_lock);
  674. atomic_set(&nc_path->refcount, 2);
  675. nc_path->last_valid = jiffies;
  676. memcpy(nc_path->next_hop, dst, ETH_ALEN);
  677. memcpy(nc_path->prev_hop, src, ETH_ALEN);
  678. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
  679. nc_path->prev_hop,
  680. nc_path->next_hop);
  681. /* Add nc_path to hash table */
  682. hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
  683. batadv_nc_hash_choose, &nc_path_key,
  684. &nc_path->hash_entry);
  685. if (hash_added < 0) {
  686. kfree(nc_path);
  687. return NULL;
  688. }
  689. return nc_path;
  690. }
  691. /**
  692. * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
  693. * selection of a receiver with slightly lower TQ than the other
  694. * @tq: to be weighted tq value
  695. */
  696. static uint8_t batadv_nc_random_weight_tq(uint8_t tq)
  697. {
  698. uint8_t rand_val, rand_tq;
  699. get_random_bytes(&rand_val, sizeof(rand_val));
  700. /* randomize the estimated packet loss (max TQ - estimated TQ) */
  701. rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq);
  702. /* normalize the randomized packet loss */
  703. rand_tq /= BATADV_TQ_MAX_VALUE;
  704. /* convert to (randomized) estimated tq again */
  705. return BATADV_TQ_MAX_VALUE - rand_tq;
  706. }
  707. /**
  708. * batadv_nc_memxor - XOR destination with source
  709. * @dst: byte array to XOR into
  710. * @src: byte array to XOR from
  711. * @len: length of destination array
  712. */
  713. static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
  714. {
  715. unsigned int i;
  716. for (i = 0; i < len; ++i)
  717. dst[i] ^= src[i];
  718. }
  719. /**
  720. * batadv_nc_code_packets - code a received unicast_packet with an nc packet
  721. * into a coded_packet and send it
  722. * @bat_priv: the bat priv with all the soft interface information
  723. * @skb: data skb to forward
  724. * @ethhdr: pointer to the ethernet header inside the skb
  725. * @nc_packet: structure containing the packet to the skb can be coded with
  726. * @neigh_node: next hop to forward packet to
  727. *
  728. * Returns true if both packets are consumed, false otherwise.
  729. */
  730. static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
  731. struct sk_buff *skb,
  732. struct ethhdr *ethhdr,
  733. struct batadv_nc_packet *nc_packet,
  734. struct batadv_neigh_node *neigh_node)
  735. {
  736. uint8_t tq_weighted_neigh, tq_weighted_coding;
  737. struct sk_buff *skb_dest, *skb_src;
  738. struct batadv_unicast_packet *packet1;
  739. struct batadv_unicast_packet *packet2;
  740. struct batadv_coded_packet *coded_packet;
  741. struct batadv_neigh_node *neigh_tmp, *router_neigh;
  742. struct batadv_neigh_node *router_coding = NULL;
  743. uint8_t *first_source, *first_dest, *second_source, *second_dest;
  744. __be32 packet_id1, packet_id2;
  745. size_t count;
  746. bool res = false;
  747. int coding_len;
  748. int unicast_size = sizeof(*packet1);
  749. int coded_size = sizeof(*coded_packet);
  750. int header_add = coded_size - unicast_size;
  751. router_neigh = batadv_orig_node_get_router(neigh_node->orig_node);
  752. if (!router_neigh)
  753. goto out;
  754. neigh_tmp = nc_packet->neigh_node;
  755. router_coding = batadv_orig_node_get_router(neigh_tmp->orig_node);
  756. if (!router_coding)
  757. goto out;
  758. tq_weighted_neigh = batadv_nc_random_weight_tq(router_neigh->tq_avg);
  759. tq_weighted_coding = batadv_nc_random_weight_tq(router_coding->tq_avg);
  760. /* Select one destination for the MAC-header dst-field based on
  761. * weighted TQ-values.
  762. */
  763. if (tq_weighted_neigh >= tq_weighted_coding) {
  764. /* Destination from nc_packet is selected for MAC-header */
  765. first_dest = nc_packet->nc_path->next_hop;
  766. first_source = nc_packet->nc_path->prev_hop;
  767. second_dest = neigh_node->addr;
  768. second_source = ethhdr->h_source;
  769. packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  770. packet2 = (struct batadv_unicast_packet *)skb->data;
  771. packet_id1 = nc_packet->packet_id;
  772. packet_id2 = batadv_skb_crc32(skb,
  773. skb->data + sizeof(*packet2));
  774. } else {
  775. /* Destination for skb is selected for MAC-header */
  776. first_dest = neigh_node->addr;
  777. first_source = ethhdr->h_source;
  778. second_dest = nc_packet->nc_path->next_hop;
  779. second_source = nc_packet->nc_path->prev_hop;
  780. packet1 = (struct batadv_unicast_packet *)skb->data;
  781. packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  782. packet_id1 = batadv_skb_crc32(skb,
  783. skb->data + sizeof(*packet1));
  784. packet_id2 = nc_packet->packet_id;
  785. }
  786. /* Instead of zero padding the smallest data buffer, we
  787. * code into the largest.
  788. */
  789. if (skb->len <= nc_packet->skb->len) {
  790. skb_dest = nc_packet->skb;
  791. skb_src = skb;
  792. } else {
  793. skb_dest = skb;
  794. skb_src = nc_packet->skb;
  795. }
  796. /* coding_len is used when decoding the packet shorter packet */
  797. coding_len = skb_src->len - unicast_size;
  798. if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
  799. goto out;
  800. skb_push(skb_dest, header_add);
  801. coded_packet = (struct batadv_coded_packet *)skb_dest->data;
  802. skb_reset_mac_header(skb_dest);
  803. coded_packet->header.packet_type = BATADV_CODED;
  804. coded_packet->header.version = BATADV_COMPAT_VERSION;
  805. coded_packet->header.ttl = packet1->header.ttl;
  806. /* Info about first unicast packet */
  807. memcpy(coded_packet->first_source, first_source, ETH_ALEN);
  808. memcpy(coded_packet->first_orig_dest, packet1->dest, ETH_ALEN);
  809. coded_packet->first_crc = packet_id1;
  810. coded_packet->first_ttvn = packet1->ttvn;
  811. /* Info about second unicast packet */
  812. memcpy(coded_packet->second_dest, second_dest, ETH_ALEN);
  813. memcpy(coded_packet->second_source, second_source, ETH_ALEN);
  814. memcpy(coded_packet->second_orig_dest, packet2->dest, ETH_ALEN);
  815. coded_packet->second_crc = packet_id2;
  816. coded_packet->second_ttl = packet2->header.ttl;
  817. coded_packet->second_ttvn = packet2->ttvn;
  818. coded_packet->coded_len = htons(coding_len);
  819. /* This is where the magic happens: Code skb_src into skb_dest */
  820. batadv_nc_memxor(skb_dest->data + coded_size,
  821. skb_src->data + unicast_size, coding_len);
  822. /* Update counters accordingly */
  823. if (BATADV_SKB_CB(skb_src)->decoded &&
  824. BATADV_SKB_CB(skb_dest)->decoded) {
  825. /* Both packets are recoded */
  826. count = skb_src->len + ETH_HLEN;
  827. count += skb_dest->len + ETH_HLEN;
  828. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
  829. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
  830. } else if (!BATADV_SKB_CB(skb_src)->decoded &&
  831. !BATADV_SKB_CB(skb_dest)->decoded) {
  832. /* Both packets are newly coded */
  833. count = skb_src->len + ETH_HLEN;
  834. count += skb_dest->len + ETH_HLEN;
  835. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
  836. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
  837. } else if (BATADV_SKB_CB(skb_src)->decoded &&
  838. !BATADV_SKB_CB(skb_dest)->decoded) {
  839. /* skb_src recoded and skb_dest is newly coded */
  840. batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  841. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  842. skb_src->len + ETH_HLEN);
  843. batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  844. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  845. skb_dest->len + ETH_HLEN);
  846. } else if (!BATADV_SKB_CB(skb_src)->decoded &&
  847. BATADV_SKB_CB(skb_dest)->decoded) {
  848. /* skb_src is newly coded and skb_dest is recoded */
  849. batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  850. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  851. skb_src->len + ETH_HLEN);
  852. batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  853. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  854. skb_dest->len + ETH_HLEN);
  855. }
  856. /* skb_src is now coded into skb_dest, so free it */
  857. kfree_skb(skb_src);
  858. /* avoid duplicate free of skb from nc_packet */
  859. nc_packet->skb = NULL;
  860. batadv_nc_packet_free(nc_packet);
  861. /* Send the coded packet and return true */
  862. batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
  863. res = true;
  864. out:
  865. if (router_neigh)
  866. batadv_neigh_node_free_ref(router_neigh);
  867. if (router_coding)
  868. batadv_neigh_node_free_ref(router_coding);
  869. return res;
  870. }
  871. /**
  872. * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
  873. * @skb: data skb to forward
  874. * @dst: destination mac address of the other skb to code with
  875. * @src: source mac address of skb
  876. *
  877. * Whenever we network code a packet we have to check whether we received it in
  878. * a network coded form. If so, we may not be able to use it for coding because
  879. * some neighbors may also have received (overheard) the packet in the network
  880. * coded form without being able to decode it. It is hard to know which of the
  881. * neighboring nodes was able to decode the packet, therefore we can only
  882. * re-code the packet if the source of the previous encoded packet is involved.
  883. * Since the source encoded the packet we can be certain it has all necessary
  884. * decode information.
  885. *
  886. * Returns true if coding of a decoded packet is allowed.
  887. */
  888. static bool batadv_nc_skb_coding_possible(struct sk_buff *skb,
  889. uint8_t *dst, uint8_t *src)
  890. {
  891. if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
  892. return false;
  893. else
  894. return true;
  895. }
  896. /**
  897. * batadv_nc_path_search - Find the coding path matching in_nc_node and
  898. * out_nc_node to retrieve a buffered packet that can be used for coding.
  899. * @bat_priv: the bat priv with all the soft interface information
  900. * @in_nc_node: pointer to skb next hop's neighbor nc node
  901. * @out_nc_node: pointer to skb source's neighbor nc node
  902. * @skb: data skb to forward
  903. * @eth_dst: next hop mac address of skb
  904. *
  905. * Returns true if coding of a decoded skb is allowed.
  906. */
  907. static struct batadv_nc_packet *
  908. batadv_nc_path_search(struct batadv_priv *bat_priv,
  909. struct batadv_nc_node *in_nc_node,
  910. struct batadv_nc_node *out_nc_node,
  911. struct sk_buff *skb,
  912. uint8_t *eth_dst)
  913. {
  914. struct batadv_nc_path *nc_path, nc_path_key;
  915. struct batadv_nc_packet *nc_packet_out = NULL;
  916. struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  917. struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
  918. int idx;
  919. if (!hash)
  920. return NULL;
  921. /* Create almost path key */
  922. batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
  923. out_nc_node->addr);
  924. idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
  925. /* Check for coding opportunities in this nc_path */
  926. rcu_read_lock();
  927. hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
  928. if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
  929. continue;
  930. if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
  931. continue;
  932. spin_lock_bh(&nc_path->packet_list_lock);
  933. if (list_empty(&nc_path->packet_list)) {
  934. spin_unlock_bh(&nc_path->packet_list_lock);
  935. continue;
  936. }
  937. list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  938. &nc_path->packet_list, list) {
  939. if (!batadv_nc_skb_coding_possible(nc_packet->skb,
  940. eth_dst,
  941. in_nc_node->addr))
  942. continue;
  943. /* Coding opportunity is found! */
  944. list_del(&nc_packet->list);
  945. nc_packet_out = nc_packet;
  946. break;
  947. }
  948. spin_unlock_bh(&nc_path->packet_list_lock);
  949. break;
  950. }
  951. rcu_read_unlock();
  952. return nc_packet_out;
  953. }
  954. /**
  955. * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
  956. * skb's sender (may be equal to the originator).
  957. * @bat_priv: the bat priv with all the soft interface information
  958. * @skb: data skb to forward
  959. * @eth_dst: next hop mac address of skb
  960. * @eth_src: source mac address of skb
  961. * @in_nc_node: pointer to skb next hop's neighbor nc node
  962. *
  963. * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
  964. */
  965. static struct batadv_nc_packet *
  966. batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
  967. struct sk_buff *skb,
  968. uint8_t *eth_dst,
  969. uint8_t *eth_src,
  970. struct batadv_nc_node *in_nc_node)
  971. {
  972. struct batadv_orig_node *orig_node;
  973. struct batadv_nc_node *out_nc_node;
  974. struct batadv_nc_packet *nc_packet = NULL;
  975. orig_node = batadv_orig_hash_find(bat_priv, eth_src);
  976. if (!orig_node)
  977. return NULL;
  978. rcu_read_lock();
  979. list_for_each_entry_rcu(out_nc_node,
  980. &orig_node->out_coding_list, list) {
  981. /* Check if the skb is decoded and if recoding is possible */
  982. if (!batadv_nc_skb_coding_possible(skb,
  983. out_nc_node->addr, eth_src))
  984. continue;
  985. /* Search for an opportunity in this nc_path */
  986. nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
  987. out_nc_node, skb, eth_dst);
  988. if (nc_packet)
  989. break;
  990. }
  991. rcu_read_unlock();
  992. batadv_orig_node_free_ref(orig_node);
  993. return nc_packet;
  994. }
  995. /**
  996. * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
  997. * @skb: data skb to forward
  998. * @neigh_node: next hop to forward packet to
  999. * @ethhdr: pointer to the ethernet header inside the skb
  1000. *
  1001. * Loops through list of neighboring nodes the next hop has a good connection to
  1002. * (receives OGMs with a sufficient quality). We need to find a neighbor of our
  1003. * next hop that potentially sent a packet which our next hop also received
  1004. * (overheard) and has stored for later decoding.
  1005. *
  1006. * Returns true if the skb was consumed (encoded packet sent) or false otherwise
  1007. */
  1008. static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
  1009. struct batadv_neigh_node *neigh_node,
  1010. struct ethhdr *ethhdr)
  1011. {
  1012. struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  1013. struct batadv_priv *bat_priv = netdev_priv(netdev);
  1014. struct batadv_orig_node *orig_node = neigh_node->orig_node;
  1015. struct batadv_nc_node *nc_node;
  1016. struct batadv_nc_packet *nc_packet = NULL;
  1017. rcu_read_lock();
  1018. list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
  1019. /* Search for coding opportunity with this in_nc_node */
  1020. nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
  1021. neigh_node->addr,
  1022. ethhdr->h_source, nc_node);
  1023. /* Opportunity was found, so stop searching */
  1024. if (nc_packet)
  1025. break;
  1026. }
  1027. rcu_read_unlock();
  1028. if (!nc_packet)
  1029. return false;
  1030. /* Code and send packets */
  1031. if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
  1032. neigh_node))
  1033. return true;
  1034. /* out of mem ? Coding failed - we have to free the buffered packet
  1035. * to avoid memleaks. The skb passed as argument will be dealt with
  1036. * by the calling function.
  1037. */
  1038. batadv_nc_send_packet(nc_packet);
  1039. return false;
  1040. }
  1041. /**
  1042. * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
  1043. * @skb: skb to add to path
  1044. * @nc_path: path to add skb to
  1045. * @neigh_node: next hop to forward packet to
  1046. * @packet_id: checksum to identify packet
  1047. *
  1048. * Returns true if the packet was buffered or false in case of an error.
  1049. */
  1050. static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
  1051. struct batadv_nc_path *nc_path,
  1052. struct batadv_neigh_node *neigh_node,
  1053. __be32 packet_id)
  1054. {
  1055. struct batadv_nc_packet *nc_packet;
  1056. nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
  1057. if (!nc_packet)
  1058. return false;
  1059. /* Initialize nc_packet */
  1060. nc_packet->timestamp = jiffies;
  1061. nc_packet->packet_id = packet_id;
  1062. nc_packet->skb = skb;
  1063. nc_packet->neigh_node = neigh_node;
  1064. nc_packet->nc_path = nc_path;
  1065. /* Add coding packet to list */
  1066. spin_lock_bh(&nc_path->packet_list_lock);
  1067. list_add_tail(&nc_packet->list, &nc_path->packet_list);
  1068. spin_unlock_bh(&nc_path->packet_list_lock);
  1069. return true;
  1070. }
  1071. /**
  1072. * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
  1073. * buffer
  1074. * @skb: data skb to forward
  1075. * @neigh_node: next hop to forward packet to
  1076. * @ethhdr: pointer to the ethernet header inside the skb
  1077. *
  1078. * Returns true if the skb was consumed (encoded packet sent) or false otherwise
  1079. */
  1080. bool batadv_nc_skb_forward(struct sk_buff *skb,
  1081. struct batadv_neigh_node *neigh_node,
  1082. struct ethhdr *ethhdr)
  1083. {
  1084. const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  1085. struct batadv_priv *bat_priv = netdev_priv(netdev);
  1086. struct batadv_unicast_packet *packet;
  1087. struct batadv_nc_path *nc_path;
  1088. __be32 packet_id;
  1089. u8 *payload;
  1090. /* Check if network coding is enabled */
  1091. if (!atomic_read(&bat_priv->network_coding))
  1092. goto out;
  1093. /* We only handle unicast packets */
  1094. payload = skb_network_header(skb);
  1095. packet = (struct batadv_unicast_packet *)payload;
  1096. if (packet->header.packet_type != BATADV_UNICAST)
  1097. goto out;
  1098. /* Try to find a coding opportunity and send the skb if one is found */
  1099. if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
  1100. return true;
  1101. /* Find or create a nc_path for this src-dst pair */
  1102. nc_path = batadv_nc_get_path(bat_priv,
  1103. bat_priv->nc.coding_hash,
  1104. ethhdr->h_source,
  1105. neigh_node->addr);
  1106. if (!nc_path)
  1107. goto out;
  1108. /* Add skb to nc_path */
  1109. packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
  1110. if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
  1111. goto free_nc_path;
  1112. /* Packet is consumed */
  1113. return true;
  1114. free_nc_path:
  1115. batadv_nc_path_free_ref(nc_path);
  1116. out:
  1117. /* Packet is not consumed */
  1118. return false;
  1119. }
  1120. /**
  1121. * batadv_nc_free - clean up network coding memory
  1122. * @bat_priv: the bat priv with all the soft interface information
  1123. */
  1124. void batadv_nc_free(struct batadv_priv *bat_priv)
  1125. {
  1126. cancel_delayed_work_sync(&bat_priv->nc.work);
  1127. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
  1128. batadv_hash_destroy(bat_priv->nc.coding_hash);
  1129. }
  1130. /**
  1131. * batadv_nc_nodes_seq_print_text - print the nc node information
  1132. * @seq: seq file to print on
  1133. * @offset: not used
  1134. */
  1135. int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
  1136. {
  1137. struct net_device *net_dev = (struct net_device *)seq->private;
  1138. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1139. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1140. struct batadv_hard_iface *primary_if;
  1141. struct hlist_head *head;
  1142. struct batadv_orig_node *orig_node;
  1143. struct batadv_nc_node *nc_node;
  1144. int i;
  1145. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1146. if (!primary_if)
  1147. goto out;
  1148. /* Traverse list of originators */
  1149. for (i = 0; i < hash->size; i++) {
  1150. head = &hash->table[i];
  1151. /* For each orig_node in this bin */
  1152. rcu_read_lock();
  1153. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1154. seq_printf(seq, "Node: %pM\n", orig_node->orig);
  1155. seq_printf(seq, " Ingoing: ");
  1156. /* For each in_nc_node to this orig_node */
  1157. list_for_each_entry_rcu(nc_node,
  1158. &orig_node->in_coding_list,
  1159. list)
  1160. seq_printf(seq, "%pM ",
  1161. nc_node->addr);
  1162. seq_printf(seq, "\n");
  1163. seq_printf(seq, " Outgoing: ");
  1164. /* For out_nc_node to this orig_node */
  1165. list_for_each_entry_rcu(nc_node,
  1166. &orig_node->out_coding_list,
  1167. list)
  1168. seq_printf(seq, "%pM ",
  1169. nc_node->addr);
  1170. seq_printf(seq, "\n\n");
  1171. }
  1172. rcu_read_unlock();
  1173. }
  1174. out:
  1175. if (primary_if)
  1176. batadv_hardif_free_ref(primary_if);
  1177. return 0;
  1178. }
  1179. /**
  1180. * batadv_nc_init_debugfs - create nc folder and related files in debugfs
  1181. * @bat_priv: the bat priv with all the soft interface information
  1182. */
  1183. int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
  1184. {
  1185. struct dentry *nc_dir, *file;
  1186. nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
  1187. if (!nc_dir)
  1188. goto out;
  1189. file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
  1190. &bat_priv->nc.min_tq);
  1191. if (!file)
  1192. goto out;
  1193. file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir,
  1194. &bat_priv->nc.max_fwd_delay);
  1195. if (!file)
  1196. goto out;
  1197. return 0;
  1198. out:
  1199. return -ENOMEM;
  1200. }