2 * Copyright (C) 2007-2010 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"
27 /* calculate the size of the hna information for a given packet */
28 static int hna_len(struct batman_packet
*batman_packet
)
30 return batman_packet
->num_hna
* ETH_ALEN
;
33 /* return true if new_packet can be aggregated with forw_packet */
34 static bool can_aggregate_with(struct batman_packet
*new_batman_packet
,
36 unsigned long send_time
,
38 struct batman_if
*if_incoming
,
39 struct forw_packet
*forw_packet
)
41 struct batman_packet
*batman_packet
=
42 (struct batman_packet
*)forw_packet
->packet_buff
;
43 int aggregated_bytes
= forw_packet
->packet_len
+ packet_len
;
46 * we can aggregate the current packet to this aggregated packet
49 * - the send time is within our MAX_AGGREGATION_MS time
50 * - the resulting packet wont be bigger than
51 * MAX_AGGREGATION_BYTES
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
)) {
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
68 /* packets without direct link flag and high TTL
69 * are flooded through the net */
71 (!(batman_packet
->flags
& DIRECTLINK
)) &&
72 (batman_packet
->ttl
!= 1) &&
74 /* own packets originating non-primary
75 * interfaces leave only that interface */
76 ((!forw_packet
->own
) ||
77 (forw_packet
->if_incoming
->if_num
== 0)))
80 /* if the incoming packet is sent via this one
81 * interface only - we still can aggregate */
83 (new_batman_packet
->ttl
== 1) &&
84 (forw_packet
->if_incoming
== if_incoming
) &&
86 /* packets from direct neighbors or
87 * own secondary interface packets
88 * (= secondary interface packets in general) */
89 (batman_packet
->flags
& DIRECTLINK
||
91 forw_packet
->if_incoming
->if_num
!= 0)))
98 #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
99 /* create a new aggregated packet and add this packet to it */
100 static void new_aggregated_packet(unsigned char *packet_buff
,
102 unsigned long send_time
,
104 struct batman_if
*if_incoming
,
107 struct forw_packet
*forw_packet_aggr
;
109 /* FIXME: each batman_if will be attached to a softif */
110 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
112 /* own packet should always be scheduled */
114 if (!atomic_dec_not_zero(&batman_queue_left
)) {
115 bat_dbg(DBG_BATMAN
, bat_priv
,
116 "batman packet queue full\n");
121 forw_packet_aggr
= kmalloc(sizeof(struct forw_packet
), GFP_ATOMIC
);
122 if (!forw_packet_aggr
) {
124 atomic_inc(&batman_queue_left
);
128 forw_packet_aggr
->packet_buff
= kmalloc(MAX_AGGREGATION_BYTES
,
130 if (!forw_packet_aggr
->packet_buff
) {
132 atomic_inc(&batman_queue_left
);
133 kfree(forw_packet_aggr
);
137 INIT_HLIST_NODE(&forw_packet_aggr
->list
);
139 forw_packet_aggr
->packet_len
= packet_len
;
140 memcpy(forw_packet_aggr
->packet_buff
,
142 forw_packet_aggr
->packet_len
);
144 forw_packet_aggr
->skb
= NULL
;
145 forw_packet_aggr
->own
= own_packet
;
146 forw_packet_aggr
->if_incoming
= if_incoming
;
147 forw_packet_aggr
->num_packets
= 0;
148 forw_packet_aggr
->direct_link_flags
= 0;
149 forw_packet_aggr
->send_time
= send_time
;
151 /* save packet direct link flag status */
153 forw_packet_aggr
->direct_link_flags
|= 1;
155 /* add new packet to packet list */
156 spin_lock_irqsave(&forw_bat_list_lock
, flags
);
157 hlist_add_head(&forw_packet_aggr
->list
, &forw_bat_list
);
158 spin_unlock_irqrestore(&forw_bat_list_lock
, flags
);
160 /* start timer for this packet */
161 INIT_DELAYED_WORK(&forw_packet_aggr
->delayed_work
,
162 send_outstanding_bat_packet
);
163 queue_delayed_work(bat_event_workqueue
,
164 &forw_packet_aggr
->delayed_work
,
165 send_time
- jiffies
);
168 /* aggregate a new packet into the existing aggregation */
169 static void aggregate(struct forw_packet
*forw_packet_aggr
,
170 unsigned char *packet_buff
,
174 memcpy((forw_packet_aggr
->packet_buff
+ forw_packet_aggr
->packet_len
),
175 packet_buff
, packet_len
);
176 forw_packet_aggr
->packet_len
+= packet_len
;
177 forw_packet_aggr
->num_packets
++;
179 /* save packet direct link flag status */
181 forw_packet_aggr
->direct_link_flags
|=
182 (1 << forw_packet_aggr
->num_packets
);
185 void add_bat_packet_to_list(struct bat_priv
*bat_priv
,
186 unsigned char *packet_buff
, int packet_len
,
187 struct batman_if
*if_incoming
, char own_packet
,
188 unsigned long send_time
)
191 * _aggr -> pointer to the packet we want to aggregate with
192 * _pos -> pointer to the position in the queue
194 struct forw_packet
*forw_packet_aggr
= NULL
, *forw_packet_pos
= NULL
;
195 struct hlist_node
*tmp_node
;
196 struct batman_packet
*batman_packet
=
197 (struct batman_packet
*)packet_buff
;
198 bool direct_link
= batman_packet
->flags
& DIRECTLINK
? 1 : 0;
201 /* find position for the packet in the forward queue */
202 spin_lock_irqsave(&forw_bat_list_lock
, flags
);
203 /* own packets are not to be aggregated */
204 if ((atomic_read(&bat_priv
->aggregation_enabled
)) && (!own_packet
)) {
205 hlist_for_each_entry(forw_packet_pos
, tmp_node
, &forw_bat_list
,
207 if (can_aggregate_with(batman_packet
,
213 forw_packet_aggr
= forw_packet_pos
;
219 /* nothing to aggregate with - either aggregation disabled or no
220 * suitable aggregation packet found */
221 if (forw_packet_aggr
== NULL
) {
222 /* the following section can run without the lock */
223 spin_unlock_irqrestore(&forw_bat_list_lock
, flags
);
226 * if we could not aggregate this packet with one of the others
227 * we hold it back for a while, so that it might be aggregated
231 (atomic_read(&bat_priv
->aggregation_enabled
)))
232 send_time
+= msecs_to_jiffies(MAX_AGGREGATION_MS
);
234 new_aggregated_packet(packet_buff
, packet_len
,
235 send_time
, direct_link
,
236 if_incoming
, own_packet
);
238 aggregate(forw_packet_aggr
,
239 packet_buff
, packet_len
,
241 spin_unlock_irqrestore(&forw_bat_list_lock
, flags
);
245 /* unpack the aggregated packets and process them one by one */
246 void receive_aggr_bat_packet(struct ethhdr
*ethhdr
, unsigned char *packet_buff
,
247 int packet_len
, struct batman_if
*if_incoming
)
249 struct batman_packet
*batman_packet
;
251 unsigned char *hna_buff
;
253 batman_packet
= (struct batman_packet
*)packet_buff
;
255 while (aggregated_packet(buff_pos
, packet_len
,
256 batman_packet
->num_hna
)) {
258 /* network to host order for our 32bit seqno, and the
260 batman_packet
->seqno
= ntohl(batman_packet
->seqno
);
262 hna_buff
= packet_buff
+ buff_pos
+ BAT_PACKET_LEN
;
263 receive_bat_packet(ethhdr
, batman_packet
,
264 hna_buff
, hna_len(batman_packet
),
267 buff_pos
+= BAT_PACKET_LEN
+ hna_len(batman_packet
);
268 batman_packet
= (struct batman_packet
*)
269 (packet_buff
+ buff_pos
);