1 /* Copyright 2011-2014 Autronica Fire and Security AS
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
11 * This file contains device methods for creating, using and destroying
12 * virtual HSR devices.
15 #include <linux/netdevice.h>
16 #include <linux/skbuff.h>
17 #include <linux/etherdevice.h>
18 #include <linux/if_arp.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/pkt_sched.h>
21 #include "hsr_device.h"
22 #include "hsr_slave.h"
23 #include "hsr_framereg.h"
27 static bool is_admin_up(struct net_device *dev)
29 return dev && (dev->flags & IFF_UP);
32 static bool is_slave_up(struct net_device *dev)
34 return dev && is_admin_up(dev) && netif_oper_up(dev);
37 static void __hsr_set_operstate(struct net_device *dev, int transition)
39 write_lock_bh(&dev_base_lock);
40 if (dev->operstate != transition) {
41 dev->operstate = transition;
42 write_unlock_bh(&dev_base_lock);
43 netdev_state_change(dev);
45 write_unlock_bh(&dev_base_lock);
49 static void hsr_set_operstate(struct net_device *hsr_dev, bool has_carrier)
51 if (!is_admin_up(hsr_dev)) {
52 __hsr_set_operstate(hsr_dev, IF_OPER_DOWN);
57 __hsr_set_operstate(hsr_dev, IF_OPER_UP);
59 __hsr_set_operstate(hsr_dev, IF_OPER_LOWERLAYERDOWN);
62 static bool hsr_check_carrier(struct hsr_priv *hsr)
66 has_carrier = (is_slave_up(hsr->slave[0]) || is_slave_up(hsr->slave[1]));
69 netif_carrier_on(hsr->dev);
71 netif_carrier_off(hsr->dev);
77 static void hsr_check_announce(struct net_device *hsr_dev,
78 unsigned char old_operstate)
82 hsr = netdev_priv(hsr_dev);
84 if ((hsr_dev->operstate == IF_OPER_UP) && (old_operstate != IF_OPER_UP)) {
86 hsr->announce_count = 0;
87 hsr->announce_timer.expires = jiffies +
88 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
89 add_timer(&hsr->announce_timer);
92 if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP))
94 del_timer(&hsr->announce_timer);
97 void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
99 unsigned char old_operstate;
102 /* netif_stacked_transfer_operstate() cannot be used here since
103 * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
105 old_operstate = hsr->dev->operstate;
106 has_carrier = hsr_check_carrier(hsr);
107 hsr_set_operstate(hsr->dev, has_carrier);
108 hsr_check_announce(hsr->dev, old_operstate);
112 int hsr_get_max_mtu(struct hsr_priv *hsr)
116 if (hsr->slave[0] && hsr->slave[1])
117 mtu_max = min(hsr->slave[0]->mtu, hsr->slave[1]->mtu);
118 else if (hsr->slave[0])
119 mtu_max = hsr->slave[0]->mtu;
120 else if (hsr->slave[1])
121 mtu_max = hsr->slave[1]->mtu;
125 return mtu_max - HSR_HLEN;
128 static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
130 struct hsr_priv *hsr;
132 hsr = netdev_priv(dev);
134 if (new_mtu > hsr_get_max_mtu(hsr)) {
135 netdev_info(hsr->dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
145 static int hsr_dev_open(struct net_device *dev)
147 struct hsr_priv *hsr;
151 hsr = netdev_priv(dev);
153 for (i = 0; i < HSR_MAX_SLAVE; i++) {
155 slave_name = hsr->slave[i]->name;
159 if (!is_slave_up(hsr->slave[i]))
160 netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a working HSR network\n",
161 'A' + i, slave_name);
167 static int hsr_dev_close(struct net_device *dev)
169 /* Nothing to do here. We could try to restore the state of the slaves
170 * to what they were before being changed by the hsr master dev's state,
171 * but they might have been changed manually in the mean time too, so
172 * taking them up or down here might be confusing and is probably not a
179 static void hsr_fill_tag(struct hsr_ethhdr *hsr_ethhdr, struct hsr_priv *hsr)
181 unsigned long irqflags;
183 /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values
184 * between 0001-1001 ("ring identifier", for regular HSR frames),
185 * or 1111 ("HSR management", supervision frames). Unfortunately, the
186 * spec writers forgot to explain what a "ring identifier" is, or
187 * how it is used. So we just set this to 0001 for regular frames,
188 * and 1111 for supervision frames.
190 set_hsr_tag_path(&hsr_ethhdr->hsr_tag, 0x1);
192 /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet
193 * frame is the content of the frame located between the Length/Type
194 * field and the Frame Check Sequence."
196 * IEC 62439-3, p 48, specifies the "original LPDU" to include the
197 * original "LT" field (what "LT" means is not explained anywhere as
198 * far as I can see - perhaps "Length/Type"?). So LSDU_size might
199 * equal original length + 2.
200 * Also, the fact that this field is not used anywhere (might be used
201 * by a RedBox connecting HSR and PRP nets?) means I cannot test its
202 * correctness. Instead of guessing, I set this to 0 here, to make any
203 * problems immediately apparent. Anyone using this driver with PRP/HSR
204 * RedBoxes might need to fix this...
206 set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, 0);
208 spin_lock_irqsave(&hsr->seqnr_lock, irqflags);
209 hsr_ethhdr->hsr_tag.sequence_nr = htons(hsr->sequence_nr);
211 spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags);
213 hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto;
215 hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP);
218 static int slave_xmit(struct sk_buff *skb, struct hsr_priv *hsr,
219 enum hsr_dev_idx dev_idx)
221 struct hsr_ethhdr *hsr_ethhdr;
223 hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
225 skb->dev = hsr->slave[dev_idx];
227 hsr_addr_subst_dest(hsr, &hsr_ethhdr->ethhdr, dev_idx);
229 /* Address substitution (IEC62439-3 pp 26, 50): replace mac
230 * address of outgoing frame with that of the outgoing slave's.
232 ether_addr_copy(hsr_ethhdr->ethhdr.h_source, skb->dev->dev_addr);
234 return dev_queue_xmit(skb);
238 static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
240 struct hsr_priv *hsr;
241 struct hsr_ethhdr *hsr_ethhdr;
242 struct sk_buff *skb2;
245 hsr = netdev_priv(dev);
246 hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
248 if ((skb->protocol != htons(ETH_P_PRP)) ||
249 (hsr_ethhdr->ethhdr.h_proto != htons(ETH_P_PRP))) {
250 hsr_fill_tag(hsr_ethhdr, hsr);
251 skb->protocol = htons(ETH_P_PRP);
254 skb2 = pskb_copy(skb, GFP_ATOMIC);
256 res1 = NET_XMIT_DROP;
257 if (likely(hsr->slave[HSR_DEV_SLAVE_A]))
258 res1 = slave_xmit(skb, hsr, HSR_DEV_SLAVE_A);
260 res2 = NET_XMIT_DROP;
261 if (likely(skb2 && hsr->slave[HSR_DEV_SLAVE_B]))
262 res2 = slave_xmit(skb2, hsr, HSR_DEV_SLAVE_B);
264 if (likely(res1 == NET_XMIT_SUCCESS || res1 == NET_XMIT_CN ||
265 res2 == NET_XMIT_SUCCESS || res2 == NET_XMIT_CN)) {
266 hsr->dev->stats.tx_packets++;
267 hsr->dev->stats.tx_bytes += skb->len;
269 hsr->dev->stats.tx_dropped++;
276 static int hsr_header_create(struct sk_buff *skb, struct net_device *dev,
277 unsigned short type, const void *daddr,
278 const void *saddr, unsigned int len)
282 /* Make room for the HSR tag now. We will fill it in later (in
285 if (skb_headroom(skb) < HSR_HLEN + ETH_HLEN)
287 skb_push(skb, HSR_HLEN);
289 /* To allow VLAN/HSR combos we should probably use
290 * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_HLEN);
291 * here instead. It would require other changes too, though - e.g.
292 * separate headers for each slave etc...
294 res = eth_header(skb, dev, type, daddr, saddr, len + HSR_HLEN);
297 skb_reset_mac_header(skb);
299 return res + HSR_HLEN;
303 static const struct header_ops hsr_header_ops = {
304 .create = hsr_header_create,
305 .parse = eth_header_parse,
309 /* HSR:2010 supervision frames should be padded so that the whole frame,
310 * including headers and FCS, is 64 bytes (without VLAN).
312 static int hsr_pad(int size)
314 const int min_size = ETH_ZLEN - HSR_HLEN - ETH_HLEN;
316 if (size >= min_size)
321 static void send_hsr_supervision_frame(struct net_device *hsr_dev, u8 type)
323 struct hsr_priv *hsr;
326 struct hsr_sup_tag *hsr_stag;
327 struct hsr_sup_payload *hsr_sp;
328 unsigned long irqflags;
330 hlen = LL_RESERVED_SPACE(hsr_dev);
331 tlen = hsr_dev->needed_tailroom;
332 skb = alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload)) + hlen + tlen,
338 hsr = netdev_priv(hsr_dev);
340 skb_reserve(skb, hlen);
343 skb->protocol = htons(ETH_P_PRP);
344 skb->priority = TC_PRIO_CONTROL;
346 if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
347 hsr->sup_multicast_addr,
348 skb->dev->dev_addr, skb->len) < 0)
351 skb_pull(skb, sizeof(struct ethhdr));
352 hsr_stag = (typeof(hsr_stag)) skb->data;
354 set_hsr_stag_path(hsr_stag, 0xf);
355 set_hsr_stag_HSR_Ver(hsr_stag, 0);
357 spin_lock_irqsave(&hsr->seqnr_lock, irqflags);
358 hsr_stag->sequence_nr = htons(hsr->sequence_nr);
360 spin_unlock_irqrestore(&hsr->seqnr_lock, irqflags);
362 hsr_stag->HSR_TLV_Type = type;
363 hsr_stag->HSR_TLV_Length = 12;
365 skb_push(skb, sizeof(struct ethhdr));
367 /* Payload: MacAddressA */
368 hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(*hsr_sp));
369 ether_addr_copy(hsr_sp->MacAddressA, hsr_dev->dev_addr);
379 /* Announce (supervision frame) timer function
381 static void hsr_announce(unsigned long data)
383 struct hsr_priv *hsr;
385 hsr = (struct hsr_priv *) data;
387 if (hsr->announce_count < 3) {
388 send_hsr_supervision_frame(hsr->dev, HSR_TLV_ANNOUNCE);
389 hsr->announce_count++;
391 send_hsr_supervision_frame(hsr->dev, HSR_TLV_LIFE_CHECK);
394 if (hsr->announce_count < 3)
395 hsr->announce_timer.expires = jiffies +
396 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
398 hsr->announce_timer.expires = jiffies +
399 msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
401 if (is_admin_up(hsr->dev))
402 add_timer(&hsr->announce_timer);
406 static void restore_slaves(struct net_device *hsr_dev)
408 struct hsr_priv *hsr;
412 hsr = netdev_priv(hsr_dev);
416 for (i = 0; i < HSR_MAX_SLAVE; i++) {
419 res = dev_set_promiscuity(hsr->slave[i], -1);
422 "Cannot restore slave promiscuity (%s, %d)\n",
423 hsr->slave[i]->name, res);
425 if (hsr->slave[i]->rx_handler == hsr_handle_frame)
426 netdev_rx_handler_unregister(hsr->slave[i]);
433 static void reclaim_hsr_dev(struct rcu_head *rh)
435 struct hsr_priv *hsr;
437 hsr = container_of(rh, struct hsr_priv, rcu_head);
438 free_netdev(hsr->dev);
442 /* According to comments in the declaration of struct net_device, this function
443 * is "Called from unregister, can be used to call free_netdev". Ok then...
445 static void hsr_dev_destroy(struct net_device *hsr_dev)
447 struct hsr_priv *hsr;
449 hsr = netdev_priv(hsr_dev);
451 del_timer_sync(&hsr->prune_timer);
452 del_timer_sync(&hsr->announce_timer);
453 unregister_hsr_master(hsr); /* calls list_del_rcu on hsr */
454 restore_slaves(hsr_dev);
455 call_rcu(&hsr->rcu_head, reclaim_hsr_dev); /* reclaim hsr */
458 static const struct net_device_ops hsr_device_ops = {
459 .ndo_change_mtu = hsr_dev_change_mtu,
460 .ndo_open = hsr_dev_open,
461 .ndo_stop = hsr_dev_close,
462 .ndo_start_xmit = hsr_dev_xmit,
466 void hsr_dev_setup(struct net_device *dev)
468 random_ether_addr(dev->dev_addr);
471 dev->header_ops = &hsr_header_ops;
472 dev->netdev_ops = &hsr_device_ops;
473 dev->tx_queue_len = 0;
475 dev->destructor = hsr_dev_destroy;
479 /* Return true if dev is a HSR master; return false otherwise.
481 bool is_hsr_master(struct net_device *dev)
483 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
486 static int check_slave_ok(struct net_device *dev)
488 /* Don't allow HSR on non-ethernet like devices */
489 if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) ||
490 (dev->addr_len != ETH_ALEN)) {
491 netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n");
495 /* Don't allow enslaving hsr devices */
496 if (is_hsr_master(dev)) {
497 netdev_info(dev, "Cannot create trees of HSR devices.\n");
501 if (is_hsr_slave(dev)) {
502 netdev_info(dev, "This device is already a HSR slave.\n");
506 if (dev->priv_flags & IFF_802_1Q_VLAN) {
507 netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n");
511 /* HSR over bonded devices has not been tested, but I'm not sure it
519 /* Default multicast address for HSR Supervision frames */
520 static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
521 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
524 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
525 unsigned char multicast_spec)
527 struct hsr_priv *hsr;
531 hsr = netdev_priv(hsr_dev);
533 INIT_LIST_HEAD(&hsr->node_db);
534 INIT_LIST_HEAD(&hsr->self_node_db);
535 for (i = 0; i < HSR_MAX_SLAVE; i++)
536 hsr->slave[i] = slave[i];
538 spin_lock_init(&hsr->seqnr_lock);
539 /* Overflow soon to find bugs easier: */
540 hsr->sequence_nr = USHRT_MAX - 1024;
542 init_timer(&hsr->announce_timer);
543 hsr->announce_timer.function = hsr_announce;
544 hsr->announce_timer.data = (unsigned long) hsr;
546 init_timer(&hsr->prune_timer);
547 hsr->prune_timer.function = hsr_prune_nodes;
548 hsr->prune_timer.data = (unsigned long) hsr;
550 ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
551 hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
553 /* FIXME: should I modify the value of these?
555 * - hsr_dev->flags - i.e.
557 * - hsr_dev->priv_flags - i.e.
559 * IFF_TX_SKB_SHARING?
560 * IFF_HSR_MASTER/SLAVE?
563 for (i = 0; i < HSR_MAX_SLAVE; i++) {
564 res = check_slave_ok(slave[i]);
569 hsr_dev->features = slave[0]->features & slave[1]->features;
570 /* Prevent recursive tx locking */
571 hsr_dev->features |= NETIF_F_LLTX;
572 /* VLAN on top of HSR needs testing and probably some work on
573 * hsr_header_create() etc.
575 hsr_dev->features |= NETIF_F_VLAN_CHALLENGED;
577 /* Set hsr_dev's MAC address to that of mac_slave1 */
578 ether_addr_copy(hsr_dev->dev_addr, hsr->slave[0]->dev_addr);
580 /* Set required header length */
581 for (i = 0; i < HSR_MAX_SLAVE; i++) {
582 if (slave[i]->hard_header_len + HSR_HLEN >
583 hsr_dev->hard_header_len)
584 hsr_dev->hard_header_len =
585 slave[i]->hard_header_len + HSR_HLEN;
589 for (i = 0; i < HSR_MAX_SLAVE; i++)
590 if (slave[i]->mtu - HSR_HLEN < hsr_dev->mtu)
591 hsr_dev->mtu = slave[i]->mtu - HSR_HLEN;
593 /* Make sure the 1st call to netif_carrier_on() gets through */
594 netif_carrier_off(hsr_dev);
597 for (i = 0; i < HSR_MAX_SLAVE; i++) {
598 res = dev_set_promiscuity(slave[i], 1);
600 netdev_info(hsr_dev, "Cannot set slave promiscuity (%s, %d)\n",
601 slave[i]->name, res);
606 for (i = 0; i < HSR_MAX_SLAVE; i++) {
607 res = netdev_rx_handler_register(slave[i], hsr_handle_frame,
613 /* Make sure we recognize frames from ourselves in hsr_rcv() */
614 res = hsr_create_self_node(&hsr->self_node_db, hsr_dev->dev_addr,
615 hsr->slave[1]->dev_addr);
619 res = register_netdevice(hsr_dev);
623 hsr->prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD);
624 add_timer(&hsr->prune_timer);
626 register_hsr_master(hsr);
631 restore_slaves(hsr_dev);