aggregation.c 8.3 KB

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