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/rtnetlink.h>
19 #include <linux/pkt_sched.h>
20 #include "hsr_device.h"
21 #include "hsr_slave.h"
22 #include "hsr_framereg.h"
26 static bool is_admin_up(struct net_device
*dev
)
28 return dev
&& (dev
->flags
& IFF_UP
);
31 static bool is_slave_up(struct net_device
*dev
)
33 return dev
&& is_admin_up(dev
) && netif_oper_up(dev
);
36 static void __hsr_set_operstate(struct net_device
*dev
, int transition
)
38 write_lock_bh(&dev_base_lock
);
39 if (dev
->operstate
!= transition
) {
40 dev
->operstate
= transition
;
41 write_unlock_bh(&dev_base_lock
);
42 netdev_state_change(dev
);
44 write_unlock_bh(&dev_base_lock
);
48 static void hsr_set_operstate(struct hsr_port
*master
, bool has_carrier
)
50 if (!is_admin_up(master
->dev
)) {
51 __hsr_set_operstate(master
->dev
, IF_OPER_DOWN
);
56 __hsr_set_operstate(master
->dev
, IF_OPER_UP
);
58 __hsr_set_operstate(master
->dev
, IF_OPER_LOWERLAYERDOWN
);
61 static bool hsr_check_carrier(struct hsr_port
*master
)
63 struct hsr_port
*port
;
69 hsr_for_each_port(master
->hsr
, port
)
70 if ((port
->type
!= HSR_PT_MASTER
) && is_slave_up(port
->dev
)) {
77 netif_carrier_on(master
->dev
);
79 netif_carrier_off(master
->dev
);
85 static void hsr_check_announce(struct net_device
*hsr_dev
,
86 unsigned char old_operstate
)
90 hsr
= netdev_priv(hsr_dev
);
92 if ((hsr_dev
->operstate
== IF_OPER_UP
) && (old_operstate
!= IF_OPER_UP
)) {
94 hsr
->announce_count
= 0;
95 hsr
->announce_timer
.expires
= jiffies
+
96 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL
);
97 add_timer(&hsr
->announce_timer
);
100 if ((hsr_dev
->operstate
!= IF_OPER_UP
) && (old_operstate
== IF_OPER_UP
))
102 del_timer(&hsr
->announce_timer
);
105 void hsr_check_carrier_and_operstate(struct hsr_priv
*hsr
)
107 struct hsr_port
*master
;
108 unsigned char old_operstate
;
111 master
= hsr_port_get_hsr(hsr
, HSR_PT_MASTER
);
112 /* netif_stacked_transfer_operstate() cannot be used here since
113 * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
115 old_operstate
= master
->dev
->operstate
;
116 has_carrier
= hsr_check_carrier(master
);
117 hsr_set_operstate(master
, has_carrier
);
118 hsr_check_announce(master
->dev
, old_operstate
);
121 int hsr_get_max_mtu(struct hsr_priv
*hsr
)
123 unsigned int mtu_max
;
124 struct hsr_port
*port
;
126 mtu_max
= ETH_DATA_LEN
;
128 hsr_for_each_port(hsr
, port
)
129 if (port
->type
!= HSR_PT_MASTER
)
130 mtu_max
= min(port
->dev
->mtu
, mtu_max
);
133 if (mtu_max
< HSR_HLEN
)
135 return mtu_max
- HSR_HLEN
;
139 static int hsr_dev_change_mtu(struct net_device
*dev
, int new_mtu
)
141 struct hsr_priv
*hsr
;
142 struct hsr_port
*master
;
144 hsr
= netdev_priv(dev
);
145 master
= hsr_port_get_hsr(hsr
, HSR_PT_MASTER
);
147 if (new_mtu
> hsr_get_max_mtu(hsr
)) {
148 netdev_info(master
->dev
, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
158 static int hsr_dev_open(struct net_device
*dev
)
160 struct hsr_priv
*hsr
;
161 struct hsr_port
*port
;
164 hsr
= netdev_priv(dev
);
168 hsr_for_each_port(hsr
, port
) {
169 if (port
->type
== HSR_PT_MASTER
)
171 switch (port
->type
) {
181 if (!is_slave_up(port
->dev
))
182 netdev_warn(dev
, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n",
183 designation
, port
->dev
->name
);
187 if (designation
== '\0')
188 netdev_warn(dev
, "No slave devices configured\n");
194 static int hsr_dev_close(struct net_device
*dev
)
196 /* Nothing to do here. */
201 static void hsr_fill_tag(struct hsr_ethhdr
*hsr_ethhdr
, struct hsr_priv
*hsr
)
203 unsigned long irqflags
;
205 /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values
206 * between 0001-1001 ("ring identifier", for regular HSR frames),
207 * or 1111 ("HSR management", supervision frames). Unfortunately, the
208 * spec writers forgot to explain what a "ring identifier" is, or
209 * how it is used. So we just set this to 0001 for regular frames,
210 * and 1111 for supervision frames.
212 set_hsr_tag_path(&hsr_ethhdr
->hsr_tag
, 0x1);
214 /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet
215 * frame is the content of the frame located between the Length/Type
216 * field and the Frame Check Sequence."
218 * IEC 62439-3, p 48, specifies the "original LPDU" to include the
219 * original "LT" field (what "LT" means is not explained anywhere as
220 * far as I can see - perhaps "Length/Type"?). So LSDU_size might
221 * equal original length + 2.
222 * Also, the fact that this field is not used anywhere (might be used
223 * by a RedBox connecting HSR and PRP nets?) means I cannot test its
224 * correctness. Instead of guessing, I set this to 0 here, to make any
225 * problems immediately apparent. Anyone using this driver with PRP/HSR
226 * RedBoxes might need to fix this...
228 set_hsr_tag_LSDU_size(&hsr_ethhdr
->hsr_tag
, 0);
230 spin_lock_irqsave(&hsr
->seqnr_lock
, irqflags
);
231 hsr_ethhdr
->hsr_tag
.sequence_nr
= htons(hsr
->sequence_nr
);
233 spin_unlock_irqrestore(&hsr
->seqnr_lock
, irqflags
);
235 hsr_ethhdr
->hsr_tag
.encap_proto
= hsr_ethhdr
->ethhdr
.h_proto
;
237 hsr_ethhdr
->ethhdr
.h_proto
= htons(ETH_P_PRP
);
240 static int slave_xmit(struct hsr_priv
*hsr
, struct sk_buff
*skb
,
241 enum hsr_port_type type
)
243 struct hsr_port
*port
;
244 struct hsr_ethhdr
*hsr_ethhdr
;
246 hsr_ethhdr
= (struct hsr_ethhdr
*) skb
->data
;
249 port
= hsr_port_get_hsr(hsr
, type
);
252 return NET_XMIT_DROP
;
254 skb
->dev
= port
->dev
;
256 hsr_addr_subst_dest(port
->hsr
, &hsr_ethhdr
->ethhdr
, port
);
259 /* Address substitution (IEC62439-3 pp 26, 50): replace mac
260 * address of outgoing frame with that of the outgoing slave's.
262 ether_addr_copy(hsr_ethhdr
->ethhdr
.h_source
, skb
->dev
->dev_addr
);
264 return dev_queue_xmit(skb
);
267 static int hsr_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
269 struct hsr_priv
*hsr
;
270 struct hsr_port
*master
;
271 struct hsr_ethhdr
*hsr_ethhdr
;
272 struct sk_buff
*skb2
;
275 hsr
= netdev_priv(dev
);
276 hsr_ethhdr
= (struct hsr_ethhdr
*) skb
->data
;
278 if ((skb
->protocol
!= htons(ETH_P_PRP
)) ||
279 (hsr_ethhdr
->ethhdr
.h_proto
!= htons(ETH_P_PRP
))) {
280 hsr_fill_tag(hsr_ethhdr
, hsr
);
281 skb
->protocol
= htons(ETH_P_PRP
);
284 skb2
= pskb_copy(skb
, GFP_ATOMIC
);
286 res1
= slave_xmit(hsr
, skb
, HSR_PT_SLAVE_A
);
288 res2
= slave_xmit(hsr
, skb2
, HSR_PT_SLAVE_B
);
290 res2
= NET_XMIT_DROP
;
293 master
= hsr_port_get_hsr(hsr
, HSR_PT_MASTER
);
294 if (likely(res1
== NET_XMIT_SUCCESS
|| res1
== NET_XMIT_CN
||
295 res2
== NET_XMIT_SUCCESS
|| res2
== NET_XMIT_CN
)) {
296 master
->dev
->stats
.tx_packets
++;
297 master
->dev
->stats
.tx_bytes
+= skb
->len
;
299 master
->dev
->stats
.tx_dropped
++;
307 static int hsr_header_create(struct sk_buff
*skb
, struct net_device
*dev
,
308 unsigned short type
, const void *daddr
,
309 const void *saddr
, unsigned int len
)
313 /* Make room for the HSR tag now. We will fill it in later (in
316 if (skb_headroom(skb
) < HSR_HLEN
+ ETH_HLEN
)
318 skb_push(skb
, HSR_HLEN
);
320 /* To allow VLAN/HSR combos we should probably use
321 * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_HLEN);
322 * here instead. It would require other changes too, though - e.g.
323 * separate headers for each slave etc...
325 res
= eth_header(skb
, dev
, type
, daddr
, saddr
, len
+ HSR_HLEN
);
328 skb_reset_mac_header(skb
);
330 return res
+ HSR_HLEN
;
334 static const struct header_ops hsr_header_ops
= {
335 .create
= hsr_header_create
,
336 .parse
= eth_header_parse
,
340 /* HSR:2010 supervision frames should be padded so that the whole frame,
341 * including headers and FCS, is 64 bytes (without VLAN).
343 static int hsr_pad(int size
)
345 const int min_size
= ETH_ZLEN
- HSR_HLEN
- ETH_HLEN
;
347 if (size
>= min_size
)
352 static void send_hsr_supervision_frame(struct net_device
*hsr_dev
, u8 type
)
354 struct hsr_priv
*hsr
;
355 struct hsr_port
*master
;
358 struct hsr_sup_tag
*hsr_stag
;
359 struct hsr_sup_payload
*hsr_sp
;
360 unsigned long irqflags
;
363 hsr
= netdev_priv(hsr_dev
);
364 master
= hsr_port_get_hsr(hsr
, HSR_PT_MASTER
);
366 hlen
= LL_RESERVED_SPACE(master
->dev
);
367 tlen
= master
->dev
->needed_tailroom
;
368 skb
= alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload
)) + hlen
+ tlen
,
374 skb_reserve(skb
, hlen
);
376 skb
->dev
= master
->dev
;
377 skb
->protocol
= htons(ETH_P_PRP
);
378 skb
->priority
= TC_PRIO_CONTROL
;
380 res
= dev_hard_header(skb
, skb
->dev
, ETH_P_PRP
,
381 hsr
->sup_multicast_addr
,
382 skb
->dev
->dev_addr
, skb
->len
);
386 skb_pull(skb
, sizeof(struct ethhdr
));
387 hsr_stag
= (typeof(hsr_stag
)) skb
->data
;
389 set_hsr_stag_path(hsr_stag
, 0xf);
390 set_hsr_stag_HSR_Ver(hsr_stag
, 0);
392 spin_lock_irqsave(&hsr
->seqnr_lock
, irqflags
);
393 hsr_stag
->sequence_nr
= htons(hsr
->sequence_nr
);
395 spin_unlock_irqrestore(&hsr
->seqnr_lock
, irqflags
);
397 hsr_stag
->HSR_TLV_Type
= type
;
398 hsr_stag
->HSR_TLV_Length
= 12;
400 skb_push(skb
, sizeof(struct ethhdr
));
402 /* Payload: MacAddressA */
403 hsr_sp
= (typeof(hsr_sp
)) skb_put(skb
, sizeof(*hsr_sp
));
404 ether_addr_copy(hsr_sp
->MacAddressA
, master
->dev
->dev_addr
);
410 WARN_ON_ONCE("HSR: Could not send supervision frame\n");
415 /* Announce (supervision frame) timer function
417 static void hsr_announce(unsigned long data
)
419 struct hsr_priv
*hsr
;
420 struct hsr_port
*master
;
422 hsr
= (struct hsr_priv
*) data
;
423 master
= hsr_port_get_hsr(hsr
, HSR_PT_MASTER
);
425 if (hsr
->announce_count
< 3) {
426 send_hsr_supervision_frame(master
->dev
, HSR_TLV_ANNOUNCE
);
427 hsr
->announce_count
++;
429 send_hsr_supervision_frame(master
->dev
, HSR_TLV_LIFE_CHECK
);
432 if (hsr
->announce_count
< 3)
433 hsr
->announce_timer
.expires
= jiffies
+
434 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL
);
436 hsr
->announce_timer
.expires
= jiffies
+
437 msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL
);
439 if (is_admin_up(master
->dev
))
440 add_timer(&hsr
->announce_timer
);
444 /* According to comments in the declaration of struct net_device, this function
445 * is "Called from unregister, can be used to call free_netdev". Ok then...
447 static void hsr_dev_destroy(struct net_device
*hsr_dev
)
449 struct hsr_priv
*hsr
;
450 struct hsr_port
*port
;
452 hsr
= netdev_priv(hsr_dev
);
453 hsr_for_each_port(hsr
, port
)
456 del_timer_sync(&hsr
->prune_timer
);
457 del_timer_sync(&hsr
->announce_timer
);
460 free_netdev(hsr_dev
);
463 static const struct net_device_ops hsr_device_ops
= {
464 .ndo_change_mtu
= hsr_dev_change_mtu
,
465 .ndo_open
= hsr_dev_open
,
466 .ndo_stop
= hsr_dev_close
,
467 .ndo_start_xmit
= hsr_dev_xmit
,
471 void hsr_dev_setup(struct net_device
*dev
)
473 random_ether_addr(dev
->dev_addr
);
476 dev
->header_ops
= &hsr_header_ops
;
477 dev
->netdev_ops
= &hsr_device_ops
;
478 dev
->tx_queue_len
= 0;
480 dev
->destructor
= hsr_dev_destroy
;
484 /* Return true if dev is a HSR master; return false otherwise.
486 inline bool is_hsr_master(struct net_device
*dev
)
488 return (dev
->netdev_ops
->ndo_start_xmit
== hsr_dev_xmit
);
491 /* Default multicast address for HSR Supervision frames */
492 static const unsigned char def_multicast_addr
[ETH_ALEN
] __aligned(2) = {
493 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
496 int hsr_dev_finalize(struct net_device
*hsr_dev
, struct net_device
*slave
[2],
497 unsigned char multicast_spec
)
499 struct hsr_priv
*hsr
;
500 struct hsr_port
*port
;
503 hsr
= netdev_priv(hsr_dev
);
504 INIT_LIST_HEAD(&hsr
->ports
);
505 INIT_LIST_HEAD(&hsr
->node_db
);
506 INIT_LIST_HEAD(&hsr
->self_node_db
);
508 ether_addr_copy(hsr_dev
->dev_addr
, slave
[0]->dev_addr
);
510 /* Make sure we recognize frames from ourselves in hsr_rcv() */
511 res
= hsr_create_self_node(&hsr
->self_node_db
, hsr_dev
->dev_addr
,
516 hsr_dev
->features
= slave
[0]->features
& slave
[1]->features
;
517 /* Prevent recursive tx locking */
518 hsr_dev
->features
|= NETIF_F_LLTX
;
519 /* VLAN on top of HSR needs testing and probably some work on
520 * hsr_header_create() etc.
522 hsr_dev
->features
|= NETIF_F_VLAN_CHALLENGED
;
524 spin_lock_init(&hsr
->seqnr_lock
);
525 /* Overflow soon to find bugs easier: */
526 hsr
->sequence_nr
= USHRT_MAX
- 1024;
528 init_timer(&hsr
->announce_timer
);
529 hsr
->announce_timer
.function
= hsr_announce
;
530 hsr
->announce_timer
.data
= (unsigned long) hsr
;
532 init_timer(&hsr
->prune_timer
);
533 hsr
->prune_timer
.function
= hsr_prune_nodes
;
534 hsr
->prune_timer
.data
= (unsigned long) hsr
;
536 ether_addr_copy(hsr
->sup_multicast_addr
, def_multicast_addr
);
537 hsr
->sup_multicast_addr
[ETH_ALEN
- 1] = multicast_spec
;
539 /* FIXME: should I modify the value of these?
541 * - hsr_dev->flags - i.e.
543 * - hsr_dev->priv_flags - i.e.
545 * IFF_TX_SKB_SHARING?
546 * IFF_HSR_MASTER/SLAVE?
549 /* Make sure the 1st call to netif_carrier_on() gets through */
550 netif_carrier_off(hsr_dev
);
552 res
= hsr_add_port(hsr
, hsr_dev
, HSR_PT_MASTER
);
556 res
= register_netdevice(hsr_dev
);
560 res
= hsr_add_port(hsr
, slave
[0], HSR_PT_SLAVE_A
);
563 res
= hsr_add_port(hsr
, slave
[1], HSR_PT_SLAVE_B
);
567 hsr
->prune_timer
.expires
= jiffies
+ msecs_to_jiffies(PRUNE_PERIOD
);
568 add_timer(&hsr
->prune_timer
);
573 hsr_for_each_port(hsr
, port
)