hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/hsr/hsr_device.c
....@@ -1,15 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /* Copyright 2011-2014 Autronica Fire and Security AS
2
- *
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)
6
- * any later version.
73 *
84 * Author(s):
95 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
10
- *
116 * This file contains device methods for creating, using and destroying
12
- * virtual HSR devices.
7
+ * virtual HSR or PRP devices.
138 */
149
1510 #include <linux/netdevice.h>
....@@ -22,7 +17,6 @@
2217 #include "hsr_framereg.h"
2318 #include "hsr_main.h"
2419 #include "hsr_forward.h"
25
-
2620
2721 static bool is_admin_up(struct net_device *dev)
2822 {
....@@ -62,26 +56,20 @@
6256 static bool hsr_check_carrier(struct hsr_port *master)
6357 {
6458 struct hsr_port *port;
65
- bool has_carrier;
6659
67
- has_carrier = false;
60
+ ASSERT_RTNL();
6861
69
- rcu_read_lock();
70
- hsr_for_each_port(master->hsr, port)
71
- if ((port->type != HSR_PT_MASTER) && is_slave_up(port->dev)) {
72
- has_carrier = true;
73
- break;
62
+ hsr_for_each_port(master->hsr, port) {
63
+ if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
64
+ netif_carrier_on(master->dev);
65
+ return true;
7466 }
75
- rcu_read_unlock();
67
+ }
7668
77
- if (has_carrier)
78
- netif_carrier_on(master->dev);
79
- else
80
- netif_carrier_off(master->dev);
69
+ netif_carrier_off(master->dev);
8170
82
- return has_carrier;
71
+ return false;
8372 }
84
-
8573
8674 static void hsr_check_announce(struct net_device *hsr_dev,
8775 unsigned char old_operstate)
....@@ -90,15 +78,14 @@
9078
9179 hsr = netdev_priv(hsr_dev);
9280
93
- if ((hsr_dev->operstate == IF_OPER_UP)
94
- && (old_operstate != IF_OPER_UP)) {
81
+ if (hsr_dev->operstate == IF_OPER_UP && old_operstate != IF_OPER_UP) {
9582 /* Went up */
9683 hsr->announce_count = 0;
9784 mod_timer(&hsr->announce_timer,
9885 jiffies + msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
9986 }
10087
101
- if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP))
88
+ if (hsr_dev->operstate != IF_OPER_UP && old_operstate == IF_OPER_UP)
10289 /* Went down */
10390 del_timer(&hsr->announce_timer);
10491 }
....@@ -125,28 +112,23 @@
125112 struct hsr_port *port;
126113
127114 mtu_max = ETH_DATA_LEN;
128
- rcu_read_lock();
129115 hsr_for_each_port(hsr, port)
130116 if (port->type != HSR_PT_MASTER)
131117 mtu_max = min(port->dev->mtu, mtu_max);
132
- rcu_read_unlock();
133118
134119 if (mtu_max < HSR_HLEN)
135120 return 0;
136121 return mtu_max - HSR_HLEN;
137122 }
138123
139
-
140124 static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
141125 {
142126 struct hsr_priv *hsr;
143
- struct hsr_port *master;
144127
145128 hsr = netdev_priv(dev);
146
- master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
147129
148130 if (new_mtu > hsr_get_max_mtu(hsr)) {
149
- 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",
131
+ 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",
150132 HSR_HLEN);
151133 return -EINVAL;
152134 }
....@@ -165,7 +147,6 @@
165147 hsr = netdev_priv(dev);
166148 designation = '\0';
167149
168
- rcu_read_lock();
169150 hsr_for_each_port(hsr, port) {
170151 if (port->type == HSR_PT_MASTER)
171152 continue;
....@@ -183,7 +164,6 @@
183164 netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n",
184165 designation, port->dev->name);
185166 }
186
- rcu_read_unlock();
187167
188168 if (designation == '\0')
189169 netdev_warn(dev, "No slave devices configured\n");
....@@ -191,13 +171,11 @@
191171 return 0;
192172 }
193173
194
-
195174 static int hsr_dev_close(struct net_device *dev)
196175 {
197176 /* Nothing to do here. */
198177 return 0;
199178 }
200
-
201179
202180 static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
203181 netdev_features_t features)
....@@ -231,101 +209,163 @@
231209 return hsr_features_recompute(hsr, features);
232210 }
233211
234
-
235
-static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
212
+static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
236213 {
237214 struct hsr_priv *hsr = netdev_priv(dev);
238215 struct hsr_port *master;
239216
240217 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
241
- skb->dev = master->dev;
242
- hsr_forward_skb(skb, master);
243
-
218
+ if (master) {
219
+ skb->dev = master->dev;
220
+ skb_reset_mac_header(skb);
221
+ skb_reset_mac_len(skb);
222
+ spin_lock_bh(&hsr->seqnr_lock);
223
+ hsr_forward_skb(skb, master);
224
+ spin_unlock_bh(&hsr->seqnr_lock);
225
+ } else {
226
+ atomic_long_inc(&dev->tx_dropped);
227
+ dev_kfree_skb_any(skb);
228
+ }
244229 return NETDEV_TX_OK;
245230 }
246
-
247231
248232 static const struct header_ops hsr_header_ops = {
249233 .create = eth_header,
250234 .parse = eth_header_parse,
251235 };
252236
253
-static void send_hsr_supervision_frame(struct hsr_port *master,
254
- u8 type, u8 hsrVer)
237
+static struct sk_buff *hsr_init_skb(struct hsr_port *master)
255238 {
239
+ struct hsr_priv *hsr = master->hsr;
256240 struct sk_buff *skb;
257241 int hlen, tlen;
258
- struct hsr_tag *hsr_tag;
259
- struct hsr_sup_tag *hsr_stag;
260
- struct hsr_sup_payload *hsr_sp;
261
- unsigned long irqflags;
262242
263243 hlen = LL_RESERVED_SPACE(master->dev);
264244 tlen = master->dev->needed_tailroom;
265
- skb = dev_alloc_skb(
266
- sizeof(struct hsr_tag) +
267
- sizeof(struct hsr_sup_tag) +
268
- sizeof(struct hsr_sup_payload) + hlen + tlen);
245
+ /* skb size is same for PRP/HSR frames, only difference
246
+ * being, for PRP it is a trailer and for HSR it is a
247
+ * header
248
+ */
249
+ skb = dev_alloc_skb(sizeof(struct hsr_sup_tag) +
250
+ sizeof(struct hsr_sup_payload) + hlen + tlen);
269251
270
- if (skb == NULL)
271
- return;
252
+ if (!skb)
253
+ return skb;
272254
273255 skb_reserve(skb, hlen);
274
-
275256 skb->dev = master->dev;
276
- skb->protocol = htons(hsrVer ? ETH_P_HSR : ETH_P_PRP);
277257 skb->priority = TC_PRIO_CONTROL;
278258
279
- if (dev_hard_header(skb, skb->dev, (hsrVer ? ETH_P_HSR : ETH_P_PRP),
280
- master->hsr->sup_multicast_addr,
259
+ if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
260
+ hsr->sup_multicast_addr,
281261 skb->dev->dev_addr, skb->len) <= 0)
282262 goto out;
263
+
283264 skb_reset_mac_header(skb);
265
+ skb_reset_mac_len(skb);
284266 skb_reset_network_header(skb);
285267 skb_reset_transport_header(skb);
286268
287
- if (hsrVer > 0) {
288
- hsr_tag = skb_put(skb, sizeof(struct hsr_tag));
289
- hsr_tag->encap_proto = htons(ETH_P_PRP);
290
- set_hsr_tag_LSDU_size(hsr_tag, HSR_V1_SUP_LSDUSIZE);
269
+ return skb;
270
+out:
271
+ kfree_skb(skb);
272
+
273
+ return NULL;
274
+}
275
+
276
+static void send_hsr_supervision_frame(struct hsr_port *master,
277
+ unsigned long *interval)
278
+{
279
+ struct hsr_priv *hsr = master->hsr;
280
+ __u8 type = HSR_TLV_LIFE_CHECK;
281
+ struct hsr_sup_payload *hsr_sp;
282
+ struct hsr_sup_tag *hsr_stag;
283
+ struct sk_buff *skb;
284
+
285
+ *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
286
+ if (hsr->announce_count < 3 && hsr->prot_version == 0) {
287
+ type = HSR_TLV_ANNOUNCE;
288
+ *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
289
+ hsr->announce_count++;
290
+ }
291
+
292
+ skb = hsr_init_skb(master);
293
+ if (!skb) {
294
+ WARN_ONCE(1, "HSR: Could not send supervision frame\n");
295
+ return;
291296 }
292297
293298 hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
294
- set_hsr_stag_path(hsr_stag, (hsrVer ? 0x0 : 0xf));
295
- set_hsr_stag_HSR_Ver(hsr_stag, hsrVer);
299
+ set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
300
+ set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
296301
297302 /* From HSRv1 on we have separate supervision sequence numbers. */
298
- spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
299
- if (hsrVer > 0) {
300
- hsr_stag->sequence_nr = htons(master->hsr->sup_sequence_nr);
301
- hsr_tag->sequence_nr = htons(master->hsr->sequence_nr);
302
- master->hsr->sup_sequence_nr++;
303
- master->hsr->sequence_nr++;
303
+ spin_lock_bh(&hsr->seqnr_lock);
304
+ if (hsr->prot_version > 0) {
305
+ hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
306
+ hsr->sup_sequence_nr++;
304307 } else {
305
- hsr_stag->sequence_nr = htons(master->hsr->sequence_nr);
306
- master->hsr->sequence_nr++;
308
+ hsr_stag->sequence_nr = htons(hsr->sequence_nr);
309
+ hsr->sequence_nr++;
307310 }
308
- spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
309311
310
- hsr_stag->HSR_TLV_Type = type;
312
+ hsr_stag->HSR_TLV_type = type;
311313 /* TODO: Why 12 in HSRv0? */
312
- hsr_stag->HSR_TLV_Length = hsrVer ? sizeof(struct hsr_sup_payload) : 12;
314
+ hsr_stag->HSR_TLV_length = hsr->prot_version ?
315
+ sizeof(struct hsr_sup_payload) : 12;
313316
314317 /* Payload: MacAddressA */
315318 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
316
- ether_addr_copy(hsr_sp->MacAddressA, master->dev->dev_addr);
319
+ ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
317320
318
- if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN))
321
+ if (skb_put_padto(skb, ETH_ZLEN)) {
322
+ spin_unlock_bh(&hsr->seqnr_lock);
319323 return;
324
+ }
320325
321326 hsr_forward_skb(skb, master);
327
+ spin_unlock_bh(&hsr->seqnr_lock);
322328 return;
323
-
324
-out:
325
- WARN_ONCE(1, "HSR: Could not send supervision frame\n");
326
- kfree_skb(skb);
327329 }
328330
331
+static void send_prp_supervision_frame(struct hsr_port *master,
332
+ unsigned long *interval)
333
+{
334
+ struct hsr_priv *hsr = master->hsr;
335
+ struct hsr_sup_payload *hsr_sp;
336
+ struct hsr_sup_tag *hsr_stag;
337
+ struct sk_buff *skb;
338
+
339
+ skb = hsr_init_skb(master);
340
+ if (!skb) {
341
+ WARN_ONCE(1, "PRP: Could not send supervision frame\n");
342
+ return;
343
+ }
344
+
345
+ *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
346
+ hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
347
+ set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
348
+ set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
349
+
350
+ /* From HSRv1 on we have separate supervision sequence numbers. */
351
+ spin_lock_bh(&hsr->seqnr_lock);
352
+ hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
353
+ hsr->sup_sequence_nr++;
354
+ hsr_stag->HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
355
+ hsr_stag->HSR_TLV_length = sizeof(struct hsr_sup_payload);
356
+
357
+ /* Payload: MacAddressA */
358
+ hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
359
+ ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
360
+
361
+ if (skb_put_padto(skb, ETH_ZLEN)) {
362
+ spin_unlock_bh(&hsr->seqnr_lock);
363
+ return;
364
+ }
365
+
366
+ hsr_forward_skb(skb, master);
367
+ spin_unlock_bh(&hsr->seqnr_lock);
368
+}
329369
330370 /* Announce (supervision frame) timer function
331371 */
....@@ -339,19 +379,7 @@
339379
340380 rcu_read_lock();
341381 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
342
-
343
- if (hsr->announce_count < 3 && hsr->protVersion == 0) {
344
- send_hsr_supervision_frame(master, HSR_TLV_ANNOUNCE,
345
- hsr->protVersion);
346
- hsr->announce_count++;
347
-
348
- interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
349
- } else {
350
- send_hsr_supervision_frame(master, HSR_TLV_LIFE_CHECK,
351
- hsr->protVersion);
352
-
353
- interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
354
- }
382
+ hsr->proto_ops->send_sv_frame(master, &interval);
355383
356384 if (is_admin_up(master->dev))
357385 mod_timer(&hsr->announce_timer, jiffies + interval);
....@@ -359,26 +387,21 @@
359387 rcu_read_unlock();
360388 }
361389
362
-
363
-/* According to comments in the declaration of struct net_device, this function
364
- * is "Called from unregister, can be used to call free_netdev". Ok then...
365
- */
366
-static void hsr_dev_destroy(struct net_device *hsr_dev)
390
+void hsr_del_ports(struct hsr_priv *hsr)
367391 {
368
- struct hsr_priv *hsr;
369392 struct hsr_port *port;
370393
371
- hsr = netdev_priv(hsr_dev);
372
-
373
- rtnl_lock();
374
- hsr_for_each_port(hsr, port)
394
+ port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
395
+ if (port)
375396 hsr_del_port(port);
376
- rtnl_unlock();
377397
378
- del_timer_sync(&hsr->prune_timer);
379
- del_timer_sync(&hsr->announce_timer);
398
+ port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
399
+ if (port)
400
+ hsr_del_port(port);
380401
381
- synchronize_rcu();
402
+ port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
403
+ if (port)
404
+ hsr_del_port(port);
382405 }
383406
384407 static const struct net_device_ops hsr_device_ops = {
....@@ -393,6 +416,24 @@
393416 .name = "hsr",
394417 };
395418
419
+static struct hsr_proto_ops hsr_ops = {
420
+ .send_sv_frame = send_hsr_supervision_frame,
421
+ .create_tagged_frame = hsr_create_tagged_frame,
422
+ .get_untagged_frame = hsr_get_untagged_frame,
423
+ .fill_frame_info = hsr_fill_frame_info,
424
+ .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
425
+};
426
+
427
+static struct hsr_proto_ops prp_ops = {
428
+ .send_sv_frame = send_prp_supervision_frame,
429
+ .create_tagged_frame = prp_create_tagged_frame,
430
+ .get_untagged_frame = prp_get_untagged_frame,
431
+ .drop_frame = prp_drop_frame,
432
+ .fill_frame_info = prp_fill_frame_info,
433
+ .handle_san_frame = prp_handle_san_frame,
434
+ .update_san_info = prp_update_san_info,
435
+};
436
+
396437 void hsr_dev_setup(struct net_device *dev)
397438 {
398439 eth_hw_addr_random(dev);
....@@ -402,10 +443,9 @@
402443 dev->header_ops = &hsr_header_ops;
403444 dev->netdev_ops = &hsr_device_ops;
404445 SET_NETDEV_DEVTYPE(dev, &hsr_type);
405
- dev->priv_flags |= IFF_NO_QUEUE;
446
+ dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL;
406447
407448 dev->needs_free_netdev = true;
408
- dev->priv_destructor = hsr_dev_destroy;
409449
410450 dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
411451 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
....@@ -425,7 +465,6 @@
425465 dev->features |= NETIF_F_NETNS_LOCAL;
426466 }
427467
428
-
429468 /* Return true if dev is a HSR master; return false otherwise.
430469 */
431470 inline bool is_hsr_master(struct net_device *dev)
....@@ -439,21 +478,34 @@
439478 };
440479
441480 int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
442
- unsigned char multicast_spec, u8 protocol_version)
481
+ unsigned char multicast_spec, u8 protocol_version,
482
+ struct netlink_ext_ack *extack)
443483 {
484
+ bool unregister = false;
444485 struct hsr_priv *hsr;
445
- struct hsr_port *port;
446486 int res;
447487
448488 hsr = netdev_priv(hsr_dev);
449489 INIT_LIST_HEAD(&hsr->ports);
450490 INIT_LIST_HEAD(&hsr->node_db);
451491 INIT_LIST_HEAD(&hsr->self_node_db);
492
+ spin_lock_init(&hsr->list_lock);
452493
453494 ether_addr_copy(hsr_dev->dev_addr, slave[0]->dev_addr);
454495
496
+ /* initialize protocol specific functions */
497
+ if (protocol_version == PRP_V1) {
498
+ /* For PRP, lan_id has most significant 3 bits holding
499
+ * the net_id of PRP_LAN_ID
500
+ */
501
+ hsr->net_id = PRP_LAN_ID << 1;
502
+ hsr->proto_ops = &prp_ops;
503
+ } else {
504
+ hsr->proto_ops = &hsr_ops;
505
+ }
506
+
455507 /* Make sure we recognize frames from ourselves in hsr_rcv() */
456
- res = hsr_create_self_node(&hsr->self_node_db, hsr_dev->dev_addr,
508
+ res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
457509 slave[1]->dev_addr);
458510 if (res < 0)
459511 return res;
....@@ -469,7 +521,7 @@
469521 ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
470522 hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
471523
472
- hsr->protVersion = protocol_version;
524
+ hsr->prot_version = protocol_version;
473525
474526 /* FIXME: should I modify the value of these?
475527 *
....@@ -484,30 +536,35 @@
484536 /* Make sure the 1st call to netif_carrier_on() gets through */
485537 netif_carrier_off(hsr_dev);
486538
487
- res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER);
539
+ res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
488540 if (res)
489
- goto err_add_port;
541
+ goto err_add_master;
490542
491543 res = register_netdevice(hsr_dev);
492544 if (res)
493
- goto fail;
545
+ goto err_unregister;
494546
495
- res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A);
496
- if (res)
497
- goto fail;
498
- res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B);
499
- if (res)
500
- goto fail;
547
+ unregister = true;
501548
549
+ res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
550
+ if (res)
551
+ goto err_unregister;
552
+
553
+ res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
554
+ if (res)
555
+ goto err_unregister;
556
+
557
+ hsr_debugfs_init(hsr, hsr_dev);
502558 mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
503559
504560 return 0;
505561
506
-fail:
507
- hsr_for_each_port(hsr, port)
508
- hsr_del_port(port);
509
-err_add_port:
510
- hsr_del_node(&hsr->self_node_db);
562
+err_unregister:
563
+ hsr_del_ports(hsr);
564
+err_add_master:
565
+ hsr_del_self_node(hsr);
511566
567
+ if (unregister)
568
+ unregister_netdevice(hsr_dev);
512569 return res;
513570 }