9c6e681f6fb65bc319944dcd3ea5d467bf0f9786
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / aggregation.c
1 /*
2  * Copyright (C) 2007-2009 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             (aggregated_bytes <= MAX_AGGREGATION_BYTES)) {
56
57                 /**
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
63                  *    packet
64                  */
65
66                 /* packets without direct link flag and high TTL
67                  * are flooded through the net  */
68                 if ((!directlink) &&
69                     (!(batman_packet->flags & DIRECTLINK)) &&
70                     (batman_packet->ttl != 1) &&
71
72                     /* own packets originating non-primary
73                      * interfaces leave only that interface */
74                     ((!forw_packet->own) ||
75                      (forw_packet->if_incoming->if_num == 0)))
76                         return true;
77
78                 /* if the incoming packet is sent via this one
79                  * interface only - we still can aggregate */
80                 if ((directlink) &&
81                     (new_batman_packet->ttl == 1) &&
82                     (forw_packet->if_incoming == if_incoming))
83                         return true;
84
85         }
86
87         return false;
88 }
89
90 /* create a new aggregated packet and add this packet to it */
91 static void new_aggregated_packet(unsigned char *packet_buff,
92                            int packet_len,
93                            unsigned long send_time,
94                            bool direct_link,
95                            struct batman_if *if_incoming,
96                            int own_packet)
97 {
98         struct forw_packet *forw_packet_aggr;
99
100         forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
101         if (!forw_packet_aggr)
102                 return;
103
104         forw_packet_aggr->packet_buff = kmalloc(MAX_AGGREGATION_BYTES,
105                                                 GFP_ATOMIC);
106         if (!forw_packet_aggr->packet_buff) {
107                 kfree(forw_packet_aggr);
108                 return;
109         }
110
111         INIT_HLIST_NODE(&forw_packet_aggr->list);
112
113         forw_packet_aggr->packet_len = packet_len;
114         memcpy(forw_packet_aggr->packet_buff,
115                packet_buff,
116                forw_packet_aggr->packet_len);
117
118         forw_packet_aggr->own = own_packet;
119         forw_packet_aggr->if_incoming = if_incoming;
120         forw_packet_aggr->num_packets = 0;
121         forw_packet_aggr->direct_link_flags = 0;
122         forw_packet_aggr->send_time = send_time;
123
124         /* save packet direct link flag status */
125         if (direct_link)
126                 forw_packet_aggr->direct_link_flags |= 1;
127
128         /* add new packet to packet list */
129         spin_lock(&forw_bat_list_lock);
130         hlist_add_head(&forw_packet_aggr->list, &forw_bat_list);
131         spin_unlock(&forw_bat_list_lock);
132
133         /* start timer for this packet */
134         INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
135                           send_outstanding_bat_packet);
136         queue_delayed_work(bat_event_workqueue,
137                            &forw_packet_aggr->delayed_work,
138                            send_time - jiffies);
139 }
140
141 /* aggregate a new packet into the existing aggregation */
142 static void aggregate(struct forw_packet *forw_packet_aggr,
143                       unsigned char *packet_buff,
144                       int packet_len,
145                       bool direct_link)
146 {
147         memcpy((forw_packet_aggr->packet_buff + forw_packet_aggr->packet_len),
148                packet_buff, packet_len);
149         forw_packet_aggr->packet_len += packet_len;
150         forw_packet_aggr->num_packets++;
151
152         /* save packet direct link flag status */
153         if (direct_link)
154                 forw_packet_aggr->direct_link_flags |=
155                         (1 << forw_packet_aggr->num_packets);
156 }
157
158 void add_bat_packet_to_list(unsigned char *packet_buff, int packet_len,
159                             struct batman_if *if_incoming, char own_packet,
160                             unsigned long send_time)
161 {
162         /**
163          * _aggr -> pointer to the packet we want to aggregate with
164          * _pos -> pointer to the position in the queue
165          */
166         struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL;
167         struct hlist_node *tmp_node;
168         struct batman_packet *batman_packet =
169                 (struct batman_packet *)packet_buff;
170         bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0;
171
172         /* find position for the packet in the forward queue */
173         spin_lock(&forw_bat_list_lock);
174         /* own packets are not to be aggregated */
175         if ((atomic_read(&aggregation_enabled)) && (!own_packet)) {
176                 hlist_for_each_entry(forw_packet_pos, tmp_node, &forw_bat_list,
177                                      list) {
178                         if (can_aggregate_with(batman_packet,
179                                                packet_len,
180                                                send_time,
181                                                direct_link,
182                                                if_incoming,
183                                                forw_packet_pos)) {
184                                 forw_packet_aggr = forw_packet_pos;
185                                 break;
186                         }
187                 }
188         }
189
190         /* nothing to aggregate with - either aggregation disabled or no
191          * suitable aggregation packet found */
192         if (forw_packet_aggr == NULL) {
193                 /* the following section can run without the lock */
194                 spin_unlock(&forw_bat_list_lock);
195                 new_aggregated_packet(packet_buff, packet_len,
196                                       send_time, direct_link,
197                                       if_incoming, own_packet);
198         } else {
199                 aggregate(forw_packet_aggr,
200                           packet_buff, packet_len,
201                           direct_link);
202                 spin_unlock(&forw_bat_list_lock);
203         }
204 }
205
206 /* unpack the aggregated packets and process them one by one */
207 void receive_aggr_bat_packet(struct ethhdr *ethhdr, unsigned char *packet_buff,
208                              int packet_len, struct batman_if *if_incoming)
209 {
210         struct batman_packet *batman_packet;
211         int buff_pos = 0;
212         unsigned char *hna_buff;
213
214         batman_packet = (struct batman_packet *)packet_buff;
215
216         while (aggregated_packet(buff_pos, packet_len,
217                                  batman_packet->num_hna)) {
218
219                 /* network to host order for our 16bit seqno, and the
220                    orig_interval. */
221                 batman_packet->seqno = ntohs(batman_packet->seqno);
222
223                 hna_buff = packet_buff + buff_pos + BAT_PACKET_LEN;
224                 receive_bat_packet(ethhdr, batman_packet,
225                                    hna_buff, hna_len(batman_packet),
226                                    if_incoming);
227
228                 buff_pos += BAT_PACKET_LEN + hna_len(batman_packet);
229                 batman_packet = (struct batman_packet *)
230                         (packet_buff + buff_pos);
231         }
232 }