]>
Commit | Line | Data |
---|---|---|
5beef3c9 AL |
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) && | |
bd13b616 ML |
55 | time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS), |
56 | forw_packet->send_time) && | |
5beef3c9 AL |
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 | return true; | |
86 | ||
87 | } | |
88 | ||
89 | return false; | |
90 | } | |
91 | ||
92 | /* create a new aggregated packet and add this packet to it */ | |
93 | static void new_aggregated_packet(unsigned char *packet_buff, | |
94 | int packet_len, | |
95 | unsigned long send_time, | |
96 | bool direct_link, | |
97 | struct batman_if *if_incoming, | |
98 | int own_packet) | |
99 | { | |
100 | struct forw_packet *forw_packet_aggr; | |
e7017195 | 101 | unsigned long flags; |
5beef3c9 AL |
102 | |
103 | forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC); | |
104 | if (!forw_packet_aggr) | |
105 | return; | |
106 | ||
107 | forw_packet_aggr->packet_buff = kmalloc(MAX_AGGREGATION_BYTES, | |
108 | GFP_ATOMIC); | |
109 | if (!forw_packet_aggr->packet_buff) { | |
110 | kfree(forw_packet_aggr); | |
111 | return; | |
112 | } | |
113 | ||
114 | INIT_HLIST_NODE(&forw_packet_aggr->list); | |
115 | ||
116 | forw_packet_aggr->packet_len = packet_len; | |
117 | memcpy(forw_packet_aggr->packet_buff, | |
118 | packet_buff, | |
119 | forw_packet_aggr->packet_len); | |
120 | ||
e7017195 | 121 | forw_packet_aggr->skb = NULL; |
5beef3c9 AL |
122 | forw_packet_aggr->own = own_packet; |
123 | forw_packet_aggr->if_incoming = if_incoming; | |
124 | forw_packet_aggr->num_packets = 0; | |
125 | forw_packet_aggr->direct_link_flags = 0; | |
126 | forw_packet_aggr->send_time = send_time; | |
127 | ||
128 | /* save packet direct link flag status */ | |
129 | if (direct_link) | |
130 | forw_packet_aggr->direct_link_flags |= 1; | |
131 | ||
132 | /* add new packet to packet list */ | |
e7017195 | 133 | spin_lock_irqsave(&forw_bat_list_lock, flags); |
5beef3c9 | 134 | hlist_add_head(&forw_packet_aggr->list, &forw_bat_list); |
e7017195 | 135 | spin_unlock_irqrestore(&forw_bat_list_lock, flags); |
5beef3c9 AL |
136 | |
137 | /* start timer for this packet */ | |
138 | INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work, | |
139 | send_outstanding_bat_packet); | |
140 | queue_delayed_work(bat_event_workqueue, | |
141 | &forw_packet_aggr->delayed_work, | |
142 | send_time - jiffies); | |
143 | } | |
144 | ||
145 | /* aggregate a new packet into the existing aggregation */ | |
146 | static void aggregate(struct forw_packet *forw_packet_aggr, | |
147 | unsigned char *packet_buff, | |
148 | int packet_len, | |
149 | bool direct_link) | |
150 | { | |
151 | memcpy((forw_packet_aggr->packet_buff + forw_packet_aggr->packet_len), | |
152 | packet_buff, packet_len); | |
153 | forw_packet_aggr->packet_len += packet_len; | |
154 | forw_packet_aggr->num_packets++; | |
155 | ||
156 | /* save packet direct link flag status */ | |
157 | if (direct_link) | |
158 | forw_packet_aggr->direct_link_flags |= | |
159 | (1 << forw_packet_aggr->num_packets); | |
160 | } | |
161 | ||
162 | void add_bat_packet_to_list(unsigned char *packet_buff, int packet_len, | |
163 | struct batman_if *if_incoming, char own_packet, | |
164 | unsigned long send_time) | |
165 | { | |
166 | /** | |
167 | * _aggr -> pointer to the packet we want to aggregate with | |
168 | * _pos -> pointer to the position in the queue | |
169 | */ | |
170 | struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL; | |
171 | struct hlist_node *tmp_node; | |
172 | struct batman_packet *batman_packet = | |
173 | (struct batman_packet *)packet_buff; | |
174 | bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0; | |
e7017195 | 175 | unsigned long flags; |
5beef3c9 AL |
176 | |
177 | /* find position for the packet in the forward queue */ | |
e7017195 | 178 | spin_lock_irqsave(&forw_bat_list_lock, flags); |
5beef3c9 AL |
179 | /* own packets are not to be aggregated */ |
180 | if ((atomic_read(&aggregation_enabled)) && (!own_packet)) { | |
181 | hlist_for_each_entry(forw_packet_pos, tmp_node, &forw_bat_list, | |
182 | list) { | |
183 | if (can_aggregate_with(batman_packet, | |
184 | packet_len, | |
185 | send_time, | |
186 | direct_link, | |
187 | if_incoming, | |
188 | forw_packet_pos)) { | |
189 | forw_packet_aggr = forw_packet_pos; | |
190 | break; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | /* nothing to aggregate with - either aggregation disabled or no | |
196 | * suitable aggregation packet found */ | |
197 | if (forw_packet_aggr == NULL) { | |
198 | /* the following section can run without the lock */ | |
e7017195 | 199 | spin_unlock_irqrestore(&forw_bat_list_lock, flags); |
bd13b616 ML |
200 | |
201 | /** | |
202 | * if we could not aggregate this packet with one of the others | |
203 | * we hold it back for a while, so that it might be aggregated | |
204 | * later on | |
205 | */ | |
206 | if ((!own_packet) && | |
207 | (atomic_read(&bat_priv->aggregation_enabled))) | |
208 | send_time += msecs_to_jiffies(MAX_AGGREGATION_MS); | |
209 | ||
5beef3c9 AL |
210 | new_aggregated_packet(packet_buff, packet_len, |
211 | send_time, direct_link, | |
212 | if_incoming, own_packet); | |
213 | } else { | |
214 | aggregate(forw_packet_aggr, | |
215 | packet_buff, packet_len, | |
216 | direct_link); | |
e7017195 | 217 | spin_unlock_irqrestore(&forw_bat_list_lock, flags); |
5beef3c9 AL |
218 | } |
219 | } | |
220 | ||
221 | /* unpack the aggregated packets and process them one by one */ | |
222 | void receive_aggr_bat_packet(struct ethhdr *ethhdr, unsigned char *packet_buff, | |
223 | int packet_len, struct batman_if *if_incoming) | |
224 | { | |
225 | struct batman_packet *batman_packet; | |
226 | int buff_pos = 0; | |
227 | unsigned char *hna_buff; | |
228 | ||
229 | batman_packet = (struct batman_packet *)packet_buff; | |
230 | ||
231 | while (aggregated_packet(buff_pos, packet_len, | |
232 | batman_packet->num_hna)) { | |
233 | ||
234 | /* network to host order for our 16bit seqno, and the | |
235 | orig_interval. */ | |
236 | batman_packet->seqno = ntohs(batman_packet->seqno); | |
237 | ||
238 | hna_buff = packet_buff + buff_pos + BAT_PACKET_LEN; | |
239 | receive_bat_packet(ethhdr, batman_packet, | |
240 | hna_buff, hna_len(batman_packet), | |
241 | if_incoming); | |
242 | ||
243 | buff_pos += BAT_PACKET_LEN + hna_len(batman_packet); | |
244 | batman_packet = (struct batman_packet *) | |
245 | (packet_buff + buff_pos); | |
246 | } | |
247 | } |