aggregation.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "translation-table.h"
  23. #include "aggregation.h"
  24. #include "send.h"
  25. #include "routing.h"
  26. #include "hard-interface.h"
  27. /* return true if new_packet can be aggregated with forw_packet */
  28. static bool can_aggregate_with(const struct batman_packet *new_batman_packet,
  29. struct bat_priv *bat_priv,
  30. int packet_len,
  31. unsigned long send_time,
  32. bool directlink,
  33. const struct hard_iface *if_incoming,
  34. const struct forw_packet *forw_packet)
  35. {
  36. struct batman_packet *batman_packet =
  37. (struct batman_packet *)forw_packet->skb->data;
  38. int aggregated_bytes = forw_packet->packet_len + packet_len;
  39. struct hard_iface *primary_if = NULL;
  40. bool res = false;
  41. /**
  42. * we can aggregate the current packet to this aggregated packet
  43. * if:
  44. *
  45. * - the send time is within our MAX_AGGREGATION_MS time
  46. * - the resulting packet wont be bigger than
  47. * MAX_AGGREGATION_BYTES
  48. */
  49. if (time_before(send_time, forw_packet->send_time) &&
  50. time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
  51. forw_packet->send_time) &&
  52. (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
  53. /**
  54. * check aggregation compatibility
  55. * -> direct link packets are broadcasted on
  56. * their interface only
  57. * -> aggregate packet if the current packet is
  58. * a "global" packet as well as the base
  59. * packet
  60. */
  61. primary_if = primary_if_get_selected(bat_priv);
  62. if (!primary_if)
  63. goto out;
  64. /* packets without direct link flag and high TTL
  65. * are flooded through the net */
  66. if ((!directlink) &&
  67. (!(batman_packet->flags & DIRECTLINK)) &&
  68. (batman_packet->ttl != 1) &&
  69. /* own packets originating non-primary
  70. * interfaces leave only that interface */
  71. ((!forw_packet->own) ||
  72. (forw_packet->if_incoming == primary_if))) {
  73. res = true;
  74. goto out;
  75. }
  76. /* if the incoming packet is sent via this one
  77. * interface only - we still can aggregate */
  78. if ((directlink) &&
  79. (new_batman_packet->ttl == 1) &&
  80. (forw_packet->if_incoming == if_incoming) &&
  81. /* packets from direct neighbors or
  82. * own secondary interface packets
  83. * (= secondary interface packets in general) */
  84. (batman_packet->flags & DIRECTLINK ||
  85. (forw_packet->own &&
  86. forw_packet->if_incoming != primary_if))) {
  87. res = true;
  88. goto out;
  89. }
  90. }
  91. out:
  92. if (primary_if)
  93. hardif_free_ref(primary_if);
  94. return res;
  95. }
  96. /* create a new aggregated packet and add this packet to it */
  97. static void new_aggregated_packet(const unsigned char *packet_buff,
  98. int packet_len, unsigned long send_time,
  99. bool direct_link,
  100. struct hard_iface *if_incoming,
  101. int own_packet)
  102. {
  103. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  104. struct forw_packet *forw_packet_aggr;
  105. unsigned char *skb_buff;
  106. if (!atomic_inc_not_zero(&if_incoming->refcount))
  107. return;
  108. /* own packet should always be scheduled */
  109. if (!own_packet) {
  110. if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
  111. bat_dbg(DBG_BATMAN, bat_priv,
  112. "batman packet queue full\n");
  113. goto out;
  114. }
  115. }
  116. forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
  117. if (!forw_packet_aggr) {
  118. if (!own_packet)
  119. atomic_inc(&bat_priv->batman_queue_left);
  120. goto out;
  121. }
  122. if ((atomic_read(&bat_priv->aggregated_ogms)) &&
  123. (packet_len < MAX_AGGREGATION_BYTES))
  124. forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
  125. sizeof(struct ethhdr));
  126. else
  127. forw_packet_aggr->skb = dev_alloc_skb(packet_len +
  128. sizeof(struct ethhdr));
  129. if (!forw_packet_aggr->skb) {
  130. if (!own_packet)
  131. atomic_inc(&bat_priv->batman_queue_left);
  132. kfree(forw_packet_aggr);
  133. goto out;
  134. }
  135. skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
  136. INIT_HLIST_NODE(&forw_packet_aggr->list);
  137. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  138. forw_packet_aggr->packet_len = packet_len;
  139. memcpy(skb_buff, packet_buff, packet_len);
  140. forw_packet_aggr->own = own_packet;
  141. forw_packet_aggr->if_incoming = if_incoming;
  142. forw_packet_aggr->num_packets = 0;
  143. forw_packet_aggr->direct_link_flags = NO_FLAGS;
  144. forw_packet_aggr->send_time = send_time;
  145. /* save packet direct link flag status */
  146. if (direct_link)
  147. forw_packet_aggr->direct_link_flags |= 1;
  148. /* add new packet to packet list */
  149. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  150. hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
  151. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  152. /* start timer for this packet */
  153. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  154. send_outstanding_bat_packet);
  155. queue_delayed_work(bat_event_workqueue,
  156. &forw_packet_aggr->delayed_work,
  157. send_time - jiffies);
  158. return;
  159. out:
  160. hardif_free_ref(if_incoming);
  161. }
  162. /* aggregate a new packet into the existing aggregation */
  163. static void aggregate(struct forw_packet *forw_packet_aggr,
  164. const unsigned char *packet_buff, int packet_len,
  165. bool direct_link)
  166. {
  167. unsigned char *skb_buff;
  168. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  169. memcpy(skb_buff, packet_buff, packet_len);
  170. forw_packet_aggr->packet_len += packet_len;
  171. forw_packet_aggr->num_packets++;
  172. /* save packet direct link flag status */
  173. if (direct_link)
  174. forw_packet_aggr->direct_link_flags |=
  175. (1 << forw_packet_aggr->num_packets);
  176. }
  177. void add_bat_packet_to_list(struct bat_priv *bat_priv,
  178. unsigned char *packet_buff, int packet_len,
  179. struct hard_iface *if_incoming, int own_packet,
  180. unsigned long send_time)
  181. {
  182. /**
  183. * _aggr -> pointer to the packet we want to aggregate with
  184. * _pos -> pointer to the position in the queue
  185. */
  186. struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
  187. struct hlist_node *tmp_node;
  188. struct batman_packet *batman_packet =
  189. (struct batman_packet *)packet_buff;
  190. bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
  191. /* find position for the packet in the forward queue */
  192. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  193. /* own packets are not to be aggregated */
  194. if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
  195. hlist_for_each_entry(forw_packet_pos, tmp_node,
  196. &bat_priv->forw_bat_list, list) {
  197. if (can_aggregate_with(batman_packet,
  198. bat_priv,
  199. packet_len,
  200. send_time,
  201. direct_link,
  202. if_incoming,
  203. forw_packet_pos)) {
  204. forw_packet_aggr = forw_packet_pos;
  205. break;
  206. }
  207. }
  208. }
  209. /* nothing to aggregate with - either aggregation disabled or no
  210. * suitable aggregation packet found */
  211. if (!forw_packet_aggr) {
  212. /* the following section can run without the lock */
  213. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  214. /**
  215. * if we could not aggregate this packet with one of the others
  216. * we hold it back for a while, so that it might be aggregated
  217. * later on
  218. */
  219. if ((!own_packet) &&
  220. (atomic_read(&bat_priv->aggregated_ogms)))
  221. send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
  222. new_aggregated_packet(packet_buff, packet_len,
  223. send_time, direct_link,
  224. if_incoming, own_packet);
  225. } else {
  226. aggregate(forw_packet_aggr,
  227. packet_buff, packet_len,
  228. direct_link);
  229. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  230. }
  231. }
  232. /* unpack the aggregated packets and process them one by one */
  233. void receive_aggr_bat_packet(const struct ethhdr *ethhdr,
  234. unsigned char *packet_buff, int packet_len,
  235. struct hard_iface *if_incoming)
  236. {
  237. struct batman_packet *batman_packet;
  238. int buff_pos = 0;
  239. unsigned char *tt_buff;
  240. batman_packet = (struct batman_packet *)packet_buff;
  241. do {
  242. /* network to host order for our 32bit seqno and the
  243. orig_interval */
  244. batman_packet->seqno = ntohl(batman_packet->seqno);
  245. batman_packet->tt_crc = ntohs(batman_packet->tt_crc);
  246. tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
  247. receive_bat_packet(ethhdr, batman_packet, tt_buff, if_incoming);
  248. buff_pos += BAT_PACKET_LEN +
  249. tt_len(batman_packet->tt_num_changes);
  250. batman_packet = (struct batman_packet *)
  251. (packet_buff + buff_pos);
  252. } while (aggregated_packet(buff_pos, packet_len,
  253. batman_packet->tt_num_changes));
  254. }