Merge branch 'upstream/core' into upstream/xen
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / batman-adv / aggregation.c
1 /*
2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
3 *
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
27 /* calculate the size of the hna information for a given packet */
28 static int hna_len(struct batman_packet *batman_packet)
29 {
30 return batman_packet->num_hna * ETH_ALEN;
31 }
32
33 /* return true if new_packet can be aggregated with forw_packet */
34 static bool can_aggregate_with(struct batman_packet *new_batman_packet,
35 int packet_len,
36 unsigned long send_time,
37 bool directlink,
38 struct batman_if *if_incoming,
39 struct forw_packet *forw_packet)
40 {
41 struct batman_packet *batman_packet =
42 (struct batman_packet *)forw_packet->packet_buff;
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
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,
101 int packet_len,
102 unsigned long send_time,
103 bool direct_link,
104 struct batman_if *if_incoming,
105 int own_packet)
106 {
107 struct forw_packet *forw_packet_aggr;
108 unsigned long flags;
109
110 /* own packet should always be scheduled */
111 if (!own_packet) {
112 if (!atomic_dec_not_zero(&batman_queue_left)) {
113 bat_dbg(DBG_BATMAN, "batman packet queue full\n");
114 return;
115 }
116 }
117
118 forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
119 if (!forw_packet_aggr) {
120 if (!own_packet)
121 atomic_inc(&batman_queue_left);
122 return;
123 }
124
125 forw_packet_aggr->packet_buff = kmalloc(MAX_AGGREGATION_BYTES,
126 GFP_ATOMIC);
127 if (!forw_packet_aggr->packet_buff) {
128 if (!own_packet)
129 atomic_inc(&batman_queue_left);
130 kfree(forw_packet_aggr);
131 return;
132 }
133
134 INIT_HLIST_NODE(&forw_packet_aggr->list);
135
136 forw_packet_aggr->packet_len = packet_len;
137 memcpy(forw_packet_aggr->packet_buff,
138 packet_buff,
139 forw_packet_aggr->packet_len);
140
141 forw_packet_aggr->skb = NULL;
142 forw_packet_aggr->own = own_packet;
143 forw_packet_aggr->if_incoming = if_incoming;
144 forw_packet_aggr->num_packets = 0;
145 forw_packet_aggr->direct_link_flags = 0;
146 forw_packet_aggr->send_time = send_time;
147
148 /* save packet direct link flag status */
149 if (direct_link)
150 forw_packet_aggr->direct_link_flags |= 1;
151
152 /* add new packet to packet list */
153 spin_lock_irqsave(&forw_bat_list_lock, flags);
154 hlist_add_head(&forw_packet_aggr->list, &forw_bat_list);
155 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
156
157 /* start timer for this packet */
158 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
159 send_outstanding_bat_packet);
160 queue_delayed_work(bat_event_workqueue,
161 &forw_packet_aggr->delayed_work,
162 send_time - jiffies);
163 }
164
165 /* aggregate a new packet into the existing aggregation */
166 static void aggregate(struct forw_packet *forw_packet_aggr,
167 unsigned char *packet_buff,
168 int packet_len,
169 bool direct_link)
170 {
171 memcpy((forw_packet_aggr->packet_buff + forw_packet_aggr->packet_len),
172 packet_buff, packet_len);
173 forw_packet_aggr->packet_len += packet_len;
174 forw_packet_aggr->num_packets++;
175
176 /* save packet direct link flag status */
177 if (direct_link)
178 forw_packet_aggr->direct_link_flags |=
179 (1 << forw_packet_aggr->num_packets);
180 }
181
182 void add_bat_packet_to_list(struct bat_priv *bat_priv,
183 unsigned char *packet_buff, int packet_len,
184 struct batman_if *if_incoming, char own_packet,
185 unsigned long send_time)
186 {
187 /**
188 * _aggr -> pointer to the packet we want to aggregate with
189 * _pos -> pointer to the position in the queue
190 */
191 struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
192 struct hlist_node *tmp_node;
193 struct batman_packet *batman_packet =
194 (struct batman_packet *)packet_buff;
195 bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
196 unsigned long flags;
197
198 /* find position for the packet in the forward queue */
199 spin_lock_irqsave(&forw_bat_list_lock, flags);
200 /* own packets are not to be aggregated */
201 if ((atomic_read(&bat_priv->aggregation_enabled)) && (!own_packet)) {
202 hlist_for_each_entry(forw_packet_pos, tmp_node, &forw_bat_list,
203 list) {
204 if (can_aggregate_with(batman_packet,
205 packet_len,
206 send_time,
207 direct_link,
208 if_incoming,
209 forw_packet_pos)) {
210 forw_packet_aggr = forw_packet_pos;
211 break;
212 }
213 }
214 }
215
216 /* nothing to aggregate with - either aggregation disabled or no
217 * suitable aggregation packet found */
218 if (forw_packet_aggr == NULL) {
219 /* the following section can run without the lock */
220 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
221
222 /**
223 * if we could not aggregate this packet with one of the others
224 * we hold it back for a while, so that it might be aggregated
225 * later on
226 */
227 if ((!own_packet) &&
228 (atomic_read(&bat_priv->aggregation_enabled)))
229 send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
230
231 new_aggregated_packet(packet_buff, packet_len,
232 send_time, direct_link,
233 if_incoming, own_packet);
234 } else {
235 aggregate(forw_packet_aggr,
236 packet_buff, packet_len,
237 direct_link);
238 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
239 }
240 }
241
242 /* unpack the aggregated packets and process them one by one */
243 void receive_aggr_bat_packet(struct ethhdr *ethhdr, unsigned char *packet_buff,
244 int packet_len, struct batman_if *if_incoming)
245 {
246 struct batman_packet *batman_packet;
247 int buff_pos = 0;
248 unsigned char *hna_buff;
249
250 batman_packet = (struct batman_packet *)packet_buff;
251
252 while (aggregated_packet(buff_pos, packet_len,
253 batman_packet->num_hna)) {
254
255 /* network to host order for our 16bit seqno, and the
256 orig_interval. */
257 batman_packet->seqno = ntohs(batman_packet->seqno);
258
259 hna_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
260 receive_bat_packet(ethhdr, batman_packet,
261 hna_buff, hna_len(batman_packet),
262 if_incoming);
263
264 buff_pos += BAT_PACKET_LEN + hna_len(batman_packet);
265 batman_packet = (struct batman_packet *)
266 (packet_buff + buff_pos);
267 }
268 }