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(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(struct batman_packet *new_batman_packet,
  33. int packet_len,
  34. unsigned long send_time,
  35. bool directlink,
  36. struct hard_iface *if_incoming,
  37. 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(unsigned char *packet_buff, int packet_len,
  89. unsigned long send_time, bool direct_link,
  90. struct hard_iface *if_incoming,
  91. int own_packet)
  92. {
  93. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  94. struct forw_packet *forw_packet_aggr;
  95. unsigned char *skb_buff;
  96. if (!atomic_inc_not_zero(&if_incoming->refcount))
  97. return;
  98. /* own packet should always be scheduled */
  99. if (!own_packet) {
  100. if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
  101. bat_dbg(DBG_BATMAN, bat_priv,
  102. "batman packet queue full\n");
  103. goto out;
  104. }
  105. }
  106. forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
  107. if (!forw_packet_aggr) {
  108. if (!own_packet)
  109. atomic_inc(&bat_priv->batman_queue_left);
  110. goto out;
  111. }
  112. if ((atomic_read(&bat_priv->aggregated_ogms)) &&
  113. (packet_len < MAX_AGGREGATION_BYTES))
  114. forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
  115. sizeof(struct ethhdr));
  116. else
  117. forw_packet_aggr->skb = dev_alloc_skb(packet_len +
  118. sizeof(struct ethhdr));
  119. if (!forw_packet_aggr->skb) {
  120. if (!own_packet)
  121. atomic_inc(&bat_priv->batman_queue_left);
  122. kfree(forw_packet_aggr);
  123. goto out;
  124. }
  125. skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
  126. INIT_HLIST_NODE(&forw_packet_aggr->list);
  127. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  128. forw_packet_aggr->packet_len = packet_len;
  129. memcpy(skb_buff, packet_buff, packet_len);
  130. forw_packet_aggr->own = own_packet;
  131. forw_packet_aggr->if_incoming = if_incoming;
  132. forw_packet_aggr->num_packets = 0;
  133. forw_packet_aggr->direct_link_flags = 0;
  134. forw_packet_aggr->send_time = send_time;
  135. /* save packet direct link flag status */
  136. if (direct_link)
  137. forw_packet_aggr->direct_link_flags |= 1;
  138. /* add new packet to packet list */
  139. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  140. hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
  141. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  142. /* start timer for this packet */
  143. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  144. send_outstanding_bat_packet);
  145. queue_delayed_work(bat_event_workqueue,
  146. &forw_packet_aggr->delayed_work,
  147. send_time - jiffies);
  148. return;
  149. out:
  150. hardif_free_ref(if_incoming);
  151. }
  152. /* aggregate a new packet into the existing aggregation */
  153. static void aggregate(struct forw_packet *forw_packet_aggr,
  154. unsigned char *packet_buff,
  155. 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, char 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(struct ethhdr *ethhdr, unsigned char *packet_buff,
  224. int packet_len, struct hard_iface *if_incoming)
  225. {
  226. struct batman_packet *batman_packet;
  227. int buff_pos = 0;
  228. unsigned char *tt_buff;
  229. batman_packet = (struct batman_packet *)packet_buff;
  230. do {
  231. /* network to host order for our 32bit seqno, and the
  232. orig_interval. */
  233. batman_packet->seqno = ntohl(batman_packet->seqno);
  234. tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
  235. receive_bat_packet(ethhdr, batman_packet,
  236. tt_buff, tt_len(batman_packet),
  237. if_incoming);
  238. buff_pos += BAT_PACKET_LEN + tt_len(batman_packet);
  239. batman_packet = (struct batman_packet *)
  240. (packet_buff + buff_pos);
  241. } while (aggregated_packet(buff_pos, packet_len,
  242. batman_packet->num_tt));
  243. }