blob: 69467fe71ff2419f149625c29af87b6419420bc0 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
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
22#include "main.h"
Antonio Quartullia73105b2011-04-27 14:27:44 +020023#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000024#include "aggregation.h"
25#include "send.h"
26#include "routing.h"
Sven Eckelmann6d5808d2011-05-11 20:59:06 +020027#include "hard-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029/* return true if new_packet can be aggregated with forw_packet */
Sven Eckelmann747e4222011-05-14 23:14:50 +020030static bool can_aggregate_with(const struct batman_packet *new_batman_packet,
Marek Lindnerb539e4c2011-06-26 21:15:13 +020031 struct bat_priv *bat_priv,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032 int packet_len,
33 unsigned long send_time,
34 bool directlink,
Sven Eckelmann747e4222011-05-14 23:14:50 +020035 const struct hard_iface *if_incoming,
36 const struct forw_packet *forw_packet)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037{
38 struct batman_packet *batman_packet =
39 (struct batman_packet *)forw_packet->skb->data;
40 int aggregated_bytes = forw_packet->packet_len + packet_len;
Marek Lindnerb539e4c2011-06-26 21:15:13 +020041 struct hard_iface *primary_if = NULL;
42 bool res = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043
44 /**
45 * we can aggregate the current packet to this aggregated packet
46 * if:
47 *
48 * - the send time is within our MAX_AGGREGATION_MS time
49 * - the resulting packet wont be bigger than
50 * MAX_AGGREGATION_BYTES
51 */
52
53 if (time_before(send_time, forw_packet->send_time) &&
54 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
55 forw_packet->send_time) &&
56 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
57
58 /**
59 * check aggregation compatibility
60 * -> direct link packets are broadcasted on
61 * their interface only
62 * -> aggregate packet if the current packet is
63 * a "global" packet as well as the base
64 * packet
65 */
66
Marek Lindnerb539e4c2011-06-26 21:15:13 +020067 primary_if = primary_if_get_selected(bat_priv);
68 if (!primary_if)
69 goto out;
70
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000071 /* packets without direct link flag and high TTL
72 * are flooded through the net */
73 if ((!directlink) &&
74 (!(batman_packet->flags & DIRECTLINK)) &&
75 (batman_packet->ttl != 1) &&
76
77 /* own packets originating non-primary
78 * interfaces leave only that interface */
79 ((!forw_packet->own) ||
Marek Lindnerb539e4c2011-06-26 21:15:13 +020080 (forw_packet->if_incoming == primary_if))) {
81 res = true;
82 goto out;
83 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000084
85 /* if the incoming packet is sent via this one
86 * interface only - we still can aggregate */
87 if ((directlink) &&
88 (new_batman_packet->ttl == 1) &&
89 (forw_packet->if_incoming == if_incoming) &&
90
91 /* packets from direct neighbors or
92 * own secondary interface packets
93 * (= secondary interface packets in general) */
94 (batman_packet->flags & DIRECTLINK ||
95 (forw_packet->own &&
Marek Lindnerb539e4c2011-06-26 21:15:13 +020096 forw_packet->if_incoming != primary_if))) {
97 res = true;
98 goto out;
99 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000100 }
101
Marek Lindnerb539e4c2011-06-26 21:15:13 +0200102out:
103 if (primary_if)
104 hardif_free_ref(primary_if);
105 return res;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000106}
107
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108/* create a new aggregated packet and add this packet to it */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200109static void new_aggregated_packet(const unsigned char *packet_buff,
110 int packet_len, unsigned long send_time,
111 bool direct_link,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000112 struct hard_iface *if_incoming,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113 int own_packet)
114{
115 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
116 struct forw_packet *forw_packet_aggr;
117 unsigned char *skb_buff;
118
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200119 if (!atomic_inc_not_zero(&if_incoming->refcount))
120 return;
121
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122 /* own packet should always be scheduled */
123 if (!own_packet) {
124 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
125 bat_dbg(DBG_BATMAN, bat_priv,
126 "batman packet queue full\n");
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200127 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128 }
129 }
130
Sven Eckelmann704509b2011-05-14 23:14:54 +0200131 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132 if (!forw_packet_aggr) {
133 if (!own_packet)
134 atomic_inc(&bat_priv->batman_queue_left);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200135 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 }
137
138 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
139 (packet_len < MAX_AGGREGATION_BYTES))
140 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
141 sizeof(struct ethhdr));
142 else
143 forw_packet_aggr->skb = dev_alloc_skb(packet_len +
144 sizeof(struct ethhdr));
145
146 if (!forw_packet_aggr->skb) {
147 if (!own_packet)
148 atomic_inc(&bat_priv->batman_queue_left);
149 kfree(forw_packet_aggr);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200150 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 }
152 skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
153
154 INIT_HLIST_NODE(&forw_packet_aggr->list);
155
156 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
157 forw_packet_aggr->packet_len = packet_len;
158 memcpy(skb_buff, packet_buff, packet_len);
159
160 forw_packet_aggr->own = own_packet;
161 forw_packet_aggr->if_incoming = if_incoming;
162 forw_packet_aggr->num_packets = 0;
Marek Lindnerecbd5322011-06-09 17:13:09 +0200163 forw_packet_aggr->direct_link_flags = NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164 forw_packet_aggr->send_time = send_time;
165
166 /* save packet direct link flag status */
167 if (direct_link)
168 forw_packet_aggr->direct_link_flags |= 1;
169
170 /* add new packet to packet list */
171 spin_lock_bh(&bat_priv->forw_bat_list_lock);
172 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
173 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
174
175 /* start timer for this packet */
176 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
177 send_outstanding_bat_packet);
178 queue_delayed_work(bat_event_workqueue,
179 &forw_packet_aggr->delayed_work,
180 send_time - jiffies);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200181
182 return;
183out:
184 hardif_free_ref(if_incoming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185}
186
187/* aggregate a new packet into the existing aggregation */
188static void aggregate(struct forw_packet *forw_packet_aggr,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200189 const unsigned char *packet_buff, int packet_len,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190 bool direct_link)
191{
192 unsigned char *skb_buff;
193
194 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
195 memcpy(skb_buff, packet_buff, packet_len);
196 forw_packet_aggr->packet_len += packet_len;
197 forw_packet_aggr->num_packets++;
198
199 /* save packet direct link flag status */
200 if (direct_link)
201 forw_packet_aggr->direct_link_flags |=
202 (1 << forw_packet_aggr->num_packets);
203}
204
205void add_bat_packet_to_list(struct bat_priv *bat_priv,
206 unsigned char *packet_buff, int packet_len,
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200207 struct hard_iface *if_incoming, int own_packet,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 unsigned long send_time)
209{
210 /**
211 * _aggr -> pointer to the packet we want to aggregate with
212 * _pos -> pointer to the position in the queue
213 */
214 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
215 struct hlist_node *tmp_node;
216 struct batman_packet *batman_packet =
217 (struct batman_packet *)packet_buff;
218 bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
219
220 /* find position for the packet in the forward queue */
221 spin_lock_bh(&bat_priv->forw_bat_list_lock);
222 /* own packets are not to be aggregated */
223 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
224 hlist_for_each_entry(forw_packet_pos, tmp_node,
225 &bat_priv->forw_bat_list, list) {
226 if (can_aggregate_with(batman_packet,
Marek Lindnerb539e4c2011-06-26 21:15:13 +0200227 bat_priv,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228 packet_len,
229 send_time,
230 direct_link,
231 if_incoming,
232 forw_packet_pos)) {
233 forw_packet_aggr = forw_packet_pos;
234 break;
235 }
236 }
237 }
238
239 /* nothing to aggregate with - either aggregation disabled or no
240 * suitable aggregation packet found */
241 if (!forw_packet_aggr) {
242 /* the following section can run without the lock */
243 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
244
245 /**
246 * if we could not aggregate this packet with one of the others
247 * we hold it back for a while, so that it might be aggregated
248 * later on
249 */
250 if ((!own_packet) &&
251 (atomic_read(&bat_priv->aggregated_ogms)))
252 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
253
254 new_aggregated_packet(packet_buff, packet_len,
255 send_time, direct_link,
256 if_incoming, own_packet);
257 } else {
258 aggregate(forw_packet_aggr,
259 packet_buff, packet_len,
260 direct_link);
261 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
262 }
263}
264
265/* unpack the aggregated packets and process them one by one */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200266void receive_aggr_bat_packet(const struct ethhdr *ethhdr,
267 unsigned char *packet_buff, int packet_len,
268 struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269{
270 struct batman_packet *batman_packet;
271 int buff_pos = 0;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200272 unsigned char *tt_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
274 batman_packet = (struct batman_packet *)packet_buff;
275
276 do {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200277 /* network to host order for our 32bit seqno and the
278 orig_interval */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 batman_packet->seqno = ntohl(batman_packet->seqno);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200280 batman_packet->tt_crc = ntohs(batman_packet->tt_crc);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200282 tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283
Antonio Quartullia73105b2011-04-27 14:27:44 +0200284 receive_bat_packet(ethhdr, batman_packet, tt_buff, if_incoming);
285
286 buff_pos += BAT_PACKET_LEN +
287 tt_len(batman_packet->tt_num_changes);
288
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 batman_packet = (struct batman_packet *)
290 (packet_buff + buff_pos);
291 } while (aggregated_packet(buff_pos, packet_len,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200292 batman_packet->tt_num_changes));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293}