2 * Copyright (C) 2007-2009 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 (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
58 * check aggregation compatibility
59 * -> direct link packets are broadcasted on
60 * their interface only
61 * -> aggregate packet if the current packet is
62 * a "global" packet as well as the base
66 /* packets without direct link flag and high TTL
67 * are flooded through the net */
69 (!(batman_packet->flags & DIRECTLINK)) &&
70 (batman_packet->ttl != 1) &&
72 /* own packets originating non-primary
73 * interfaces leave only that interface */
74 ((!forw_packet->own) ||
75 (forw_packet->if_incoming->if_num == 0)))
78 /* if the incoming packet is sent via this one
79 * interface only - we still can aggregate */
81 (new_batman_packet->ttl == 1) &&
82 (forw_packet->if_incoming == if_incoming))
90 /* create a new aggregated packet and add this packet to it */
91 static void new_aggregated_packet(unsigned char *packet_buff,
93 unsigned long send_time,
95 struct batman_if *if_incoming,
98 struct forw_packet *forw_packet_aggr;
101 forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
102 if (!forw_packet_aggr)
105 forw_packet_aggr->packet_buff = kmalloc(MAX_AGGREGATION_BYTES,
107 if (!forw_packet_aggr->packet_buff) {
108 kfree(forw_packet_aggr);
112 INIT_HLIST_NODE(&forw_packet_aggr->list);
114 forw_packet_aggr->packet_len = packet_len;
115 memcpy(forw_packet_aggr->packet_buff,
117 forw_packet_aggr->packet_len);
119 forw_packet_aggr->skb = NULL;
120 forw_packet_aggr->own = own_packet;
121 forw_packet_aggr->if_incoming = if_incoming;
122 forw_packet_aggr->num_packets = 0;
123 forw_packet_aggr->direct_link_flags = 0;
124 forw_packet_aggr->send_time = send_time;
126 /* save packet direct link flag status */
128 forw_packet_aggr->direct_link_flags |= 1;
130 /* add new packet to packet list */
131 spin_lock_irqsave(&forw_bat_list_lock, flags);
132 hlist_add_head(&forw_packet_aggr->list, &forw_bat_list);
133 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
135 /* start timer for this packet */
136 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
137 send_outstanding_bat_packet);
138 queue_delayed_work(bat_event_workqueue,
139 &forw_packet_aggr->delayed_work,
140 send_time - jiffies);
143 /* aggregate a new packet into the existing aggregation */
144 static void aggregate(struct forw_packet *forw_packet_aggr,
145 unsigned char *packet_buff,
149 memcpy((forw_packet_aggr->packet_buff + forw_packet_aggr->packet_len),
150 packet_buff, packet_len);
151 forw_packet_aggr->packet_len += packet_len;
152 forw_packet_aggr->num_packets++;
154 /* save packet direct link flag status */
156 forw_packet_aggr->direct_link_flags |=
157 (1 << forw_packet_aggr->num_packets);
160 void add_bat_packet_to_list(unsigned char *packet_buff, int packet_len,
161 struct batman_if *if_incoming, char own_packet,
162 unsigned long send_time)
165 * _aggr -> pointer to the packet we want to aggregate with
166 * _pos -> pointer to the position in the queue
168 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
169 struct hlist_node *tmp_node;
170 struct batman_packet *batman_packet =
171 (struct batman_packet *)packet_buff;
172 bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
175 /* find position for the packet in the forward queue */
176 spin_lock_irqsave(&forw_bat_list_lock, flags);
177 /* own packets are not to be aggregated */
178 if ((atomic_read(&aggregation_enabled)) && (!own_packet)) {
179 hlist_for_each_entry(forw_packet_pos, tmp_node, &forw_bat_list,
181 if (can_aggregate_with(batman_packet,
187 forw_packet_aggr = forw_packet_pos;
193 /* nothing to aggregate with - either aggregation disabled or no
194 * suitable aggregation packet found */
195 if (forw_packet_aggr == NULL) {
196 /* the following section can run without the lock */
197 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
198 new_aggregated_packet(packet_buff, packet_len,
199 send_time, direct_link,
200 if_incoming, own_packet);
202 aggregate(forw_packet_aggr,
203 packet_buff, packet_len,
205 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
209 /* unpack the aggregated packets and process them one by one */
210 void receive_aggr_bat_packet(struct ethhdr *ethhdr, unsigned char *packet_buff,
211 int packet_len, struct batman_if *if_incoming)
213 struct batman_packet *batman_packet;
215 unsigned char *hna_buff;
217 batman_packet = (struct batman_packet *)packet_buff;
219 while (aggregated_packet(buff_pos, packet_len,
220 batman_packet->num_hna)) {
222 /* network to host order for our 16bit seqno, and the
224 batman_packet->seqno = ntohs(batman_packet->seqno);
226 hna_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
227 receive_bat_packet(ethhdr, batman_packet,
228 hna_buff, hna_len(batman_packet),
231 buff_pos += BAT_PACKET_LEN + hna_len(batman_packet);
232 batman_packet = (struct batman_packet *)
233 (packet_buff + buff_pos);