Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[pandora-kernel.git] / net / batman-adv / aggregation.c
1 /*
2  * Copyright (C) 2007-2011 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 "translation-table.h"
24 #include "aggregation.h"
25 #include "send.h"
26 #include "routing.h"
27 #include "hard-interface.h"
28
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,
32                                int packet_len,
33                                unsigned long send_time,
34                                bool directlink,
35                                const struct hard_iface *if_incoming,
36                                const struct forw_packet *forw_packet)
37 {
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;
42         bool res = false;
43
44         /**
45          * we can aggregate the current packet to this aggregated packet
46          * if:
47          *
48          * - the send time is within our MAX_AGGREGATION_MS time
49          * - the resulting packet wont be bigger than
50          *   MAX_AGGREGATION_BYTES
51          */
52
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)) {
57
58                 /**
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
64                  *    packet
65                  */
66
67                 primary_if = primary_if_get_selected(bat_priv);
68                 if (!primary_if)
69                         goto out;
70
71                 /* packets without direct link flag and high TTL
72                  * are flooded through the net  */
73                 if ((!directlink) &&
74                     (!(batman_packet->flags & DIRECTLINK)) &&
75                     (batman_packet->ttl != 1) &&
76
77                     /* own packets originating non-primary
78                      * interfaces leave only that interface */
79                     ((!forw_packet->own) ||
80                      (forw_packet->if_incoming == primary_if))) {
81                         res = true;
82                         goto out;
83                 }
84
85                 /* if the incoming packet is sent via this one
86                  * interface only - we still can aggregate */
87                 if ((directlink) &&
88                     (new_batman_packet->ttl == 1) &&
89                     (forw_packet->if_incoming == if_incoming) &&
90
91                     /* packets from direct neighbors or
92                      * own secondary interface packets
93                      * (= secondary interface packets in general) */
94                     (batman_packet->flags & DIRECTLINK ||
95                      (forw_packet->own &&
96                       forw_packet->if_incoming != primary_if))) {
97                         res = true;
98                         goto out;
99                 }
100         }
101
102 out:
103         if (primary_if)
104                 hardif_free_ref(primary_if);
105         return res;
106 }
107
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,
111                                   bool direct_link,
112                                   struct hard_iface *if_incoming,
113                                   int own_packet)
114 {
115         struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
116         struct forw_packet *forw_packet_aggr;
117         unsigned char *skb_buff;
118
119         if (!atomic_inc_not_zero(&if_incoming->refcount))
120                 return;
121
122         /* own packet should always be scheduled */
123         if (!own_packet) {
124                 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
125                         bat_dbg(DBG_BATMAN, bat_priv,
126                                 "batman packet queue full\n");
127                         goto out;
128                 }
129         }
130
131         forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
132         if (!forw_packet_aggr) {
133                 if (!own_packet)
134                         atomic_inc(&bat_priv->batman_queue_left);
135                 goto out;
136         }
137
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));
142         else
143                 forw_packet_aggr->skb = dev_alloc_skb(packet_len +
144                                                       sizeof(struct ethhdr));
145
146         if (!forw_packet_aggr->skb) {
147                 if (!own_packet)
148                         atomic_inc(&bat_priv->batman_queue_left);
149                 kfree(forw_packet_aggr);
150                 goto out;
151         }
152         skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
153
154         INIT_HLIST_NODE(&forw_packet_aggr->list);
155
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);
159
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;
165
166         /* save packet direct link flag status */
167         if (direct_link)
168                 forw_packet_aggr->direct_link_flags |= 1;
169
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);
174
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);
181
182         return;
183 out:
184         hardif_free_ref(if_incoming);
185 }
186
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,
190                       bool direct_link)
191 {
192         unsigned char *skb_buff;
193
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++;
198
199         /* save packet direct link flag status */
200         if (direct_link)
201                 forw_packet_aggr->direct_link_flags |=
202                         (1 << forw_packet_aggr->num_packets);
203 }
204
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)
209 {
210         /**
211          * _aggr -> pointer to the packet we want to aggregate with
212          * _pos -> pointer to the position in the queue
213          */
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;
219
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,
227                                                bat_priv,
228                                                packet_len,
229                                                send_time,
230                                                direct_link,
231                                                if_incoming,
232                                                forw_packet_pos)) {
233                                 forw_packet_aggr = forw_packet_pos;
234                                 break;
235                         }
236                 }
237         }
238
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);
244
245                 /**
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
248                  * later on
249                  */
250                 if ((!own_packet) &&
251                     (atomic_read(&bat_priv->aggregated_ogms)))
252                         send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
253
254                 new_aggregated_packet(packet_buff, packet_len,
255                                       send_time, direct_link,
256                                       if_incoming, own_packet);
257         } else {
258                 aggregate(forw_packet_aggr,
259                           packet_buff, packet_len,
260                           direct_link);
261                 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
262         }
263 }
264
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)
269 {
270         struct batman_packet *batman_packet;
271         int buff_pos = 0;
272         unsigned char *tt_buff;
273
274         batman_packet = (struct batman_packet *)packet_buff;
275
276         do {
277                 /* network to host order for our 32bit seqno and the
278                    orig_interval */
279                 batman_packet->seqno = ntohl(batman_packet->seqno);
280                 batman_packet->tt_crc = ntohs(batman_packet->tt_crc);
281
282                 tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
283
284                 receive_bat_packet(ethhdr, batman_packet, tt_buff, if_incoming);
285
286                 buff_pos += BAT_PACKET_LEN +
287                         tt_len(batman_packet->tt_num_changes);
288
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));
293 }