aggregation.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "aggregation.h"
  23. #include "send.h"
  24. #include "routing.h"
  25. #include "hard-interface.h"
  26. /* calculate the size of the tt information for a given packet */
  27. static int tt_len(const struct batman_packet *batman_packet)
  28. {
  29. return batman_packet->num_tt * ETH_ALEN;
  30. }
  31. /* return true if new_packet can be aggregated with forw_packet */
  32. static bool can_aggregate_with(const struct batman_packet *new_batman_packet,
  33. int packet_len,
  34. unsigned long send_time,
  35. bool directlink,
  36. const struct hard_iface *if_incoming,
  37. const struct forw_packet *forw_packet)
  38. {
  39. struct batman_packet *batman_packet =
  40. (struct batman_packet *)forw_packet->skb->data;
  41. int aggregated_bytes = forw_packet->packet_len + packet_len;
  42. /**
  43. * we can aggregate the current packet to this aggregated packet
  44. * if:
  45. *
  46. * - the send time is within our MAX_AGGREGATION_MS time
  47. * - the resulting packet wont be bigger than
  48. * MAX_AGGREGATION_BYTES
  49. */
  50. if (time_before(send_time, forw_packet->send_time) &&
  51. time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
  52. forw_packet->send_time) &&
  53. (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
  54. /**
  55. * check aggregation compatibility
  56. * -> direct link packets are broadcasted on
  57. * their interface only
  58. * -> aggregate packet if the current packet is
  59. * a "global" packet as well as the base
  60. * packet
  61. */
  62. /* packets without direct link flag and high TTL
  63. * are flooded through the net */
  64. if ((!directlink) &&
  65. (!(batman_packet->flags & DIRECTLINK)) &&
  66. (batman_packet->ttl != 1) &&
  67. /* own packets originating non-primary
  68. * interfaces leave only that interface */
  69. ((!forw_packet->own) ||
  70. (forw_packet->if_incoming->if_num == 0)))
  71. return true;
  72. /* if the incoming packet is sent via this one
  73. * interface only - we still can aggregate */
  74. if ((directlink) &&
  75. (new_batman_packet->ttl == 1) &&
  76. (forw_packet->if_incoming == if_incoming) &&
  77. /* packets from direct neighbors or
  78. * own secondary interface packets
  79. * (= secondary interface packets in general) */
  80. (batman_packet->flags & DIRECTLINK ||
  81. (forw_packet->own &&
  82. forw_packet->if_incoming->if_num != 0)))
  83. return true;
  84. }
  85. return false;
  86. }
  87. /* create a new aggregated packet and add this packet to it */
  88. static void new_aggregated_packet(const unsigned char *packet_buff,
  89. int packet_len, unsigned long send_time,
  90. bool direct_link,
  91. struct hard_iface *if_incoming,
  92. int own_packet)
  93. {
  94. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  95. struct forw_packet *forw_packet_aggr;
  96. unsigned char *skb_buff;
  97. if (!atomic_inc_not_zero(&if_incoming->refcount))
  98. return;
  99. /* own packet should always be scheduled */
  100. if (!own_packet) {
  101. if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
  102. bat_dbg(DBG_BATMAN, bat_priv,
  103. "batman packet queue full\n");
  104. goto out;
  105. }
  106. }
  107. forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
  108. if (!forw_packet_aggr) {
  109. if (!own_packet)
  110. atomic_inc(&bat_priv->batman_queue_left);
  111. goto out;
  112. }
  113. if ((atomic_read(&bat_priv->aggregated_ogms)) &&
  114. (packet_len < MAX_AGGREGATION_BYTES))
  115. forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
  116. sizeof(struct ethhdr));
  117. else
  118. forw_packet_aggr->skb = dev_alloc_skb(packet_len +
  119. sizeof(struct ethhdr));
  120. if (!forw_packet_aggr->skb) {
  121. if (!own_packet)
  122. atomic_inc(&bat_priv->batman_queue_left);
  123. kfree(forw_packet_aggr);
  124. goto out;
  125. }
  126. skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
  127. INIT_HLIST_NODE(&forw_packet_aggr->list);
  128. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  129. forw_packet_aggr->packet_len = packet_len;
  130. memcpy(skb_buff, packet_buff, packet_len);
  131. forw_packet_aggr->own = own_packet;
  132. forw_packet_aggr->if_incoming = if_incoming;
  133. forw_packet_aggr->num_packets = 0;
  134. forw_packet_aggr->direct_link_flags = NO_FLAGS;
  135. forw_packet_aggr->send_time = send_time;
  136. /* save packet direct link flag status */
  137. if (direct_link)
  138. forw_packet_aggr->direct_link_flags |= 1;
  139. /* add new packet to packet list */
  140. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  141. hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
  142. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  143. /* start timer for this packet */
  144. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  145. send_outstanding_bat_packet);
  146. queue_delayed_work(bat_event_workqueue,
  147. &forw_packet_aggr->delayed_work,
  148. send_time - jiffies);
  149. return;
  150. out:
  151. hardif_free_ref(if_incoming);
  152. }
  153. /* aggregate a new packet into the existing aggregation */
  154. static void aggregate(struct forw_packet *forw_packet_aggr,
  155. const unsigned char *packet_buff, int packet_len,
  156. bool direct_link)
  157. {
  158. unsigned char *skb_buff;
  159. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  160. memcpy(skb_buff, packet_buff, packet_len);
  161. forw_packet_aggr->packet_len += packet_len;
  162. forw_packet_aggr->num_packets++;
  163. /* save packet direct link flag status */
  164. if (direct_link)
  165. forw_packet_aggr->direct_link_flags |=
  166. (1 << forw_packet_aggr->num_packets);
  167. }
  168. void add_bat_packet_to_list(struct bat_priv *bat_priv,
  169. unsigned char *packet_buff, int packet_len,
  170. struct hard_iface *if_incoming, int own_packet,
  171. unsigned long send_time)
  172. {
  173. /**
  174. * _aggr -> pointer to the packet we want to aggregate with
  175. * _pos -> pointer to the position in the queue
  176. */
  177. struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
  178. struct hlist_node *tmp_node;
  179. struct batman_packet *batman_packet =
  180. (struct batman_packet *)packet_buff;
  181. bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
  182. /* find position for the packet in the forward queue */
  183. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  184. /* own packets are not to be aggregated */
  185. if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
  186. hlist_for_each_entry(forw_packet_pos, tmp_node,
  187. &bat_priv->forw_bat_list, list) {
  188. if (can_aggregate_with(batman_packet,
  189. packet_len,
  190. send_time,
  191. direct_link,
  192. if_incoming,
  193. forw_packet_pos)) {
  194. forw_packet_aggr = forw_packet_pos;
  195. break;
  196. }
  197. }
  198. }
  199. /* nothing to aggregate with - either aggregation disabled or no
  200. * suitable aggregation packet found */
  201. if (!forw_packet_aggr) {
  202. /* the following section can run without the lock */
  203. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  204. /**
  205. * if we could not aggregate this packet with one of the others
  206. * we hold it back for a while, so that it might be aggregated
  207. * later on
  208. */
  209. if ((!own_packet) &&
  210. (atomic_read(&bat_priv->aggregated_ogms)))
  211. send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
  212. new_aggregated_packet(packet_buff, packet_len,
  213. send_time, direct_link,
  214. if_incoming, own_packet);
  215. } else {
  216. aggregate(forw_packet_aggr,
  217. packet_buff, packet_len,
  218. direct_link);
  219. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  220. }
  221. }
  222. /* unpack the aggregated packets and process them one by one */
  223. void receive_aggr_bat_packet(const struct ethhdr *ethhdr,
  224. unsigned char *packet_buff, int packet_len,
  225. struct hard_iface *if_incoming)
  226. {
  227. struct batman_packet *batman_packet;
  228. int buff_pos = 0;
  229. unsigned char *tt_buff;
  230. batman_packet = (struct batman_packet *)packet_buff;
  231. do {
  232. /* network to host order for our 32bit seqno, and the
  233. orig_interval. */
  234. batman_packet->seqno = ntohl(batman_packet->seqno);
  235. tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
  236. receive_bat_packet(ethhdr, batman_packet,
  237. tt_buff, tt_len(batman_packet),
  238. if_incoming);
  239. buff_pos += BAT_PACKET_LEN + tt_len(batman_packet);
  240. batman_packet = (struct batman_packet *)
  241. (packet_buff + buff_pos);
  242. } while (aggregated_packet(buff_pos, packet_len,
  243. batman_packet->num_tt));
  244. }