batman-adv: Add const type qualifier for pointers
[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 "aggregation.h"
24 #include "send.h"
25 #include "routing.h"
26 #include "hard-interface.h"
27
28 /* calculate the size of the tt information for a given packet */
29 static int tt_len(const struct batman_packet *batman_packet)
30 {
31         return batman_packet->num_tt * ETH_ALEN;
32 }
33
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,
36                                int packet_len,
37                                unsigned long send_time,
38                                bool directlink,
39                                const struct hard_iface *if_incoming,
40                                const struct forw_packet *forw_packet)
41 {
42         struct batman_packet *batman_packet =
43                 (struct batman_packet *)forw_packet->skb->data;
44         int aggregated_bytes = forw_packet->packet_len + packet_len;
45
46         /**
47          * we can aggregate the current packet to this aggregated packet
48          * if:
49          *
50          * - the send time is within our MAX_AGGREGATION_MS time
51          * - the resulting packet wont be bigger than
52          *   MAX_AGGREGATION_BYTES
53          */
54
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)) {
59
60                 /**
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
66                  *    packet
67                  */
68
69                 /* packets without direct link flag and high TTL
70                  * are flooded through the net  */
71                 if ((!directlink) &&
72                     (!(batman_packet->flags & DIRECTLINK)) &&
73                     (batman_packet->ttl != 1) &&
74
75                     /* own packets originating non-primary
76                      * interfaces leave only that interface */
77                     ((!forw_packet->own) ||
78                      (forw_packet->if_incoming->if_num == 0)))
79                         return true;
80
81                 /* if the incoming packet is sent via this one
82                  * interface only - we still can aggregate */
83                 if ((directlink) &&
84                     (new_batman_packet->ttl == 1) &&
85                     (forw_packet->if_incoming == if_incoming) &&
86
87                     /* packets from direct neighbors or
88                      * own secondary interface packets
89                      * (= secondary interface packets in general) */
90                     (batman_packet->flags & DIRECTLINK ||
91                      (forw_packet->own &&
92                       forw_packet->if_incoming->if_num != 0)))
93                         return true;
94         }
95
96         return false;
97 }
98
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,
102                                   bool direct_link,
103                                   struct hard_iface *if_incoming,
104                                   int own_packet)
105 {
106         struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
107         struct forw_packet *forw_packet_aggr;
108         unsigned char *skb_buff;
109
110         if (!atomic_inc_not_zero(&if_incoming->refcount))
111                 return;
112
113         /* own packet should always be scheduled */
114         if (!own_packet) {
115                 if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
116                         bat_dbg(DBG_BATMAN, bat_priv,
117                                 "batman packet queue full\n");
118                         goto out;
119                 }
120         }
121
122         forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
123         if (!forw_packet_aggr) {
124                 if (!own_packet)
125                         atomic_inc(&bat_priv->batman_queue_left);
126                 goto out;
127         }
128
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));
133         else
134                 forw_packet_aggr->skb = dev_alloc_skb(packet_len +
135                                                       sizeof(struct ethhdr));
136
137         if (!forw_packet_aggr->skb) {
138                 if (!own_packet)
139                         atomic_inc(&bat_priv->batman_queue_left);
140                 kfree(forw_packet_aggr);
141                 goto out;
142         }
143         skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr));
144
145         INIT_HLIST_NODE(&forw_packet_aggr->list);
146
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);
150
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;
156
157         /* save packet direct link flag status */
158         if (direct_link)
159                 forw_packet_aggr->direct_link_flags |= 1;
160
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);
165
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);
172
173         return;
174 out:
175         hardif_free_ref(if_incoming);
176 }
177
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,
181                       bool direct_link)
182 {
183         unsigned char *skb_buff;
184
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++;
189
190         /* save packet direct link flag status */
191         if (direct_link)
192                 forw_packet_aggr->direct_link_flags |=
193                         (1 << forw_packet_aggr->num_packets);
194 }
195
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)
200 {
201         /**
202          * _aggr -> pointer to the packet we want to aggregate with
203          * _pos -> pointer to the position in the queue
204          */
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;
210
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,
218                                                packet_len,
219                                                send_time,
220                                                direct_link,
221                                                if_incoming,
222                                                forw_packet_pos)) {
223                                 forw_packet_aggr = forw_packet_pos;
224                                 break;
225                         }
226                 }
227         }
228
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);
234
235                 /**
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
238                  * later on
239                  */
240                 if ((!own_packet) &&
241                     (atomic_read(&bat_priv->aggregated_ogms)))
242                         send_time += msecs_to_jiffies(MAX_AGGREGATION_MS);
243
244                 new_aggregated_packet(packet_buff, packet_len,
245                                       send_time, direct_link,
246                                       if_incoming, own_packet);
247         } else {
248                 aggregate(forw_packet_aggr,
249                           packet_buff, packet_len,
250                           direct_link);
251                 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
252         }
253 }
254
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)
259 {
260         struct batman_packet *batman_packet;
261         int buff_pos = 0;
262         unsigned char *tt_buff;
263
264         batman_packet = (struct batman_packet *)packet_buff;
265
266         do {
267                 /* network to host order for our 32bit seqno, and the
268                    orig_interval. */
269                 batman_packet->seqno = ntohl(batman_packet->seqno);
270
271                 tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
272                 receive_bat_packet(ethhdr, batman_packet,
273                                    tt_buff, tt_len(batman_packet),
274                                    if_incoming);
275
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));
281 }