Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 64afe35 | 2011-01-27 10:38:15 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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" |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 23 | #include "translation-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 24 | #include "aggregation.h" |
| 25 | #include "send.h" |
| 26 | #include "routing.h" |
Sven Eckelmann | 6d5808d | 2011-05-11 20:59:06 +0200 | [diff] [blame] | 27 | #include "hard-interface.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 28 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 29 | /* return true if new_packet can be aggregated with forw_packet */ |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 30 | static bool can_aggregate_with(const struct batman_packet *new_batman_packet, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 31 | int packet_len, |
| 32 | unsigned long send_time, |
| 33 | bool directlink, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 34 | const struct hard_iface *if_incoming, |
| 35 | const struct forw_packet *forw_packet) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 36 | { |
| 37 | struct batman_packet *batman_packet = |
| 38 | (struct batman_packet *)forw_packet->skb->data; |
| 39 | int aggregated_bytes = forw_packet->packet_len + packet_len; |
| 40 | |
| 41 | /** |
| 42 | * we can aggregate the current packet to this aggregated packet |
| 43 | * if: |
| 44 | * |
| 45 | * - the send time is within our MAX_AGGREGATION_MS time |
| 46 | * - the resulting packet wont be bigger than |
| 47 | * MAX_AGGREGATION_BYTES |
| 48 | */ |
| 49 | |
| 50 | if (time_before(send_time, forw_packet->send_time) && |
| 51 | time_after_eq(send_time + msecs_to_jiffies(MAX_AGGREGATION_MS), |
| 52 | forw_packet->send_time) && |
| 53 | (aggregated_bytes <= MAX_AGGREGATION_BYTES)) { |
| 54 | |
| 55 | /** |
| 56 | * check aggregation compatibility |
| 57 | * -> direct link packets are broadcasted on |
| 58 | * their interface only |
| 59 | * -> aggregate packet if the current packet is |
| 60 | * a "global" packet as well as the base |
| 61 | * packet |
| 62 | */ |
| 63 | |
| 64 | /* packets without direct link flag and high TTL |
| 65 | * are flooded through the net */ |
| 66 | if ((!directlink) && |
| 67 | (!(batman_packet->flags & DIRECTLINK)) && |
| 68 | (batman_packet->ttl != 1) && |
| 69 | |
| 70 | /* own packets originating non-primary |
| 71 | * interfaces leave only that interface */ |
| 72 | ((!forw_packet->own) || |
| 73 | (forw_packet->if_incoming->if_num == 0))) |
| 74 | return true; |
| 75 | |
| 76 | /* if the incoming packet is sent via this one |
| 77 | * interface only - we still can aggregate */ |
| 78 | if ((directlink) && |
| 79 | (new_batman_packet->ttl == 1) && |
| 80 | (forw_packet->if_incoming == if_incoming) && |
| 81 | |
| 82 | /* packets from direct neighbors or |
| 83 | * own secondary interface packets |
| 84 | * (= secondary interface packets in general) */ |
| 85 | (batman_packet->flags & DIRECTLINK || |
| 86 | (forw_packet->own && |
| 87 | forw_packet->if_incoming->if_num != 0))) |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 94 | /* create a new aggregated packet and add this packet to it */ |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 95 | static void new_aggregated_packet(const unsigned char *packet_buff, |
| 96 | int packet_len, unsigned long send_time, |
| 97 | bool direct_link, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 98 | struct hard_iface *if_incoming, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 99 | int own_packet) |
| 100 | { |
| 101 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 102 | struct forw_packet *forw_packet_aggr; |
| 103 | unsigned char *skb_buff; |
| 104 | |
Sven Eckelmann | 6d5808d | 2011-05-11 20:59:06 +0200 | [diff] [blame] | 105 | if (!atomic_inc_not_zero(&if_incoming->refcount)) |
| 106 | return; |
| 107 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 108 | /* own packet should always be scheduled */ |
| 109 | if (!own_packet) { |
| 110 | if (!atomic_dec_not_zero(&bat_priv->batman_queue_left)) { |
| 111 | bat_dbg(DBG_BATMAN, bat_priv, |
| 112 | "batman packet queue full\n"); |
Sven Eckelmann | 6d5808d | 2011-05-11 20:59:06 +0200 | [diff] [blame] | 113 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 117 | forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 118 | if (!forw_packet_aggr) { |
| 119 | if (!own_packet) |
| 120 | atomic_inc(&bat_priv->batman_queue_left); |
Sven Eckelmann | 6d5808d | 2011-05-11 20:59:06 +0200 | [diff] [blame] | 121 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | if ((atomic_read(&bat_priv->aggregated_ogms)) && |
| 125 | (packet_len < MAX_AGGREGATION_BYTES)) |
| 126 | forw_packet_aggr->skb = dev_alloc_skb(MAX_AGGREGATION_BYTES + |
| 127 | sizeof(struct ethhdr)); |
| 128 | else |
| 129 | forw_packet_aggr->skb = dev_alloc_skb(packet_len + |
| 130 | sizeof(struct ethhdr)); |
| 131 | |
| 132 | if (!forw_packet_aggr->skb) { |
| 133 | if (!own_packet) |
| 134 | atomic_inc(&bat_priv->batman_queue_left); |
| 135 | kfree(forw_packet_aggr); |
Sven Eckelmann | 6d5808d | 2011-05-11 20:59:06 +0200 | [diff] [blame] | 136 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 137 | } |
| 138 | skb_reserve(forw_packet_aggr->skb, sizeof(struct ethhdr)); |
| 139 | |
| 140 | INIT_HLIST_NODE(&forw_packet_aggr->list); |
| 141 | |
| 142 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 143 | forw_packet_aggr->packet_len = packet_len; |
| 144 | memcpy(skb_buff, packet_buff, packet_len); |
| 145 | |
| 146 | forw_packet_aggr->own = own_packet; |
| 147 | forw_packet_aggr->if_incoming = if_incoming; |
| 148 | forw_packet_aggr->num_packets = 0; |
Marek Lindner | ecbd532 | 2011-06-09 17:13:09 +0200 | [diff] [blame] | 149 | forw_packet_aggr->direct_link_flags = NO_FLAGS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 150 | forw_packet_aggr->send_time = send_time; |
| 151 | |
| 152 | /* save packet direct link flag status */ |
| 153 | if (direct_link) |
| 154 | forw_packet_aggr->direct_link_flags |= 1; |
| 155 | |
| 156 | /* add new packet to packet list */ |
| 157 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 158 | hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list); |
| 159 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 160 | |
| 161 | /* start timer for this packet */ |
| 162 | INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work, |
| 163 | send_outstanding_bat_packet); |
| 164 | queue_delayed_work(bat_event_workqueue, |
| 165 | &forw_packet_aggr->delayed_work, |
| 166 | send_time - jiffies); |
Sven Eckelmann | 6d5808d | 2011-05-11 20:59:06 +0200 | [diff] [blame] | 167 | |
| 168 | return; |
| 169 | out: |
| 170 | hardif_free_ref(if_incoming); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* aggregate a new packet into the existing aggregation */ |
| 174 | static void aggregate(struct forw_packet *forw_packet_aggr, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 175 | const unsigned char *packet_buff, int packet_len, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 176 | bool direct_link) |
| 177 | { |
| 178 | unsigned char *skb_buff; |
| 179 | |
| 180 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 181 | memcpy(skb_buff, packet_buff, packet_len); |
| 182 | forw_packet_aggr->packet_len += packet_len; |
| 183 | forw_packet_aggr->num_packets++; |
| 184 | |
| 185 | /* save packet direct link flag status */ |
| 186 | if (direct_link) |
| 187 | forw_packet_aggr->direct_link_flags |= |
| 188 | (1 << forw_packet_aggr->num_packets); |
| 189 | } |
| 190 | |
| 191 | void add_bat_packet_to_list(struct bat_priv *bat_priv, |
| 192 | unsigned char *packet_buff, int packet_len, |
Sven Eckelmann | b4e1705 | 2011-06-15 09:41:37 +0200 | [diff] [blame] | 193 | struct hard_iface *if_incoming, int own_packet, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 194 | unsigned long send_time) |
| 195 | { |
| 196 | /** |
| 197 | * _aggr -> pointer to the packet we want to aggregate with |
| 198 | * _pos -> pointer to the position in the queue |
| 199 | */ |
| 200 | struct forw_packet *forw_packet_aggr = NULL, *forw_packet_pos = NULL; |
| 201 | struct hlist_node *tmp_node; |
| 202 | struct batman_packet *batman_packet = |
| 203 | (struct batman_packet *)packet_buff; |
| 204 | bool direct_link = batman_packet->flags & DIRECTLINK ? 1 : 0; |
| 205 | |
| 206 | /* find position for the packet in the forward queue */ |
| 207 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 208 | /* own packets are not to be aggregated */ |
| 209 | if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) { |
| 210 | hlist_for_each_entry(forw_packet_pos, tmp_node, |
| 211 | &bat_priv->forw_bat_list, list) { |
| 212 | if (can_aggregate_with(batman_packet, |
| 213 | packet_len, |
| 214 | send_time, |
| 215 | direct_link, |
| 216 | if_incoming, |
| 217 | forw_packet_pos)) { |
| 218 | forw_packet_aggr = forw_packet_pos; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* nothing to aggregate with - either aggregation disabled or no |
| 225 | * suitable aggregation packet found */ |
| 226 | if (!forw_packet_aggr) { |
| 227 | /* the following section can run without the lock */ |
| 228 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 229 | |
| 230 | /** |
| 231 | * if we could not aggregate this packet with one of the others |
| 232 | * we hold it back for a while, so that it might be aggregated |
| 233 | * later on |
| 234 | */ |
| 235 | if ((!own_packet) && |
| 236 | (atomic_read(&bat_priv->aggregated_ogms))) |
| 237 | send_time += msecs_to_jiffies(MAX_AGGREGATION_MS); |
| 238 | |
| 239 | new_aggregated_packet(packet_buff, packet_len, |
| 240 | send_time, direct_link, |
| 241 | if_incoming, own_packet); |
| 242 | } else { |
| 243 | aggregate(forw_packet_aggr, |
| 244 | packet_buff, packet_len, |
| 245 | direct_link); |
| 246 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* unpack the aggregated packets and process them one by one */ |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 251 | void receive_aggr_bat_packet(const struct ethhdr *ethhdr, |
| 252 | unsigned char *packet_buff, int packet_len, |
| 253 | struct hard_iface *if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 254 | { |
| 255 | struct batman_packet *batman_packet; |
| 256 | int buff_pos = 0; |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 257 | unsigned char *tt_buff; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 258 | |
| 259 | batman_packet = (struct batman_packet *)packet_buff; |
| 260 | |
| 261 | do { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 262 | /* network to host order for our 32bit seqno and the |
| 263 | orig_interval */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 264 | batman_packet->seqno = ntohl(batman_packet->seqno); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 265 | batman_packet->tt_crc = ntohs(batman_packet->tt_crc); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 266 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 267 | tt_buff = packet_buff + buff_pos + BAT_PACKET_LEN; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 268 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 269 | receive_bat_packet(ethhdr, batman_packet, tt_buff, if_incoming); |
| 270 | |
| 271 | buff_pos += BAT_PACKET_LEN + |
| 272 | tt_len(batman_packet->tt_num_changes); |
| 273 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 274 | batman_packet = (struct batman_packet *) |
| 275 | (packet_buff + buff_pos); |
| 276 | } while (aggregated_packet(buff_pos, packet_len, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 277 | batman_packet->tt_num_changes)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 278 | } |