hsr_device.c (9b0dcd0e5a27958b57e3e390f63c098d63a055da) hsr_device.c (9c5f8a19b2de2860d4b7764204c52832ac0f4440)
1// SPDX-License-Identifier: GPL-2.0
1/* Copyright 2011-2014 Autronica Fire and Security AS
2 *
2/* Copyright 2011-2014 Autronica Fire and Security AS
3 *
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.
7 *
8 * Author(s):
9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
10 *
11 * This file contains device methods for creating, using and destroying
12 * virtual HSR devices.
13 */
14
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"
23#include "hsr_main.h"
24#include "hsr_forward.h"
25
4 * Author(s):
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
6 *
7 * This file contains device methods for creating, using and destroying
8 * virtual HSR devices.
9 */
10
11#include <linux/netdevice.h>
12#include <linux/skbuff.h>
13#include <linux/etherdevice.h>
14#include <linux/rtnetlink.h>
15#include <linux/pkt_sched.h>
16#include "hsr_device.h"
17#include "hsr_slave.h"
18#include "hsr_framereg.h"
19#include "hsr_main.h"
20#include "hsr_forward.h"
21
26
27static bool is_admin_up(struct net_device *dev)
28{
29 return dev && (dev->flags & IFF_UP);
30}
31
32static bool is_slave_up(struct net_device *dev)
33{
34 return dev && is_admin_up(dev) && netif_oper_up(dev);

--- 28 unchanged lines hidden (view full) ---

63{
64 struct hsr_port *port;
65 bool has_carrier;
66
67 has_carrier = false;
68
69 rcu_read_lock();
70 hsr_for_each_port(master->hsr, port)
22static bool is_admin_up(struct net_device *dev)
23{
24 return dev && (dev->flags & IFF_UP);
25}
26
27static bool is_slave_up(struct net_device *dev)
28{
29 return dev && is_admin_up(dev) && netif_oper_up(dev);

--- 28 unchanged lines hidden (view full) ---

58{
59 struct hsr_port *port;
60 bool has_carrier;
61
62 has_carrier = false;
63
64 rcu_read_lock();
65 hsr_for_each_port(master->hsr, port)
71 if ((port->type != HSR_PT_MASTER) && is_slave_up(port->dev)) {
66 if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
72 has_carrier = true;
73 break;
74 }
75 rcu_read_unlock();
76
77 if (has_carrier)
78 netif_carrier_on(master->dev);
79 else
80 netif_carrier_off(master->dev);
81
82 return has_carrier;
83}
84
67 has_carrier = true;
68 break;
69 }
70 rcu_read_unlock();
71
72 if (has_carrier)
73 netif_carrier_on(master->dev);
74 else
75 netif_carrier_off(master->dev);
76
77 return has_carrier;
78}
79
85
86static void hsr_check_announce(struct net_device *hsr_dev,
87 unsigned char old_operstate)
88{
89 struct hsr_priv *hsr;
90
91 hsr = netdev_priv(hsr_dev);
92
80static void hsr_check_announce(struct net_device *hsr_dev,
81 unsigned char old_operstate)
82{
83 struct hsr_priv *hsr;
84
85 hsr = netdev_priv(hsr_dev);
86
93 if ((hsr_dev->operstate == IF_OPER_UP)
94 && (old_operstate != IF_OPER_UP)) {
87 if (hsr_dev->operstate == IF_OPER_UP && old_operstate != IF_OPER_UP) {
95 /* Went up */
96 hsr->announce_count = 0;
97 mod_timer(&hsr->announce_timer,
98 jiffies + msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
99 }
100
88 /* Went up */
89 hsr->announce_count = 0;
90 mod_timer(&hsr->announce_timer,
91 jiffies + msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
92 }
93
101 if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP))
94 if (hsr_dev->operstate != IF_OPER_UP && old_operstate == IF_OPER_UP)
102 /* Went down */
103 del_timer(&hsr->announce_timer);
104}
105
106void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
107{
108 struct hsr_port *master;
109 unsigned char old_operstate;

--- 21 unchanged lines hidden (view full) ---

131 mtu_max = min(port->dev->mtu, mtu_max);
132 rcu_read_unlock();
133
134 if (mtu_max < HSR_HLEN)
135 return 0;
136 return mtu_max - HSR_HLEN;
137}
138
95 /* Went down */
96 del_timer(&hsr->announce_timer);
97}
98
99void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
100{
101 struct hsr_port *master;
102 unsigned char old_operstate;

--- 21 unchanged lines hidden (view full) ---

124 mtu_max = min(port->dev->mtu, mtu_max);
125 rcu_read_unlock();
126
127 if (mtu_max < HSR_HLEN)
128 return 0;
129 return mtu_max - HSR_HLEN;
130}
131
139
140static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
141{
142 struct hsr_priv *hsr;
143 struct hsr_port *master;
144
145 hsr = netdev_priv(dev);
146 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
147

--- 38 unchanged lines hidden (view full) ---

186 rcu_read_unlock();
187
188 if (designation == '\0')
189 netdev_warn(dev, "No slave devices configured\n");
190
191 return 0;
192}
193
132static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
133{
134 struct hsr_priv *hsr;
135 struct hsr_port *master;
136
137 hsr = netdev_priv(dev);
138 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
139

--- 38 unchanged lines hidden (view full) ---

178 rcu_read_unlock();
179
180 if (designation == '\0')
181 netdev_warn(dev, "No slave devices configured\n");
182
183 return 0;
184}
185
194
195static int hsr_dev_close(struct net_device *dev)
196{
197 /* Nothing to do here. */
198 return 0;
199}
200
186static int hsr_dev_close(struct net_device *dev)
187{
188 /* Nothing to do here. */
189 return 0;
190}
191
201
202static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
203 netdev_features_t features)
204{
205 netdev_features_t mask;
206 struct hsr_port *port;
207
208 mask = features;
209

--- 16 unchanged lines hidden (view full) ---

226static netdev_features_t hsr_fix_features(struct net_device *dev,
227 netdev_features_t features)
228{
229 struct hsr_priv *hsr = netdev_priv(dev);
230
231 return hsr_features_recompute(hsr, features);
232}
233
192static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
193 netdev_features_t features)
194{
195 netdev_features_t mask;
196 struct hsr_port *port;
197
198 mask = features;
199

--- 16 unchanged lines hidden (view full) ---

216static netdev_features_t hsr_fix_features(struct net_device *dev,
217 netdev_features_t features)
218{
219 struct hsr_priv *hsr = netdev_priv(dev);
220
221 return hsr_features_recompute(hsr, features);
222}
223
234
235static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
236{
237 struct hsr_priv *hsr = netdev_priv(dev);
238 struct hsr_port *master;
239
240 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
241 skb->dev = master->dev;
242 hsr_forward_skb(skb, master);
243
244 return NETDEV_TX_OK;
245}
246
224static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
225{
226 struct hsr_priv *hsr = netdev_priv(dev);
227 struct hsr_port *master;
228
229 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
230 skb->dev = master->dev;
231 hsr_forward_skb(skb, master);
232
233 return NETDEV_TX_OK;
234}
235
247
248static const struct header_ops hsr_header_ops = {
249 .create = eth_header,
250 .parse = eth_header_parse,
251};
252
253static void send_hsr_supervision_frame(struct hsr_port *master,
236static const struct header_ops hsr_header_ops = {
237 .create = eth_header,
238 .parse = eth_header_parse,
239};
240
241static void send_hsr_supervision_frame(struct hsr_port *master,
254 u8 type, u8 hsrVer)
242 u8 type, u8 hsr_ver)
255{
256 struct sk_buff *skb;
257 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;
262
263 hlen = LL_RESERVED_SPACE(master->dev);
264 tlen = master->dev->needed_tailroom;
243{
244 struct sk_buff *skb;
245 int hlen, tlen;
246 struct hsr_tag *hsr_tag;
247 struct hsr_sup_tag *hsr_stag;
248 struct hsr_sup_payload *hsr_sp;
249 unsigned long irqflags;
250
251 hlen = LL_RESERVED_SPACE(master->dev);
252 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);
253 skb = dev_alloc_skb(sizeof(struct hsr_tag) +
254 sizeof(struct hsr_sup_tag) +
255 sizeof(struct hsr_sup_payload) + hlen + tlen);
269
256
270 if (skb == NULL)
257 if (!skb)
271 return;
272
273 skb_reserve(skb, hlen);
274
275 skb->dev = master->dev;
258 return;
259
260 skb_reserve(skb, hlen);
261
262 skb->dev = master->dev;
276 skb->protocol = htons(hsrVer ? ETH_P_HSR : ETH_P_PRP);
263 skb->protocol = htons(hsr_ver ? ETH_P_HSR : ETH_P_PRP);
277 skb->priority = TC_PRIO_CONTROL;
278
264 skb->priority = TC_PRIO_CONTROL;
265
279 if (dev_hard_header(skb, skb->dev, (hsrVer ? ETH_P_HSR : ETH_P_PRP),
266 if (dev_hard_header(skb, skb->dev, (hsr_ver ? ETH_P_HSR : ETH_P_PRP),
280 master->hsr->sup_multicast_addr,
281 skb->dev->dev_addr, skb->len) <= 0)
282 goto out;
283 skb_reset_mac_header(skb);
284
267 master->hsr->sup_multicast_addr,
268 skb->dev->dev_addr, skb->len) <= 0)
269 goto out;
270 skb_reset_mac_header(skb);
271
285 if (hsrVer > 0) {
272 if (hsr_ver > 0) {
286 hsr_tag = skb_put(skb, sizeof(struct hsr_tag));
287 hsr_tag->encap_proto = htons(ETH_P_PRP);
288 set_hsr_tag_LSDU_size(hsr_tag, HSR_V1_SUP_LSDUSIZE);
289 }
290
291 hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
273 hsr_tag = skb_put(skb, sizeof(struct hsr_tag));
274 hsr_tag->encap_proto = htons(ETH_P_PRP);
275 set_hsr_tag_LSDU_size(hsr_tag, HSR_V1_SUP_LSDUSIZE);
276 }
277
278 hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
292 set_hsr_stag_path(hsr_stag, (hsrVer ? 0x0 : 0xf));
293 set_hsr_stag_HSR_Ver(hsr_stag, hsrVer);
279 set_hsr_stag_path(hsr_stag, (hsr_ver ? 0x0 : 0xf));
280 set_hsr_stag_HSR_ver(hsr_stag, hsr_ver);
294
295 /* From HSRv1 on we have separate supervision sequence numbers. */
296 spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
281
282 /* From HSRv1 on we have separate supervision sequence numbers. */
283 spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
297 if (hsrVer > 0) {
284 if (hsr_ver > 0) {
298 hsr_stag->sequence_nr = htons(master->hsr->sup_sequence_nr);
299 hsr_tag->sequence_nr = htons(master->hsr->sequence_nr);
300 master->hsr->sup_sequence_nr++;
301 master->hsr->sequence_nr++;
302 } else {
303 hsr_stag->sequence_nr = htons(master->hsr->sequence_nr);
304 master->hsr->sequence_nr++;
305 }
306 spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
307
285 hsr_stag->sequence_nr = htons(master->hsr->sup_sequence_nr);
286 hsr_tag->sequence_nr = htons(master->hsr->sequence_nr);
287 master->hsr->sup_sequence_nr++;
288 master->hsr->sequence_nr++;
289 } else {
290 hsr_stag->sequence_nr = htons(master->hsr->sequence_nr);
291 master->hsr->sequence_nr++;
292 }
293 spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
294
308 hsr_stag->HSR_TLV_Type = type;
295 hsr_stag->HSR_TLV_type = type;
309 /* TODO: Why 12 in HSRv0? */
296 /* TODO: Why 12 in HSRv0? */
310 hsr_stag->HSR_TLV_Length = hsrVer ? sizeof(struct hsr_sup_payload) : 12;
297 hsr_stag->HSR_TLV_length =
298 hsr_ver ? sizeof(struct hsr_sup_payload) : 12;
311
312 /* Payload: MacAddressA */
313 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
299
300 /* Payload: MacAddressA */
301 hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
314 ether_addr_copy(hsr_sp->MacAddressA, master->dev->dev_addr);
302 ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
315
316 if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN))
317 return;
318
319 hsr_forward_skb(skb, master);
320 return;
321
322out:
323 WARN_ONCE(1, "HSR: Could not send supervision frame\n");
324 kfree_skb(skb);
325}
326
303
304 if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN))
305 return;
306
307 hsr_forward_skb(skb, master);
308 return;
309
310out:
311 WARN_ONCE(1, "HSR: Could not send supervision frame\n");
312 kfree_skb(skb);
313}
314
327
328/* Announce (supervision frame) timer function
329 */
330static void hsr_announce(struct timer_list *t)
331{
332 struct hsr_priv *hsr;
333 struct hsr_port *master;
334 unsigned long interval;
335
336 hsr = from_timer(hsr, t, announce_timer);
337
338 rcu_read_lock();
339 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
340
315/* Announce (supervision frame) timer function
316 */
317static void hsr_announce(struct timer_list *t)
318{
319 struct hsr_priv *hsr;
320 struct hsr_port *master;
321 unsigned long interval;
322
323 hsr = from_timer(hsr, t, announce_timer);
324
325 rcu_read_lock();
326 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
327
341 if (hsr->announce_count < 3 && hsr->protVersion == 0) {
328 if (hsr->announce_count < 3 && hsr->prot_version == 0) {
342 send_hsr_supervision_frame(master, HSR_TLV_ANNOUNCE,
329 send_hsr_supervision_frame(master, HSR_TLV_ANNOUNCE,
343 hsr->protVersion);
330 hsr->prot_version);
344 hsr->announce_count++;
345
346 interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
347 } else {
348 send_hsr_supervision_frame(master, HSR_TLV_LIFE_CHECK,
331 hsr->announce_count++;
332
333 interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
334 } else {
335 send_hsr_supervision_frame(master, HSR_TLV_LIFE_CHECK,
349 hsr->protVersion);
336 hsr->prot_version);
350
351 interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
352 }
353
354 if (is_admin_up(master->dev))
355 mod_timer(&hsr->announce_timer, jiffies + interval);
356
357 rcu_read_unlock();
358}
359
337
338 interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
339 }
340
341 if (is_admin_up(master->dev))
342 mod_timer(&hsr->announce_timer, jiffies + interval);
343
344 rcu_read_unlock();
345}
346
360
361/* According to comments in the declaration of struct net_device, this function
362 * is "Called from unregister, can be used to call free_netdev". Ok then...
363 */
364static void hsr_dev_destroy(struct net_device *hsr_dev)
365{
366 struct hsr_priv *hsr;
367 struct hsr_port *port;
368
369 hsr = netdev_priv(hsr_dev);
370
347/* According to comments in the declaration of struct net_device, this function
348 * is "Called from unregister, can be used to call free_netdev". Ok then...
349 */
350static void hsr_dev_destroy(struct net_device *hsr_dev)
351{
352 struct hsr_priv *hsr;
353 struct hsr_port *port;
354
355 hsr = netdev_priv(hsr_dev);
356
357 hsr_debugfs_term(hsr);
358
371 rtnl_lock();
372 hsr_for_each_port(hsr, port)
373 hsr_del_port(port);
374 rtnl_unlock();
375
376 del_timer_sync(&hsr->prune_timer);
377 del_timer_sync(&hsr->announce_timer);
378

--- 39 unchanged lines hidden (view full) ---

418 */
419 dev->features |= NETIF_F_VLAN_CHALLENGED;
420 /* Not sure about this. Taken from bridge code. netdev_features.h says
421 * it means "Does not change network namespaces".
422 */
423 dev->features |= NETIF_F_NETNS_LOCAL;
424}
425
359 rtnl_lock();
360 hsr_for_each_port(hsr, port)
361 hsr_del_port(port);
362 rtnl_unlock();
363
364 del_timer_sync(&hsr->prune_timer);
365 del_timer_sync(&hsr->announce_timer);
366

--- 39 unchanged lines hidden (view full) ---

406 */
407 dev->features |= NETIF_F_VLAN_CHALLENGED;
408 /* Not sure about this. Taken from bridge code. netdev_features.h says
409 * it means "Does not change network namespaces".
410 */
411 dev->features |= NETIF_F_NETNS_LOCAL;
412}
413
426
427/* Return true if dev is a HSR master; return false otherwise.
428 */
429inline bool is_hsr_master(struct net_device *dev)
430{
431 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
432}
433
434/* Default multicast address for HSR Supervision frames */

