2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
23 #include "translation-table.h"
24 #include "aggregation.h"
27 #include "hard-interface.h"
29 /* return true if new_packet can be aggregated with forw_packet */
30 static bool can_aggregate_with(const struct batman_packet
*new_batman_packet
,
31 struct bat_priv
*bat_priv
,
33 unsigned long send_time
,
35 const struct hard_iface
*if_incoming
,
36 const struct forw_packet
*forw_packet
)
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 struct hard_iface
*primary_if
= NULL
;
45 * we can aggregate the current packet to this aggregated packet
48 * - the send time is within our MAX_AGGREGATION_MS time
49 * - the resulting packet wont be bigger than
50 * MAX_AGGREGATION_BYTES
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
)) {
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
67 primary_if
= primary_if_get_selected(bat_priv
);
71 /* packets without direct link flag and high TTL
72 * are flooded through the net */
74 (!(batman_packet
->flags
& DIRECTLINK
)) &&
75 (batman_packet
->ttl
!= 1) &&
77 /* own packets originating non-primary
78 * interfaces leave only that interface */
79 ((!forw_packet
->own
) ||
80 (forw_packet
->if_incoming
== primary_if
))) {
85 /* if the incoming packet is sent via this one
86 * interface only - we still can aggregate */
88 (new_batman_packet
->ttl
== 1) &&
89 (forw_packet
->if_incoming
== if_incoming
) &&
91 /* packets from direct neighbors or
92 * own secondary interface packets
93 * (= secondary interface packets in general) */
94 (batman_packet
->flags
& DIRECTLINK
||
96 forw_packet
->if_incoming
!= primary_if
))) {
104 hardif_free_ref(primary_if
);
108 /* create a new aggregated packet and add this packet to it */
109 static void new_aggregated_packet(const unsigned char *packet_buff
,
110 int packet_len
, unsigned long send_time
,
112 struct hard_iface
*if_incoming
,
115 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
116 struct forw_packet
*forw_packet_aggr
;
117 unsigned char *skb_buff
;
119 if (!atomic_inc_not_zero(&if_incoming
->refcount
))
122 /* own packet should always be scheduled */
124 if (!atomic_dec_not_zero(&bat_priv
->batman_queue_left
)) {
125 bat_dbg(DBG_BATMAN
, bat_priv
,
126 "batman packet queue full\n");
131 forw_packet_aggr
= kmalloc(sizeof(*forw_packet_aggr
), GFP_ATOMIC
);
132 if (!forw_packet_aggr
) {
134 atomic_inc(&bat_priv
->batman_queue_left
);
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
));
143 forw_packet_aggr
->skb
= dev_alloc_skb(packet_len
+
144 sizeof(struct ethhdr
));
146 if (!forw_packet_aggr
->skb
) {
148 atomic_inc(&bat_priv
->batman_queue_left
);
149 kfree(forw_packet_aggr
);
152 skb_reserve(forw_packet_aggr
->skb
, sizeof(struct ethhdr
));
154 INIT_HLIST_NODE(&forw_packet_aggr
->list
);
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
);
160 forw_packet_aggr
->own
= own_packet
;
161 forw_packet_aggr
->if_incoming
= if_incoming
;
162 forw_packet_aggr
->num_packets
= 0;
163 forw_packet_aggr
->direct_link_flags
= NO_FLAGS
;
164 forw_packet_aggr
->send_time
= send_time
;
166 /* save packet direct link flag status */
168 forw_packet_aggr
->direct_link_flags
|= 1;
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
);
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
);
184 hardif_free_ref(if_incoming
);
187 /* aggregate a new packet into the existing aggregation */
188 static void aggregate(struct forw_packet
*forw_packet_aggr
,
189 const unsigned char *packet_buff
, int packet_len
,
192 unsigned char *skb_buff
;
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
++;
199 /* save packet direct link flag status */
201 forw_packet_aggr
->direct_link_flags
|=
202 (1 << forw_packet_aggr
->num_packets
);
205 void add_bat_packet_to_list(struct bat_priv
*bat_priv
,
206 unsigned char *packet_buff
, int packet_len
,
207 struct hard_iface
*if_incoming
, int own_packet
,
208 unsigned long send_time
)
211 * _aggr -> pointer to the packet we want to aggregate with
212 * _pos -> pointer to the position in the queue
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;
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
,
233 forw_packet_aggr
= forw_packet_pos
;
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
);
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
251 (atomic_read(&bat_priv
->aggregated_ogms
)))
252 send_time
+= msecs_to_jiffies(MAX_AGGREGATION_MS
);
254 new_aggregated_packet(packet_buff
, packet_len
,
255 send_time
, direct_link
,
256 if_incoming
, own_packet
);
258 aggregate(forw_packet_aggr
,
259 packet_buff
, packet_len
,
261 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
265 /* unpack the aggregated packets and process them one by one */
266 void receive_aggr_bat_packet(const struct ethhdr
*ethhdr
,
267 unsigned char *packet_buff
, int packet_len
,
268 struct hard_iface
*if_incoming
)
270 struct batman_packet
*batman_packet
;
272 unsigned char *tt_buff
;
274 batman_packet
= (struct batman_packet
*)packet_buff
;
277 /* network to host order for our 32bit seqno and the
279 batman_packet
->seqno
= ntohl(batman_packet
->seqno
);
280 batman_packet
->tt_crc
= ntohs(batman_packet
->tt_crc
);
282 tt_buff
= packet_buff
+ buff_pos
+ BAT_PACKET_LEN
;
284 receive_bat_packet(ethhdr
, batman_packet
, tt_buff
, if_incoming
);
286 buff_pos
+= BAT_PACKET_LEN
+
287 tt_len(batman_packet
->tt_num_changes
);
289 batman_packet
= (struct batman_packet
*)
290 (packet_buff
+ buff_pos
);
291 } while (aggregated_packet(buff_pos
, packet_len
,
292 batman_packet
->tt_num_changes
));