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 "aggregation.h"
26 #include "hard-interface.h"
28 /* calculate the size of the tt information for a given packet */
29 static int tt_len(const struct batman_packet
*batman_packet
)
31 return batman_packet
->num_tt
* ETH_ALEN
;
34 /* return true if new_packet can be aggregated with forw_packet */
35 static bool can_aggregate_with(const struct batman_packet
*new_batman_packet
,
37 unsigned long send_time
,
39 const struct hard_iface
*if_incoming
,
40 const struct forw_packet
*forw_packet
)
42 struct batman_packet
*batman_packet
=
43 (struct batman_packet
*)forw_packet
->skb
->data
;
44 int aggregated_bytes
= forw_packet
->packet_len
+ packet_len
;
47 * we can aggregate the current packet to this aggregated packet
50 * - the send time is within our MAX_AGGREGATION_MS time
51 * - the resulting packet wont be bigger than
52 * MAX_AGGREGATION_BYTES
55 if (time_before(send_time
, forw_packet
->send_time
) &&
56 time_after_eq(send_time
+ msecs_to_jiffies(MAX_AGGREGATION_MS
),
57 forw_packet
->send_time
) &&
58 (aggregated_bytes
<= MAX_AGGREGATION_BYTES
)) {
61 * check aggregation compatibility
62 * -> direct link packets are broadcasted on
63 * their interface only
64 * -> aggregate packet if the current packet is
65 * a "global" packet as well as the base
69 /* packets without direct link flag and high TTL
70 * are flooded through the net */
72 (!(batman_packet
->flags
& DIRECTLINK
)) &&
73 (batman_packet
->ttl
!= 1) &&
75 /* own packets originating non-primary
76 * interfaces leave only that interface */
77 ((!forw_packet
->own
) ||
78 (forw_packet
->if_incoming
->if_num
== 0)))
81 /* if the incoming packet is sent via this one
82 * interface only - we still can aggregate */
84 (new_batman_packet
->ttl
== 1) &&
85 (forw_packet
->if_incoming
== if_incoming
) &&
87 /* packets from direct neighbors or
88 * own secondary interface packets
89 * (= secondary interface packets in general) */
90 (batman_packet
->flags
& DIRECTLINK
||
92 forw_packet
->if_incoming
->if_num
!= 0)))
99 /* create a new aggregated packet and add this packet to it */
100 static void new_aggregated_packet(const unsigned char *packet_buff
,
101 int packet_len
, unsigned long send_time
,
103 struct hard_iface
*if_incoming
,
106 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
107 struct forw_packet
*forw_packet_aggr
;
108 unsigned char *skb_buff
;
110 if (!atomic_inc_not_zero(&if_incoming
->refcount
))
113 /* own packet should always be scheduled */
115 if (!atomic_dec_not_zero(&bat_priv
->batman_queue_left
)) {
116 bat_dbg(DBG_BATMAN
, bat_priv
,
117 "batman packet queue full\n");
122 forw_packet_aggr
= kmalloc(sizeof(struct forw_packet
), GFP_ATOMIC
);
123 if (!forw_packet_aggr
) {
125 atomic_inc(&bat_priv
->batman_queue_left
);
129 if ((atomic_read(&bat_priv
->aggregated_ogms
)) &&
130 (packet_len
< MAX_AGGREGATION_BYTES
))
131 forw_packet_aggr
->skb
= dev_alloc_skb(MAX_AGGREGATION_BYTES
+
132 sizeof(struct ethhdr
));
134 forw_packet_aggr
->skb
= dev_alloc_skb(packet_len
+
135 sizeof(struct ethhdr
));
137 if (!forw_packet_aggr
->skb
) {
139 atomic_inc(&bat_priv
->batman_queue_left
);
140 kfree(forw_packet_aggr
);
143 skb_reserve(forw_packet_aggr
->skb
, sizeof(struct ethhdr
));
145 INIT_HLIST_NODE(&forw_packet_aggr
->list
);
147 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
148 forw_packet_aggr
->packet_len
= packet_len
;
149 memcpy(skb_buff
, packet_buff
, packet_len
);
151 forw_packet_aggr
->own
= own_packet
;
152 forw_packet_aggr
->if_incoming
= if_incoming
;
153 forw_packet_aggr
->num_packets
= 0;
154 forw_packet_aggr
->direct_link_flags
= 0;
155 forw_packet_aggr
->send_time
= send_time
;
157 /* save packet direct link flag status */
159 forw_packet_aggr
->direct_link_flags
|= 1;
161 /* add new packet to packet list */
162 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
163 hlist_add_head(&forw_packet_aggr
->list
, &bat_priv
->forw_bat_list
);
164 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
166 /* start timer for this packet */
167 INIT_DELAYED_WORK(&forw_packet_aggr
->delayed_work
,
168 send_outstanding_bat_packet
);
169 queue_delayed_work(bat_event_workqueue
,
170 &forw_packet_aggr
->delayed_work
,
171 send_time
- jiffies
);
175 hardif_free_ref(if_incoming
);
178 /* aggregate a new packet into the existing aggregation */
179 static void aggregate(struct forw_packet
*forw_packet_aggr
,
180 const unsigned char *packet_buff
, int packet_len
,
183 unsigned char *skb_buff
;
185 skb_buff
= skb_put(forw_packet_aggr
->skb
, packet_len
);
186 memcpy(skb_buff
, packet_buff
, packet_len
);
187 forw_packet_aggr
->packet_len
+= packet_len
;
188 forw_packet_aggr
->num_packets
++;
190 /* save packet direct link flag status */
192 forw_packet_aggr
->direct_link_flags
|=
193 (1 << forw_packet_aggr
->num_packets
);
196 void add_bat_packet_to_list(struct bat_priv
*bat_priv
,
197 unsigned char *packet_buff
, int packet_len
,
198 struct hard_iface
*if_incoming
, char own_packet
,
199 unsigned long send_time
)
202 * _aggr -> pointer to the packet we want to aggregate with
203 * _pos -> pointer to the position in the queue
205 struct forw_packet
*forw_packet_aggr
= NULL
, *forw_packet_pos
= NULL
;
206 struct hlist_node
*tmp_node
;
207 struct batman_packet
*batman_packet
=
208 (struct batman_packet
*)packet_buff
;
209 bool direct_link
= batman_packet
->flags
& DIRECTLINK
? 1 : 0;
211 /* find position for the packet in the forward queue */
212 spin_lock_bh(&bat_priv
->forw_bat_list_lock
);
213 /* own packets are not to be aggregated */
214 if ((atomic_read(&bat_priv
->aggregated_ogms
)) && (!own_packet
)) {
215 hlist_for_each_entry(forw_packet_pos
, tmp_node
,
216 &bat_priv
->forw_bat_list
, list
) {
217 if (can_aggregate_with(batman_packet
,
223 forw_packet_aggr
= forw_packet_pos
;
229 /* nothing to aggregate with - either aggregation disabled or no
230 * suitable aggregation packet found */
231 if (!forw_packet_aggr
) {
232 /* the following section can run without the lock */
233 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
236 * if we could not aggregate this packet with one of the others
237 * we hold it back for a while, so that it might be aggregated
241 (atomic_read(&bat_priv
->aggregated_ogms
)))
242 send_time
+= msecs_to_jiffies(MAX_AGGREGATION_MS
);
244 new_aggregated_packet(packet_buff
, packet_len
,
245 send_time
, direct_link
,
246 if_incoming
, own_packet
);
248 aggregate(forw_packet_aggr
,
249 packet_buff
, packet_len
,
251 spin_unlock_bh(&bat_priv
->forw_bat_list_lock
);
255 /* unpack the aggregated packets and process them one by one */
256 void receive_aggr_bat_packet(const struct ethhdr
*ethhdr
,
257 unsigned char *packet_buff
, int packet_len
,
258 struct hard_iface
*if_incoming
)
260 struct batman_packet
*batman_packet
;
262 unsigned char *tt_buff
;
264 batman_packet
= (struct batman_packet
*)packet_buff
;
267 /* network to host order for our 32bit seqno, and the
269 batman_packet
->seqno
= ntohl(batman_packet
->seqno
);
271 tt_buff
= packet_buff
+ buff_pos
+ BAT_PACKET_LEN
;
272 receive_bat_packet(ethhdr
, batman_packet
,
273 tt_buff
, tt_len(batman_packet
),
276 buff_pos
+= BAT_PACKET_LEN
+ tt_len(batman_packet
);
277 batman_packet
= (struct batman_packet
*)
278 (packet_buff
+ buff_pos
);
279 } while (aggregated_packet(buff_pos
, packet_len
,
280 batman_packet
->num_tt
));