blob: c583e049f421b12c5a2a41b4801198c08e650154 [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,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031 int packet_len,
32 unsigned long send_time,
33 bool directlink,
Sven Eckelmann747e4222011-05-14 23:14:50 +020034 const struct hard_iface *if_incoming,
35 const struct forw_packet *forw_packet)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
37 struct batman_packet *batman_packet =
38 (struct batman_packet *)forw_packet->skb->data;
39 int aggregated_bytes = forw_packet->packet_len + packet_len;
40
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
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 /**
56 * check aggregation compatibility
57 * -> direct link packets are broadcasted on
58 * their interface only
59 * -> aggregate packet if the current packet is
60 * a "global" packet as well as the base
61 * packet
62 */
63
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
70 /* own packets originating non-primary
71 * interfaces leave only that interface */
72 ((!forw_packet->own) ||
73 (forw_packet->if_incoming->if_num == 0)))
74 return true;
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
82 /* packets from direct neighbors or
83 * own secondary interface packets
84 * (= secondary interface packets in general) */
85 (batman_packet->flags & DIRECTLINK ||
86 (forw_packet->own &&
87 forw_packet->if_incoming->if_num != 0)))
88 return true;
89 }
90
91 return false;
92}
93
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094/* create a new aggregated packet and add this packet to it */
Sven Eckelmann747e4222011-05-14 23:14:50 +020095static void new_aggregated_packet(const unsigned char *packet_buff,
96 int packet_len, unsigned long send_time,
97 bool direct_link,
Marek Lindnere6c10f42011-02-18 12:33:20 +000098 struct hard_iface *if_incoming,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099 int own_packet)
100{
101 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
102 struct forw_packet *forw_packet_aggr;
103 unsigned char *skb_buff;
104
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200105 if (!atomic_inc_not_zero(&if_incoming->refcount))
106 return;
107
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108 /* 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");
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200113 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114 }
115 }
116
Sven Eckelmann704509b2011-05-14 23:14:54 +0200117 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 if (!forw_packet_aggr) {
119 if (!own_packet)
120 atomic_inc(&bat_priv->batman_queue_left);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200121 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122 }
123
124 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
125 (packet_len < MAX_AGGREGATION_BYTES))
126 forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES +
127 sizeof(struct ethhdr));
128 else
129 forw_packet_aggr->skb = dev_alloc_skb(packet_len +
130 sizeof(struct ethhdr));
131
132 if (!forw_packet_aggr->skb) {
133 if (!own_packet)
134 atomic_inc(&bat_priv->batman_queue_left);
135 kfree(forw_packet_aggr);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200136 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137 }
138 skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
139
140 INIT_HLIST_NODE(&forw_packet_aggr->list);
141
142 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
143 forw_packet_aggr->packet_len = packet_len;
144 memcpy(skb_buff, packet_buff, packet_len);
145
146 forw_packet_aggr->own = own_packet;
147 forw_packet_aggr->if_incoming = if_incoming;
148 forw_packet_aggr->num_packets = 0;
Marek Lindnerecbd5322011-06-09 17:13:09 +0200149 forw_packet_aggr->direct_link_flags = NO_FLAGS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150 forw_packet_aggr->send_time = send_time;
151
152 /* save packet direct link flag status */
153 if (direct_link)
154 forw_packet_aggr->direct_link_flags |= 1;
155
156 /* add new packet to packet list */
157 spin_lock_bh(&bat_priv->forw_bat_list_lock);
158 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
159 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
160
161 /* start timer for this packet */
162 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
163 send_outstanding_bat_packet);
164 queue_delayed_work(bat_event_workqueue,
165 &forw_packet_aggr->delayed_work,
166 send_time - jiffies);
Sven Eckelmann6d5808d2011-05-11 20:59:06 +0200167
168 return;
169out:
170 hardif_free_ref(if_incoming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171}
172
173/* aggregate a new packet into the existing aggregation */
174static void aggregate(struct forw_packet *forw_packet_aggr,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200175 const unsigned char *packet_buff, int packet_len,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176 bool direct_link)
177{
178 unsigned char *skb_buff;
179
180 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
181 memcpy(skb_buff, packet_buff, packet_len);
182 forw_packet_aggr->packet_len += packet_len;
183 forw_packet_aggr->num_packets++;
184
185 /* save packet direct link flag status */
186 if (direct_link)
187 forw_packet_aggr->direct_link_flags |=
188 (1 << forw_packet_aggr->num_packets);
189}
190
191void add_bat_packet_to_list(struct bat_priv *bat_priv,
192 unsigned char *packet_buff, int packet_len,
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200193 struct hard_iface *if_incoming, int own_packet,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194 unsigned long send_time)
195{
196 /**
197 * _aggr -> pointer to the packet we want to aggregate with
198 * _pos -> pointer to the position in the queue
199 */
200 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
201 struct hlist_node *tmp_node;
202 struct batman_packet *batman_packet =
203 (struct batman_packet *)packet_buff;
204 bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
205
206 /* find position for the packet in the forward queue */
207 spin_lock_bh(&bat_priv->forw_bat_list_lock);
208 /* own packets are not to be aggregated */
209 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
210 hlist_for_each_entry(forw_packet_pos, tmp_node,
211 &bat_priv->forw_bat_list, list) {
212 if (can_aggregate_with(batman_packet,
213 packet_len,
214 send_time,
215 direct_link,
216 if_incoming,
217 forw_packet_pos)) {
218 forw_packet_aggr = forw_packet_pos;
219 break;
220 }
221 }
222 }
223
224 /* nothing to aggregate with - either aggregation disabled or no
225 * suitable aggregation packet found */
226 if (!forw_packet_aggr) {
227 /* the following section can run without the lock */
228 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
229
230 /**
231 * if we could not aggregate this packet with one of the others
232 * we hold it back for a while, so that it might be aggregated
233 * later on
234 */
235 if ((!own_packet) &&
236 (atomic_read(&bat_priv->aggregated_ogms)))
237 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
238
239 new_aggregated_packet(packet_buff, packet_len,
240 send_time, direct_link,
241 if_incoming, own_packet);
242 } else {
243 aggregate(forw_packet_aggr,
244 packet_buff, packet_len,
245 direct_link);
246 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
247 }
248}
249
250/* unpack the aggregated packets and process them one by one */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200251void receive_aggr_bat_packet(const struct ethhdr *ethhdr,
252 unsigned char *packet_buff, int packet_len,
253 struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254{
255 struct batman_packet *batman_packet;
256 int buff_pos = 0;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200257 unsigned char *tt_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258
259 batman_packet = (struct batman_packet *)packet_buff;
260
261 do {
Antonio Quartullia73105b2011-04-27 14:27:44 +0200262 /* network to host order for our 32bit seqno and the
263 orig_interval */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264 batman_packet->seqno = ntohl(batman_packet->seqno);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200265 batman_packet->tt_crc = ntohs(batman_packet->tt_crc);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200267 tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
Antonio Quartullia73105b2011-04-27 14:27:44 +0200269 receive_bat_packet(ethhdr, batman_packet, tt_buff, if_incoming);
270
271 buff_pos += BAT_PACKET_LEN +
272 tt_len(batman_packet->tt_num_changes);
273
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274 batman_packet = (struct batman_packet *)
275 (packet_buff + buff_pos);
276 } while (aggregated_packet(buff_pos, packet_len,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200277 batman_packet->tt_num_changes));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278}