--- 27 unchanged lines hidden (view full) ---

462 hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
463
464 timer_setup(&hsr->announce_timer, hsr_announce, 0);
465 timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
466
467 ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
468 hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
469
414/* Return true if dev is a HSR master; return false otherwise.
415 */
416inline bool is_hsr_master(struct net_device *dev)
417{
418 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
419}
420
421/* Default multicast address for HSR Supervision frames */

--- 27 unchanged lines hidden (view full) ---

449 hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
450
451 timer_setup(&hsr->announce_timer, hsr_announce, 0);
452 timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
453
454 ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
455 hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
456
470 hsr->protVersion = protocol_version;
457 hsr->prot_version = protocol_version;
471
472 /* FIXME: should I modify the value of these?
473 *
474 * - hsr_dev->flags - i.e.
475 * IFF_MASTER/SLAVE?
476 * - hsr_dev->priv_flags - i.e.
477 * IFF_EBRIDGE?
478 * IFF_TX_SKB_SHARING?

--- 14 unchanged lines hidden (view full) ---

493 res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A);
494 if (res)
495 goto fail;
496 res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B);
497 if (res)
498 goto fail;
499
500 mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
458
459 /* FIXME: should I modify the value of these?
460 *
461 * - hsr_dev->flags - i.e.
462 * IFF_MASTER/SLAVE?
463 * - hsr_dev->priv_flags - i.e.
464 * IFF_EBRIDGE?
465 * IFF_TX_SKB_SHARING?

--- 14 unchanged lines hidden (view full) ---

480 res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A);
481 if (res)
482 goto fail;
483 res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B);
484 if (res)
485 goto fail;
486
487 mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
488 res = hsr_debugfs_init(hsr);
489 if (res)
490 goto fail;
501
502 return 0;
503
504fail:
505 hsr_for_each_port(hsr, port)
506 hsr_del_port(port);
507err_add_port:
508 hsr_del_node(&hsr->self_node_db);
509
510 return res;
511}
491
492 return 0;
493
494fail:
495 hsr_for_each_port(hsr, port)
496 hsr_del_port(port);
497err_add_port:
498 hsr_del_node(&hsr->self_node_db);
499
500 return res;
501}