]> Git Repo - linux.git/blob - net/hsr/hsr_device.c
drm/xe/tests: Convert xe_mocs live tests
[linux.git] / net / hsr / hsr_device.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
3  *
4  * Author(s):
5  *      2011-2014 Arvid Brodin, [email protected]
6  * This file contains device methods for creating, using and destroying
7  * virtual HSR or PRP devices.
8  */
9
10 #include <linux/netdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/etherdevice.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/pkt_sched.h>
15 #include "hsr_device.h"
16 #include "hsr_slave.h"
17 #include "hsr_framereg.h"
18 #include "hsr_main.h"
19 #include "hsr_forward.h"
20
21 static bool is_admin_up(struct net_device *dev)
22 {
23         return dev && (dev->flags & IFF_UP);
24 }
25
26 static bool is_slave_up(struct net_device *dev)
27 {
28         return dev && is_admin_up(dev) && netif_oper_up(dev);
29 }
30
31 static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
32 {
33         struct net_device *dev = master->dev;
34
35         if (!is_admin_up(dev)) {
36                 netdev_set_operstate(dev, IF_OPER_DOWN);
37                 return;
38         }
39
40         if (has_carrier)
41                 netdev_set_operstate(dev, IF_OPER_UP);
42         else
43                 netdev_set_operstate(dev, IF_OPER_LOWERLAYERDOWN);
44 }
45
46 static bool hsr_check_carrier(struct hsr_port *master)
47 {
48         struct hsr_port *port;
49
50         ASSERT_RTNL();
51
52         hsr_for_each_port(master->hsr, port) {
53                 if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
54                         netif_carrier_on(master->dev);
55                         return true;
56                 }
57         }
58
59         netif_carrier_off(master->dev);
60
61         return false;
62 }
63
64 static void hsr_check_announce(struct net_device *hsr_dev)
65 {
66         struct hsr_priv *hsr;
67
68         hsr = netdev_priv(hsr_dev);
69         if (netif_running(hsr_dev) && netif_oper_up(hsr_dev)) {
70                 /* Enable announce timer and start sending supervisory frames */
71                 if (!timer_pending(&hsr->announce_timer)) {
72                         hsr->announce_count = 0;
73                         mod_timer(&hsr->announce_timer, jiffies +
74                                   msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
75                 }
76         } else {
77                 /* Deactivate the announce timer  */
78                 timer_delete(&hsr->announce_timer);
79         }
80 }
81
82 void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
83 {
84         struct hsr_port *master;
85         bool has_carrier;
86
87         master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
88         /* netif_stacked_transfer_operstate() cannot be used here since
89          * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
90          */
91         has_carrier = hsr_check_carrier(master);
92         hsr_set_operstate(master, has_carrier);
93         hsr_check_announce(master->dev);
94 }
95
96 int hsr_get_max_mtu(struct hsr_priv *hsr)
97 {
98         unsigned int mtu_max;
99         struct hsr_port *port;
100
101         mtu_max = ETH_DATA_LEN;
102         hsr_for_each_port(hsr, port)
103                 if (port->type != HSR_PT_MASTER)
104                         mtu_max = min(port->dev->mtu, mtu_max);
105
106         if (mtu_max < HSR_HLEN)
107                 return 0;
108         return mtu_max - HSR_HLEN;
109 }
110
111 static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
112 {
113         struct hsr_priv *hsr;
114
115         hsr = netdev_priv(dev);
116
117         if (new_mtu > hsr_get_max_mtu(hsr)) {
118                 netdev_info(dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
119                             HSR_HLEN);
120                 return -EINVAL;
121         }
122
123         WRITE_ONCE(dev->mtu, new_mtu);
124
125         return 0;
126 }
127
128 static int hsr_dev_open(struct net_device *dev)
129 {
130         struct hsr_priv *hsr;
131         struct hsr_port *port;
132         const char *designation = NULL;
133
134         hsr = netdev_priv(dev);
135
136         hsr_for_each_port(hsr, port) {
137                 if (port->type == HSR_PT_MASTER)
138                         continue;
139                 switch (port->type) {
140                 case HSR_PT_SLAVE_A:
141                         designation = "Slave A";
142                         break;
143                 case HSR_PT_SLAVE_B:
144                         designation = "Slave B";
145                         break;
146                 case HSR_PT_INTERLINK:
147                         designation = "Interlink";
148                         break;
149                 default:
150                         designation = "Unknown";
151                 }
152                 if (!is_slave_up(port->dev))
153                         netdev_warn(dev, "%s (%s) is not up; please bring it up to get a fully working HSR network\n",
154                                     designation, port->dev->name);
155         }
156
157         if (!designation)
158                 netdev_warn(dev, "No slave devices configured\n");
159
160         return 0;
161 }
162
163 static int hsr_dev_close(struct net_device *dev)
164 {
165         struct hsr_port *port;
166         struct hsr_priv *hsr;
167
168         hsr = netdev_priv(dev);
169         hsr_for_each_port(hsr, port) {
170                 if (port->type == HSR_PT_MASTER)
171                         continue;
172                 switch (port->type) {
173                 case HSR_PT_SLAVE_A:
174                 case HSR_PT_SLAVE_B:
175                         dev_uc_unsync(port->dev, dev);
176                         dev_mc_unsync(port->dev, dev);
177                         break;
178                 default:
179                         break;
180                 }
181         }
182
183         return 0;
184 }
185
186 static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
187                                                 netdev_features_t features)
188 {
189         netdev_features_t mask;
190         struct hsr_port *port;
191
192         mask = features;
193
194         /* Mask out all features that, if supported by one device, should be
195          * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
196          *
197          * Anything that's off in mask will not be enabled - so only things
198          * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
199          * may become enabled.
200          */
201         features &= ~NETIF_F_ONE_FOR_ALL;
202         hsr_for_each_port(hsr, port)
203                 features = netdev_increment_features(features,
204                                                      port->dev->features,
205                                                      mask);
206
207         return features;
208 }
209
210 static netdev_features_t hsr_fix_features(struct net_device *dev,
211                                           netdev_features_t features)
212 {
213         struct hsr_priv *hsr = netdev_priv(dev);
214
215         return hsr_features_recompute(hsr, features);
216 }
217
218 static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
219 {
220         struct hsr_priv *hsr = netdev_priv(dev);
221         struct hsr_port *master;
222
223         master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
224         if (master) {
225                 skb->dev = master->dev;
226                 skb_reset_mac_header(skb);
227                 skb_reset_mac_len(skb);
228                 spin_lock_bh(&hsr->seqnr_lock);
229                 hsr_forward_skb(skb, master);
230                 spin_unlock_bh(&hsr->seqnr_lock);
231         } else {
232                 dev_core_stats_tx_dropped_inc(dev);
233                 dev_kfree_skb_any(skb);
234         }
235         return NETDEV_TX_OK;
236 }
237
238 static const struct header_ops hsr_header_ops = {
239         .create  = eth_header,
240         .parse   = eth_header_parse,
241 };
242
243 static struct sk_buff *hsr_init_skb(struct hsr_port *master)
244 {
245         struct hsr_priv *hsr = master->hsr;
246         struct sk_buff *skb;
247         int hlen, tlen;
248
249         hlen = LL_RESERVED_SPACE(master->dev);
250         tlen = master->dev->needed_tailroom;
251         /* skb size is same for PRP/HSR frames, only difference
252          * being, for PRP it is a trailer and for HSR it is a
253          * header
254          */
255         skb = dev_alloc_skb(sizeof(struct hsr_sup_tag) +
256                             sizeof(struct hsr_sup_payload) + hlen + tlen);
257
258         if (!skb)
259                 return skb;
260
261         skb_reserve(skb, hlen);
262         skb->dev = master->dev;
263         skb->priority = TC_PRIO_CONTROL;
264
265         if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
266                             hsr->sup_multicast_addr,
267                             skb->dev->dev_addr, skb->len) <= 0)
268                 goto out;
269
270         skb_reset_mac_header(skb);
271         skb_reset_mac_len(skb);
272         skb_reset_network_header(skb);
273         skb_reset_transport_header(skb);
274
275         return skb;
276 out:
277         kfree_skb(skb);
278
279         return NULL;
280 }
281
282 static void send_hsr_supervision_frame(struct hsr_port *master,
283                                        unsigned long *interval)
284 {
285         struct hsr_priv *hsr = master->hsr;
286         __u8 type = HSR_TLV_LIFE_CHECK;
287         struct hsr_sup_payload *hsr_sp;
288         struct hsr_sup_tlv *hsr_stlv;
289         struct hsr_sup_tag *hsr_stag;
290         struct sk_buff *skb;
291
292         *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
293         if (hsr->announce_count < 3 && hsr->prot_version == 0) {
294                 type = HSR_TLV_ANNOUNCE;
295                 *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
296                 hsr->announce_count++;
297         }
298
299         skb = hsr_init_skb(master);
300         if (!skb) {
301                 netdev_warn_once(master->dev, "HSR: Could not send supervision frame\n");
302                 return;
303         }
304
305         hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
306         set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
307         set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
308
309         /* From HSRv1 on we have separate supervision sequence numbers. */
310         spin_lock_bh(&hsr->seqnr_lock);
311         if (hsr->prot_version > 0) {
312                 hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
313                 hsr->sup_sequence_nr++;
314         } else {
315                 hsr_stag->sequence_nr = htons(hsr->sequence_nr);
316                 hsr->sequence_nr++;
317         }
318
319         hsr_stag->tlv.HSR_TLV_type = type;
320         /* TODO: Why 12 in HSRv0? */
321         hsr_stag->tlv.HSR_TLV_length = hsr->prot_version ?
322                                 sizeof(struct hsr_sup_payload) : 12;
323
324         /* Payload: MacAddressA */
325         hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
326         ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
327
328         if (hsr->redbox) {
329                 hsr_stlv = skb_put(skb, sizeof(struct hsr_sup_tlv));
330                 hsr_stlv->HSR_TLV_type = PRP_TLV_REDBOX_MAC;
331                 hsr_stlv->HSR_TLV_length = sizeof(struct hsr_sup_payload);
332
333                 /* Payload: MacAddressRedBox */
334                 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
335                 ether_addr_copy(hsr_sp->macaddress_A, hsr->macaddress_redbox);
336         }
337
338         if (skb_put_padto(skb, ETH_ZLEN)) {
339                 spin_unlock_bh(&hsr->seqnr_lock);
340                 return;
341         }
342
343         hsr_forward_skb(skb, master);
344         spin_unlock_bh(&hsr->seqnr_lock);
345         return;
346 }
347
348 static void send_prp_supervision_frame(struct hsr_port *master,
349                                        unsigned long *interval)
350 {
351         struct hsr_priv *hsr = master->hsr;
352         struct hsr_sup_payload *hsr_sp;
353         struct hsr_sup_tag *hsr_stag;
354         struct sk_buff *skb;
355
356         skb = hsr_init_skb(master);
357         if (!skb) {
358                 netdev_warn_once(master->dev, "PRP: Could not send supervision frame\n");
359                 return;
360         }
361
362         *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
363         hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
364         set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
365         set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
366
367         /* From HSRv1 on we have separate supervision sequence numbers. */
368         spin_lock_bh(&hsr->seqnr_lock);
369         hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
370         hsr->sup_sequence_nr++;
371         hsr_stag->tlv.HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
372         hsr_stag->tlv.HSR_TLV_length = sizeof(struct hsr_sup_payload);
373
374         /* Payload: MacAddressA */
375         hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
376         ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
377
378         if (skb_put_padto(skb, ETH_ZLEN)) {
379                 spin_unlock_bh(&hsr->seqnr_lock);
380                 return;
381         }
382
383         hsr_forward_skb(skb, master);
384         spin_unlock_bh(&hsr->seqnr_lock);
385 }
386
387 /* Announce (supervision frame) timer function
388  */
389 static void hsr_announce(struct timer_list *t)
390 {
391         struct hsr_priv *hsr;
392         struct hsr_port *master;
393         unsigned long interval;
394
395         hsr = from_timer(hsr, t, announce_timer);
396
397         rcu_read_lock();
398         master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
399         hsr->proto_ops->send_sv_frame(master, &interval);
400
401         if (is_admin_up(master->dev))
402                 mod_timer(&hsr->announce_timer, jiffies + interval);
403
404         rcu_read_unlock();
405 }
406
407 void hsr_del_ports(struct hsr_priv *hsr)
408 {
409         struct hsr_port *port;
410
411         port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
412         if (port)
413                 hsr_del_port(port);
414
415         port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
416         if (port)
417                 hsr_del_port(port);
418
419         port = hsr_port_get_hsr(hsr, HSR_PT_INTERLINK);
420         if (port)
421                 hsr_del_port(port);
422
423         port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
424         if (port)
425                 hsr_del_port(port);
426 }
427
428 static void hsr_set_rx_mode(struct net_device *dev)
429 {
430         struct hsr_port *port;
431         struct hsr_priv *hsr;
432
433         hsr = netdev_priv(dev);
434
435         hsr_for_each_port(hsr, port) {
436                 if (port->type == HSR_PT_MASTER)
437                         continue;
438                 switch (port->type) {
439                 case HSR_PT_SLAVE_A:
440                 case HSR_PT_SLAVE_B:
441                         dev_mc_sync_multiple(port->dev, dev);
442                         dev_uc_sync_multiple(port->dev, dev);
443                         break;
444                 default:
445                         break;
446                 }
447         }
448 }
449
450 static void hsr_change_rx_flags(struct net_device *dev, int change)
451 {
452         struct hsr_port *port;
453         struct hsr_priv *hsr;
454
455         hsr = netdev_priv(dev);
456
457         hsr_for_each_port(hsr, port) {
458                 if (port->type == HSR_PT_MASTER)
459                         continue;
460                 switch (port->type) {
461                 case HSR_PT_SLAVE_A:
462                 case HSR_PT_SLAVE_B:
463                         if (change & IFF_ALLMULTI)
464                                 dev_set_allmulti(port->dev,
465                                                  dev->flags &
466                                                  IFF_ALLMULTI ? 1 : -1);
467                         break;
468                 default:
469                         break;
470                 }
471         }
472 }
473
474 static const struct net_device_ops hsr_device_ops = {
475         .ndo_change_mtu = hsr_dev_change_mtu,
476         .ndo_open = hsr_dev_open,
477         .ndo_stop = hsr_dev_close,
478         .ndo_start_xmit = hsr_dev_xmit,
479         .ndo_change_rx_flags = hsr_change_rx_flags,
480         .ndo_fix_features = hsr_fix_features,
481         .ndo_set_rx_mode = hsr_set_rx_mode,
482 };
483
484 static const struct device_type hsr_type = {
485         .name = "hsr",
486 };
487
488 static struct hsr_proto_ops hsr_ops = {
489         .send_sv_frame = send_hsr_supervision_frame,
490         .create_tagged_frame = hsr_create_tagged_frame,
491         .get_untagged_frame = hsr_get_untagged_frame,
492         .drop_frame = hsr_drop_frame,
493         .fill_frame_info = hsr_fill_frame_info,
494         .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
495 };
496
497 static struct hsr_proto_ops prp_ops = {
498         .send_sv_frame = send_prp_supervision_frame,
499         .create_tagged_frame = prp_create_tagged_frame,
500         .get_untagged_frame = prp_get_untagged_frame,
501         .drop_frame = prp_drop_frame,
502         .fill_frame_info = prp_fill_frame_info,
503         .handle_san_frame = prp_handle_san_frame,
504         .update_san_info = prp_update_san_info,
505 };
506
507 void hsr_dev_setup(struct net_device *dev)
508 {
509         eth_hw_addr_random(dev);
510
511         ether_setup(dev);
512         dev->min_mtu = 0;
513         dev->header_ops = &hsr_header_ops;
514         dev->netdev_ops = &hsr_device_ops;
515         SET_NETDEV_DEVTYPE(dev, &hsr_type);
516         dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL;
517
518         dev->needs_free_netdev = true;
519
520         dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
521                            NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
522                            NETIF_F_HW_VLAN_CTAG_TX;
523
524         dev->features = dev->hw_features;
525
526         /* Prevent recursive tx locking */
527         dev->features |= NETIF_F_LLTX;
528         /* VLAN on top of HSR needs testing and probably some work on
529          * hsr_header_create() etc.
530          */
531         dev->features |= NETIF_F_VLAN_CHALLENGED;
532         /* Not sure about this. Taken from bridge code. netdev_features.h says
533          * it means "Does not change network namespaces".
534          */
535         dev->features |= NETIF_F_NETNS_LOCAL;
536 }
537
538 /* Return true if dev is a HSR master; return false otherwise.
539  */
540 bool is_hsr_master(struct net_device *dev)
541 {
542         return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
543 }
544 EXPORT_SYMBOL(is_hsr_master);
545
546 /* Default multicast address for HSR Supervision frames */
547 static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
548         0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
549 };
550
551 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
552                      struct net_device *interlink, unsigned char multicast_spec,
553                      u8 protocol_version, struct netlink_ext_ack *extack)
554 {
555         bool unregister = false;
556         struct hsr_priv *hsr;
557         int res;
558
559         hsr = netdev_priv(hsr_dev);
560         INIT_LIST_HEAD(&hsr->ports);
561         INIT_LIST_HEAD(&hsr->node_db);
562         INIT_LIST_HEAD(&hsr->proxy_node_db);
563         spin_lock_init(&hsr->list_lock);
564
565         eth_hw_addr_set(hsr_dev, slave[0]->dev_addr);
566
567         /* initialize protocol specific functions */
568         if (protocol_version == PRP_V1) {
569                 /* For PRP, lan_id has most significant 3 bits holding
570                  * the net_id of PRP_LAN_ID
571                  */
572                 hsr->net_id = PRP_LAN_ID << 1;
573                 hsr->proto_ops = &prp_ops;
574         } else {
575                 hsr->proto_ops = &hsr_ops;
576         }
577
578         /* Make sure we recognize frames from ourselves in hsr_rcv() */
579         res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
580                                    slave[1]->dev_addr);
581         if (res < 0)
582                 return res;
583
584         spin_lock_init(&hsr->seqnr_lock);
585         /* Overflow soon to find bugs easier: */
586         hsr->sequence_nr = HSR_SEQNR_START;
587         hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
588         hsr->interlink_sequence_nr = HSR_SEQNR_START;
589
590         timer_setup(&hsr->announce_timer, hsr_announce, 0);
591         timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
592         timer_setup(&hsr->prune_proxy_timer, hsr_prune_proxy_nodes, 0);
593
594         ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
595         hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
596
597         hsr->prot_version = protocol_version;
598
599         /* Make sure the 1st call to netif_carrier_on() gets through */
600         netif_carrier_off(hsr_dev);
601
602         res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
603         if (res)
604                 goto err_add_master;
605
606         /* HSR forwarding offload supported in lower device? */
607         if ((slave[0]->features & NETIF_F_HW_HSR_FWD) &&
608             (slave[1]->features & NETIF_F_HW_HSR_FWD))
609                 hsr->fwd_offloaded = true;
610
611         res = register_netdevice(hsr_dev);
612         if (res)
613                 goto err_unregister;
614
615         unregister = true;
616
617         res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
618         if (res)
619                 goto err_unregister;
620
621         res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
622         if (res)
623                 goto err_unregister;
624
625         if (interlink) {
626                 res = hsr_add_port(hsr, interlink, HSR_PT_INTERLINK, extack);
627                 if (res)
628                         goto err_unregister;
629
630                 hsr->redbox = true;
631                 ether_addr_copy(hsr->macaddress_redbox, interlink->dev_addr);
632                 mod_timer(&hsr->prune_proxy_timer,
633                           jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
634         }
635
636         hsr_debugfs_init(hsr, hsr_dev);
637         mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
638
639         return 0;
640
641 err_unregister:
642         hsr_del_ports(hsr);
643 err_add_master:
644         hsr_del_self_node(hsr);
645
646         if (unregister)
647                 unregister_netdevice(hsr_dev);
648         return res;
649 }
This page took 0.071474 seconds and 4 git commands to generate.