blob: 9b945902447946fbf9bf2cd3b1c0a396ed3b6643 [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"
23#include "aggregation.h"
24#include "send.h"
25#include "routing.h"
26
Antonio Quartulli2dafb492011-05-05 08:42:45 +020027/* calculate the size of the tt information for a given packet */
28static int tt_len(struct batman_packet *batman_packet)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029{
Antonio Quartulli2dafb492011-05-05 08:42:45 +020030 return batman_packet->num_tt * ETH_ALEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031}
32
33/* return true if new_packet can be aggregated with forw_packet */
34static bool can_aggregate_with(struct batman_packet *new_batman_packet,
35 int packet_len,
36 unsigned long send_time,
37 bool directlink,
Marek Lindnere6c10f42011-02-18 12:33:20 +000038 struct hard_iface *if_incoming,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039 struct forw_packet *forw_packet)
40{
41 struct batman_packet *batman_packet =
42 (struct batman_packet *)forw_packet->skb->data;
43 int aggregated_bytes = forw_packet->packet_len + packet_len;
44
45 /**
46 * we can aggregate the current packet to this aggregated packet
47 * if:
48 *
49 * - the send time is within our MAX_AGGREGATION_MS time
50 * - the resulting packet wont be bigger than
51 * MAX_AGGREGATION_BYTES
52 */
53
54 if (time_before(send_time, forw_packet->send_time) &&
55 time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS),
56 forw_packet->send_time) &&
57 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
58
59 /**
60 * check aggregation compatibility
61 * -> direct link packets are broadcasted on
62 * their interface only
63 * -> aggregate packet if the current packet is
64 * a "global" packet as well as the base
65 * packet
66 */
67
68 /* packets without direct link flag and high TTL
69 * are flooded through the net */
70 if ((!directlink) &&
71 (!(batman_packet->flags & DIRECTLINK)) &&
72 (batman_packet->ttl != 1) &&
73
74 /* own packets originating non-primary
75 * interfaces leave only that interface */
76 ((!forw_packet->own) ||
77 (forw_packet->if_incoming->if_num == 0)))
78 return true;
79
80 /* if the incoming packet is sent via this one
81 * interface only - we still can aggregate */
82 if ((directlink) &&
83 (new_batman_packet->ttl == 1) &&
84 (forw_packet->if_incoming == if_incoming) &&
85
86 /* packets from direct neighbors or
87 * own secondary interface packets
88 * (= secondary interface packets in general) */
89 (batman_packet->flags & DIRECTLINK ||
90 (forw_packet->own &&
91 forw_packet->if_incoming->if_num != 0)))
92 return true;
93 }
94
95 return false;
96}
97
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098/* create a new aggregated packet and add this packet to it */
99static void new_aggregated_packet(unsigned char *packet_buff, int packet_len,
100 unsigned long send_time, bool direct_link,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000101 struct hard_iface *if_incoming,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102 int own_packet)
103{
104 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
105 struct forw_packet *forw_packet_aggr;
106 unsigned char *skb_buff;
107
108 /* 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");
113 return;
114 }
115 }
116
117 forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
118 if (!forw_packet_aggr) {
119 if (!own_packet)
120 atomic_inc(&bat_priv->batman_queue_left);
121 return;
122 }
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);
136 return;
137 }
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;
149 forw_packet_aggr->direct_link_flags = 0;
150 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);
167}
168
169/* aggregate a new packet into the existing aggregation */
170static void aggregate(struct forw_packet *forw_packet_aggr,
171 unsigned char *packet_buff,
172 int packet_len,
173 bool direct_link)
174{
175 unsigned char *skb_buff;
176
177 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
178 memcpy(skb_buff, packet_buff, packet_len);
179 forw_packet_aggr->packet_len += packet_len;
180 forw_packet_aggr->num_packets++;
181
182 /* save packet direct link flag status */
183 if (direct_link)
184 forw_packet_aggr->direct_link_flags |=
185 (1 << forw_packet_aggr->num_packets);
186}
187
188void add_bat_packet_to_list(struct bat_priv *bat_priv,
189 unsigned char *packet_buff, int packet_len,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000190 struct hard_iface *if_incoming, char own_packet,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191 unsigned long send_time)
192{
193 /**
194 * _aggr -> pointer to the packet we want to aggregate with
195 * _pos -> pointer to the position in the queue
196 */
197 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
198 struct hlist_node *tmp_node;
199 struct batman_packet *batman_packet =
200 (struct batman_packet *)packet_buff;
201 bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
202
203 /* find position for the packet in the forward queue */
204 spin_lock_bh(&bat_priv->forw_bat_list_lock);
205 /* own packets are not to be aggregated */
206 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
207 hlist_for_each_entry(forw_packet_pos, tmp_node,
208 &bat_priv->forw_bat_list, list) {
209 if (can_aggregate_with(batman_packet,
210 packet_len,
211 send_time,
212 direct_link,
213 if_incoming,
214 forw_packet_pos)) {
215 forw_packet_aggr = forw_packet_pos;
216 break;
217 }
218 }
219 }
220
221 /* nothing to aggregate with - either aggregation disabled or no
222 * suitable aggregation packet found */
223 if (!forw_packet_aggr) {
224 /* the following section can run without the lock */
225 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
226
227 /**
228 * if we could not aggregate this packet with one of the others
229 * we hold it back for a while, so that it might be aggregated
230 * later on
231 */
232 if ((!own_packet) &&
233 (atomic_read(&bat_priv->aggregated_ogms)))
234 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
235
236 new_aggregated_packet(packet_buff, packet_len,
237 send_time, direct_link,
238 if_incoming, own_packet);
239 } else {
240 aggregate(forw_packet_aggr,
241 packet_buff, packet_len,
242 direct_link);
243 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
244 }
245}
246
247/* unpack the aggregated packets and process them one by one */
248void receive_aggr_bat_packet(struct ethhdr *ethhdr, unsigned char *packet_buff,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000249 int packet_len, struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250{
251 struct batman_packet *batman_packet;
252 int buff_pos = 0;
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200253 unsigned char *tt_buff;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
255 batman_packet = (struct batman_packet *)packet_buff;
256
257 do {
258 /* network to host order for our 32bit seqno, and the
259 orig_interval. */
260 batman_packet->seqno = ntohl(batman_packet->seqno);
261
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200262 tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263 receive_bat_packet(ethhdr, batman_packet,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200264 tt_buff, tt_len(batman_packet),
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265 if_incoming);
266
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200267 buff_pos += BAT_PACKET_LEN + tt_len(batman_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268 batman_packet = (struct batman_packet *)
269 (packet_buff + buff_pos);
270 } while (aggregated_packet(buff_pos, packet_len,
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200271 batman_packet->num_tt));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272}