hwmon: (sis5595) Use pr_fmt and pr_<level>
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / batman-adv / send.c
blob7adf76ddd0ba793202a20c70327f7d32baf17a39
1 /*
2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
22 #include "main.h"
23 #include "send.h"
24 #include "routing.h"
25 #include "translation-table.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "types.h"
29 #include "vis.h"
30 #include "aggregation.h"
33 static void send_outstanding_bcast_packet(struct work_struct *work);
35 /* apply hop penalty for a normal link */
36 static uint8_t hop_penalty(const uint8_t tq)
38 return (tq * (TQ_MAX_VALUE - TQ_HOP_PENALTY)) / (TQ_MAX_VALUE);
41 /* when do we schedule our own packet to be sent */
42 static unsigned long own_send_time(struct bat_priv *bat_priv)
44 return jiffies + msecs_to_jiffies(
45 atomic_read(&bat_priv->orig_interval) -
46 JITTER + (random32() % 2*JITTER));
49 /* when do we schedule a forwarded packet to be sent */
50 static unsigned long forward_send_time(struct bat_priv *bat_priv)
52 return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
55 /* send out an already prepared packet to the given address via the
56 * specified batman interface */
57 int send_skb_packet(struct sk_buff *skb,
58 struct batman_if *batman_if,
59 uint8_t *dst_addr)
61 struct ethhdr *ethhdr;
63 if (batman_if->if_status != IF_ACTIVE)
64 goto send_skb_err;
66 if (unlikely(!batman_if->net_dev))
67 goto send_skb_err;
69 if (!(batman_if->net_dev->flags & IFF_UP)) {
70 pr_warning("Interface %s is not up - can't send packet via "
71 "that interface!\n", batman_if->net_dev->name);
72 goto send_skb_err;
75 /* push to the ethernet header. */
76 if (my_skb_head_push(skb, sizeof(struct ethhdr)) < 0)
77 goto send_skb_err;
79 skb_reset_mac_header(skb);
81 ethhdr = (struct ethhdr *) skb_mac_header(skb);
82 memcpy(ethhdr->h_source, batman_if->net_dev->dev_addr, ETH_ALEN);
83 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
84 ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
86 skb_set_network_header(skb, ETH_HLEN);
87 skb->priority = TC_PRIO_CONTROL;
88 skb->protocol = __constant_htons(ETH_P_BATMAN);
90 skb->dev = batman_if->net_dev;
92 /* dev_queue_xmit() returns a negative result on error. However on
93 * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
94 * (which is > 0). This will not be treated as an error. */
96 return dev_queue_xmit(skb);
97 send_skb_err:
98 kfree_skb(skb);
99 return NET_XMIT_DROP;
102 /* Send a packet to a given interface */
103 static void send_packet_to_if(struct forw_packet *forw_packet,
104 struct batman_if *batman_if)
106 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
107 char *fwd_str;
108 uint8_t packet_num;
109 int16_t buff_pos;
110 struct batman_packet *batman_packet;
111 struct sk_buff *skb;
113 if (batman_if->if_status != IF_ACTIVE)
114 return;
116 packet_num = 0;
117 buff_pos = 0;
118 batman_packet = (struct batman_packet *)forw_packet->skb->data;
120 /* adjust all flags and log packets */
121 while (aggregated_packet(buff_pos,
122 forw_packet->packet_len,
123 batman_packet->num_hna)) {
125 /* we might have aggregated direct link packets with an
126 * ordinary base packet */
127 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
128 (forw_packet->if_incoming == batman_if))
129 batman_packet->flags |= DIRECTLINK;
130 else
131 batman_packet->flags &= ~DIRECTLINK;
133 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
134 "Sending own" :
135 "Forwarding"));
136 bat_dbg(DBG_BATMAN, bat_priv,
137 "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d,"
138 " IDF %s) on interface %s [%pM]\n",
139 fwd_str, (packet_num > 0 ? "aggregated " : ""),
140 batman_packet->orig, ntohl(batman_packet->seqno),
141 batman_packet->tq, batman_packet->ttl,
142 (batman_packet->flags & DIRECTLINK ?
143 "on" : "off"),
144 batman_if->net_dev->name, batman_if->net_dev->dev_addr);
146 buff_pos += sizeof(struct batman_packet) +
147 (batman_packet->num_hna * ETH_ALEN);
148 packet_num++;
149 batman_packet = (struct batman_packet *)
150 (forw_packet->skb->data + buff_pos);
153 /* create clone because function is called more than once */
154 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
155 if (skb)
156 send_skb_packet(skb, batman_if, broadcast_addr);
159 /* send a batman packet */
160 static void send_packet(struct forw_packet *forw_packet)
162 struct batman_if *batman_if;
163 struct net_device *soft_iface;
164 struct bat_priv *bat_priv;
165 struct batman_packet *batman_packet =
166 (struct batman_packet *)(forw_packet->skb->data);
167 unsigned char directlink = (batman_packet->flags & DIRECTLINK ? 1 : 0);
169 if (!forw_packet->if_incoming) {
170 pr_err("Error - can't forward packet: incoming iface not "
171 "specified\n");
172 return;
175 soft_iface = forw_packet->if_incoming->soft_iface;
176 bat_priv = netdev_priv(soft_iface);
178 if (forw_packet->if_incoming->if_status != IF_ACTIVE)
179 return;
181 /* multihomed peer assumed */
182 /* non-primary OGMs are only broadcasted on their interface */
183 if ((directlink && (batman_packet->ttl == 1)) ||
184 (forw_packet->own && (forw_packet->if_incoming->if_num > 0))) {
186 /* FIXME: what about aggregated packets ? */
187 bat_dbg(DBG_BATMAN, bat_priv,
188 "%s packet (originator %pM, seqno %d, TTL %d) "
189 "on interface %s [%pM]\n",
190 (forw_packet->own ? "Sending own" : "Forwarding"),
191 batman_packet->orig, ntohl(batman_packet->seqno),
192 batman_packet->ttl,
193 forw_packet->if_incoming->net_dev->name,
194 forw_packet->if_incoming->net_dev->dev_addr);
196 /* skb is only used once and than forw_packet is free'd */
197 send_skb_packet(forw_packet->skb, forw_packet->if_incoming,
198 broadcast_addr);
199 forw_packet->skb = NULL;
201 return;
204 /* broadcast on every interface */
205 rcu_read_lock();
206 list_for_each_entry_rcu(batman_if, &if_list, list) {
207 if (batman_if->soft_iface != soft_iface)
208 continue;
210 send_packet_to_if(forw_packet, batman_if);
212 rcu_read_unlock();
215 static void rebuild_batman_packet(struct bat_priv *bat_priv,
216 struct batman_if *batman_if)
218 int new_len;
219 unsigned char *new_buff;
220 struct batman_packet *batman_packet;
222 new_len = sizeof(struct batman_packet) +
223 (bat_priv->num_local_hna * ETH_ALEN);
224 new_buff = kmalloc(new_len, GFP_ATOMIC);
226 /* keep old buffer if kmalloc should fail */
227 if (new_buff) {
228 memcpy(new_buff, batman_if->packet_buff,
229 sizeof(struct batman_packet));
230 batman_packet = (struct batman_packet *)new_buff;
232 batman_packet->num_hna = hna_local_fill_buffer(bat_priv,
233 new_buff + sizeof(struct batman_packet),
234 new_len - sizeof(struct batman_packet));
236 kfree(batman_if->packet_buff);
237 batman_if->packet_buff = new_buff;
238 batman_if->packet_len = new_len;
242 void schedule_own_packet(struct batman_if *batman_if)
244 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
245 unsigned long send_time;
246 struct batman_packet *batman_packet;
247 int vis_server;
249 if ((batman_if->if_status == IF_NOT_IN_USE) ||
250 (batman_if->if_status == IF_TO_BE_REMOVED))
251 return;
253 vis_server = atomic_read(&bat_priv->vis_mode);
256 * the interface gets activated here to avoid race conditions between
257 * the moment of activating the interface in
258 * hardif_activate_interface() where the originator mac is set and
259 * outdated packets (especially uninitialized mac addresses) in the
260 * packet queue
262 if (batman_if->if_status == IF_TO_BE_ACTIVATED)
263 batman_if->if_status = IF_ACTIVE;
265 /* if local hna has changed and interface is a primary interface */
266 if ((atomic_read(&bat_priv->hna_local_changed)) &&
267 (batman_if == bat_priv->primary_if))
268 rebuild_batman_packet(bat_priv, batman_if);
271 * NOTE: packet_buff might just have been re-allocated in
272 * rebuild_batman_packet()
274 batman_packet = (struct batman_packet *)batman_if->packet_buff;
276 /* change sequence number to network order */
277 batman_packet->seqno =
278 htonl((uint32_t)atomic_read(&batman_if->seqno));
280 if (vis_server == VIS_TYPE_SERVER_SYNC)
281 batman_packet->flags |= VIS_SERVER;
282 else
283 batman_packet->flags &= ~VIS_SERVER;
285 atomic_inc(&batman_if->seqno);
287 slide_own_bcast_window(batman_if);
288 send_time = own_send_time(bat_priv);
289 add_bat_packet_to_list(bat_priv,
290 batman_if->packet_buff,
291 batman_if->packet_len,
292 batman_if, 1, send_time);
295 void schedule_forward_packet(struct orig_node *orig_node,
296 struct ethhdr *ethhdr,
297 struct batman_packet *batman_packet,
298 uint8_t directlink, int hna_buff_len,
299 struct batman_if *if_incoming)
301 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
302 unsigned char in_tq, in_ttl, tq_avg = 0;
303 unsigned long send_time;
305 if (batman_packet->ttl <= 1) {
306 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
307 return;
310 in_tq = batman_packet->tq;
311 in_ttl = batman_packet->ttl;
313 batman_packet->ttl--;
314 memcpy(batman_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
316 /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
317 * of our best tq value */
318 if ((orig_node->router) && (orig_node->router->tq_avg != 0)) {
320 /* rebroadcast ogm of best ranking neighbor as is */
321 if (!compare_orig(orig_node->router->addr, ethhdr->h_source)) {
322 batman_packet->tq = orig_node->router->tq_avg;
324 if (orig_node->router->last_ttl)
325 batman_packet->ttl = orig_node->router->last_ttl
326 - 1;
329 tq_avg = orig_node->router->tq_avg;
332 /* apply hop penalty */
333 batman_packet->tq = hop_penalty(batman_packet->tq);
335 bat_dbg(DBG_BATMAN, bat_priv,
336 "Forwarding packet: tq_orig: %i, tq_avg: %i, "
337 "tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
338 in_tq, tq_avg, batman_packet->tq, in_ttl - 1,
339 batman_packet->ttl);
341 batman_packet->seqno = htonl(batman_packet->seqno);
343 /* switch of primaries first hop flag when forwarding */
344 batman_packet->flags &= ~PRIMARIES_FIRST_HOP;
345 if (directlink)
346 batman_packet->flags |= DIRECTLINK;
347 else
348 batman_packet->flags &= ~DIRECTLINK;
350 send_time = forward_send_time(bat_priv);
351 add_bat_packet_to_list(bat_priv,
352 (unsigned char *)batman_packet,
353 sizeof(struct batman_packet) + hna_buff_len,
354 if_incoming, 0, send_time);
357 static void forw_packet_free(struct forw_packet *forw_packet)
359 if (forw_packet->skb)
360 kfree_skb(forw_packet->skb);
361 kfree(forw_packet);
364 static void _add_bcast_packet_to_list(struct bat_priv *bat_priv,
365 struct forw_packet *forw_packet,
366 unsigned long send_time)
368 unsigned long flags;
369 INIT_HLIST_NODE(&forw_packet->list);
371 /* add new packet to packet list */
372 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
373 hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
374 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
376 /* start timer for this packet */
377 INIT_DELAYED_WORK(&forw_packet->delayed_work,
378 send_outstanding_bcast_packet);
379 queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
380 send_time);
383 #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
384 /* add a broadcast packet to the queue and setup timers. broadcast packets
385 * are sent multiple times to increase probability for beeing received.
387 * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
388 * errors.
390 * The skb is not consumed, so the caller should make sure that the
391 * skb is freed. */
392 int add_bcast_packet_to_list(struct bat_priv *bat_priv, struct sk_buff *skb)
394 struct forw_packet *forw_packet;
395 struct bcast_packet *bcast_packet;
397 if (!atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
398 bat_dbg(DBG_BATMAN, bat_priv, "bcast packet queue full\n");
399 goto out;
402 if (!bat_priv->primary_if)
403 goto out;
405 forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
407 if (!forw_packet)
408 goto out_and_inc;
410 skb = skb_copy(skb, GFP_ATOMIC);
411 if (!skb)
412 goto packet_free;
414 /* as we have a copy now, it is safe to decrease the TTL */
415 bcast_packet = (struct bcast_packet *)skb->data;
416 bcast_packet->ttl--;
418 skb_reset_mac_header(skb);
420 forw_packet->skb = skb;
421 forw_packet->if_incoming = bat_priv->primary_if;
423 /* how often did we send the bcast packet ? */
424 forw_packet->num_packets = 0;
426 _add_bcast_packet_to_list(bat_priv, forw_packet, 1);
427 return NETDEV_TX_OK;
429 packet_free:
430 kfree(forw_packet);
431 out_and_inc:
432 atomic_inc(&bat_priv->bcast_queue_left);
433 out:
434 return NETDEV_TX_BUSY;
437 static void send_outstanding_bcast_packet(struct work_struct *work)
439 struct batman_if *batman_if;
440 struct delayed_work *delayed_work =
441 container_of(work, struct delayed_work, work);
442 struct forw_packet *forw_packet =
443 container_of(delayed_work, struct forw_packet, delayed_work);
444 unsigned long flags;
445 struct sk_buff *skb1;
446 struct net_device *soft_iface = forw_packet->if_incoming->soft_iface;
447 struct bat_priv *bat_priv = netdev_priv(soft_iface);
449 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
450 hlist_del(&forw_packet->list);
451 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
453 if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
454 goto out;
456 /* rebroadcast packet */
457 rcu_read_lock();
458 list_for_each_entry_rcu(batman_if, &if_list, list) {
459 if (batman_if->soft_iface != soft_iface)
460 continue;
462 /* send a copy of the saved skb */
463 skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
464 if (skb1)
465 send_skb_packet(skb1, batman_if, broadcast_addr);
467 rcu_read_unlock();
469 forw_packet->num_packets++;
471 /* if we still have some more bcasts to send */
472 if (forw_packet->num_packets < 3) {
473 _add_bcast_packet_to_list(bat_priv, forw_packet,
474 ((5 * HZ) / 1000));
475 return;
478 out:
479 forw_packet_free(forw_packet);
480 atomic_inc(&bat_priv->bcast_queue_left);
483 void send_outstanding_bat_packet(struct work_struct *work)
485 struct delayed_work *delayed_work =
486 container_of(work, struct delayed_work, work);
487 struct forw_packet *forw_packet =
488 container_of(delayed_work, struct forw_packet, delayed_work);
489 unsigned long flags;
490 struct bat_priv *bat_priv;
492 bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
493 spin_lock_irqsave(&bat_priv->forw_bat_list_lock, flags);
494 hlist_del(&forw_packet->list);
495 spin_unlock_irqrestore(&bat_priv->forw_bat_list_lock, flags);
497 if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
498 goto out;
500 send_packet(forw_packet);
503 * we have to have at least one packet in the queue
504 * to determine the queues wake up time unless we are
505 * shutting down
507 if (forw_packet->own)
508 schedule_own_packet(forw_packet->if_incoming);
510 out:
511 /* don't count own packet */
512 if (!forw_packet->own)
513 atomic_inc(&bat_priv->batman_queue_left);
515 forw_packet_free(forw_packet);
518 void purge_outstanding_packets(struct bat_priv *bat_priv,
519 struct batman_if *batman_if)
521 struct forw_packet *forw_packet;
522 struct hlist_node *tmp_node, *safe_tmp_node;
523 unsigned long flags;
525 if (batman_if)
526 bat_dbg(DBG_BATMAN, bat_priv,
527 "purge_outstanding_packets(): %s\n",
528 batman_if->net_dev->name);
529 else
530 bat_dbg(DBG_BATMAN, bat_priv,
531 "purge_outstanding_packets()\n");
533 /* free bcast list */
534 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
535 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
536 &bat_priv->forw_bcast_list, list) {
539 * if purge_outstanding_packets() was called with an argmument
540 * we delete only packets belonging to the given interface
542 if ((batman_if) &&
543 (forw_packet->if_incoming != batman_if))
544 continue;
546 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
549 * send_outstanding_bcast_packet() will lock the list to
550 * delete the item from the list
552 cancel_delayed_work_sync(&forw_packet->delayed_work);
553 spin_lock_irqsave(&bat_priv->forw_bcast_list_lock, flags);
555 spin_unlock_irqrestore(&bat_priv->forw_bcast_list_lock, flags);
557 /* free batman packet list */
558 spin_lock_irqsave(&bat_priv->forw_bat_list_lock, flags);
559 hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
560 &bat_priv->forw_bat_list, list) {
563 * if purge_outstanding_packets() was called with an argmument
564 * we delete only packets belonging to the given interface
566 if ((batman_if) &&
567 (forw_packet->if_incoming != batman_if))
568 continue;
570 spin_unlock_irqrestore(&bat_priv->forw_bat_list_lock, flags);
573 * send_outstanding_bat_packet() will lock the list to
574 * delete the item from the list
576 cancel_delayed_work_sync(&forw_packet->delayed_work);
577 spin_lock_irqsave(&bat_priv->forw_bat_list_lock, flags);
579 spin_unlock_irqrestore(&bat_priv->forw_bat_list_lock, flags);