hsr_device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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, arvid.brodin@alten.se
  6. * This file contains device methods for creating, using and destroying
  7. * virtual HSR or PRP devices.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/pkt_sched.h>
  14. #include "hsr_device.h"
  15. #include "hsr_slave.h"
  16. #include "hsr_framereg.h"
  17. #include "hsr_main.h"
  18. #include "hsr_forward.h"
  19. static bool is_admin_up(struct net_device *dev)
  20. {
  21. return dev && (dev->flags & IFF_UP);
  22. }
  23. static bool is_slave_up(struct net_device *dev)
  24. {
  25. return dev && is_admin_up(dev) && netif_oper_up(dev);
  26. }
  27. static void __hsr_set_operstate(struct net_device *dev, int transition)
  28. {
  29. write_lock_bh(&dev_base_lock);
  30. if (dev->operstate != transition) {
  31. dev->operstate = transition;
  32. write_unlock_bh(&dev_base_lock);
  33. netdev_state_change(dev);
  34. } else {
  35. write_unlock_bh(&dev_base_lock);
  36. }
  37. }
  38. static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
  39. {
  40. if (!is_admin_up(master->dev)) {
  41. __hsr_set_operstate(master->dev, IF_OPER_DOWN);
  42. return;
  43. }
  44. if (has_carrier)
  45. __hsr_set_operstate(master->dev, IF_OPER_UP);
  46. else
  47. __hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN);
  48. }
  49. static bool hsr_check_carrier(struct hsr_port *master)
  50. {
  51. struct hsr_port *port;
  52. ASSERT_RTNL();
  53. hsr_for_each_port(master->hsr, port) {
  54. if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
  55. netif_carrier_on(master->dev);
  56. return true;
  57. }
  58. }
  59. netif_carrier_off(master->dev);
  60. return false;
  61. }
  62. static void hsr_check_announce(struct net_device *hsr_dev,
  63. unsigned char old_operstate)
  64. {
  65. struct hsr_priv *hsr;
  66. hsr = netdev_priv(hsr_dev);
  67. if (hsr_dev->operstate == IF_OPER_UP && old_operstate != IF_OPER_UP) {
  68. /* Went up */
  69. hsr->announce_count = 0;
  70. mod_timer(&hsr->announce_timer,
  71. jiffies + msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
  72. }
  73. if (hsr_dev->operstate != IF_OPER_UP && old_operstate == IF_OPER_UP)
  74. /* Went down */
  75. del_timer(&hsr->announce_timer);
  76. }
  77. void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
  78. {
  79. struct hsr_port *master;
  80. unsigned char old_operstate;
  81. bool has_carrier;
  82. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  83. /* netif_stacked_transfer_operstate() cannot be used here since
  84. * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
  85. */
  86. old_operstate = master->dev->operstate;
  87. has_carrier = hsr_check_carrier(master);
  88. hsr_set_operstate(master, has_carrier);
  89. hsr_check_announce(master->dev, old_operstate);
  90. }
  91. int hsr_get_max_mtu(struct hsr_priv *hsr)
  92. {
  93. unsigned int mtu_max;
  94. struct hsr_port *port;
  95. mtu_max = ETH_DATA_LEN;
  96. hsr_for_each_port(hsr, port)
  97. if (port->type != HSR_PT_MASTER)
  98. mtu_max = min(port->dev->mtu, mtu_max);
  99. if (mtu_max < HSR_HLEN)
  100. return 0;
  101. return mtu_max - HSR_HLEN;
  102. }
  103. static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
  104. {
  105. struct hsr_priv *hsr;
  106. hsr = netdev_priv(dev);
  107. if (new_mtu > hsr_get_max_mtu(hsr)) {
  108. 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",
  109. HSR_HLEN);
  110. return -EINVAL;
  111. }
  112. dev->mtu = new_mtu;
  113. return 0;
  114. }
  115. static int hsr_dev_open(struct net_device *dev)
  116. {
  117. struct hsr_priv *hsr;
  118. struct hsr_port *port;
  119. char designation;
  120. hsr = netdev_priv(dev);
  121. designation = '\0';
  122. hsr_for_each_port(hsr, port) {
  123. if (port->type == HSR_PT_MASTER)
  124. continue;
  125. switch (port->type) {
  126. case HSR_PT_SLAVE_A:
  127. designation = 'A';
  128. break;
  129. case HSR_PT_SLAVE_B:
  130. designation = 'B';
  131. break;
  132. default:
  133. designation = '?';
  134. }
  135. if (!is_slave_up(port->dev))
  136. netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n",
  137. designation, port->dev->name);
  138. }
  139. if (designation == '\0')
  140. netdev_warn(dev, "No slave devices configured\n");
  141. return 0;
  142. }
  143. static int hsr_dev_close(struct net_device *dev)
  144. {
  145. /* Nothing to do here. */
  146. return 0;
  147. }
  148. static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
  149. netdev_features_t features)
  150. {
  151. netdev_features_t mask;
  152. struct hsr_port *port;
  153. mask = features;
  154. /* Mask out all features that, if supported by one device, should be
  155. * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
  156. *
  157. * Anything that's off in mask will not be enabled - so only things
  158. * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
  159. * may become enabled.
  160. */
  161. features &= ~NETIF_F_ONE_FOR_ALL;
  162. hsr_for_each_port(hsr, port)
  163. features = netdev_increment_features(features,
  164. port->dev->features,
  165. mask);
  166. return features;
  167. }
  168. static netdev_features_t hsr_fix_features(struct net_device *dev,
  169. netdev_features_t features)
  170. {
  171. struct hsr_priv *hsr = netdev_priv(dev);
  172. return hsr_features_recompute(hsr, features);
  173. }
  174. static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  175. {
  176. struct hsr_priv *hsr = netdev_priv(dev);
  177. struct hsr_port *master;
  178. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  179. if (master) {
  180. skb->dev = master->dev;
  181. skb_reset_mac_header(skb);
  182. skb_reset_mac_len(skb);
  183. hsr_forward_skb(skb, master);
  184. } else {
  185. atomic_long_inc(&dev->tx_dropped);
  186. dev_kfree_skb_any(skb);
  187. }
  188. return NETDEV_TX_OK;
  189. }
  190. static const struct header_ops hsr_header_ops = {
  191. .create = eth_header,
  192. .parse = eth_header_parse,
  193. };
  194. static struct sk_buff *hsr_init_skb(struct hsr_port *master, u16 proto)
  195. {
  196. struct hsr_priv *hsr = master->hsr;
  197. struct sk_buff *skb;
  198. int hlen, tlen;
  199. hlen = LL_RESERVED_SPACE(master->dev);
  200. tlen = master->dev->needed_tailroom;
  201. /* skb size is same for PRP/HSR frames, only difference
  202. * being, for PRP it is a trailer and for HSR it is a
  203. * header
  204. */
  205. skb = dev_alloc_skb(sizeof(struct hsr_tag) +
  206. sizeof(struct hsr_sup_tag) +
  207. sizeof(struct hsr_sup_payload) + hlen + tlen);
  208. if (!skb)
  209. return skb;
  210. skb_reserve(skb, hlen);
  211. skb->dev = master->dev;
  212. skb->protocol = htons(proto);
  213. skb->priority = TC_PRIO_CONTROL;
  214. if (dev_hard_header(skb, skb->dev, proto,
  215. hsr->sup_multicast_addr,
  216. skb->dev->dev_addr, skb->len) <= 0)
  217. goto out;
  218. skb_reset_mac_header(skb);
  219. skb_reset_mac_len(skb);
  220. skb_reset_network_header(skb);
  221. skb_reset_transport_header(skb);
  222. return skb;
  223. out:
  224. kfree_skb(skb);
  225. return NULL;
  226. }
  227. static void send_hsr_supervision_frame(struct hsr_port *master,
  228. unsigned long *interval)
  229. {
  230. struct hsr_priv *hsr = master->hsr;
  231. __u8 type = HSR_TLV_LIFE_CHECK;
  232. struct hsr_tag *hsr_tag = NULL;
  233. struct hsr_sup_payload *hsr_sp;
  234. struct hsr_sup_tag *hsr_stag;
  235. unsigned long irqflags;
  236. struct sk_buff *skb;
  237. u16 proto;
  238. *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  239. if (hsr->announce_count < 3 && hsr->prot_version == 0) {
  240. type = HSR_TLV_ANNOUNCE;
  241. *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  242. hsr->announce_count++;
  243. }
  244. if (!hsr->prot_version)
  245. proto = ETH_P_PRP;
  246. else
  247. proto = ETH_P_HSR;
  248. skb = hsr_init_skb(master, proto);
  249. if (!skb) {
  250. WARN_ONCE(1, "HSR: Could not send supervision frame\n");
  251. return;
  252. }
  253. if (hsr->prot_version > 0) {
  254. hsr_tag = skb_put(skb, sizeof(struct hsr_tag));
  255. hsr_tag->encap_proto = htons(ETH_P_PRP);
  256. set_hsr_tag_LSDU_size(hsr_tag, HSR_V1_SUP_LSDUSIZE);
  257. }
  258. hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
  259. set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
  260. set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
  261. /* From HSRv1 on we have separate supervision sequence numbers. */
  262. spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
  263. if (hsr->prot_version > 0) {
  264. hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
  265. hsr->sup_sequence_nr++;
  266. hsr_tag->sequence_nr = htons(hsr->sequence_nr);
  267. hsr->sequence_nr++;
  268. } else {
  269. hsr_stag->sequence_nr = htons(hsr->sequence_nr);
  270. hsr->sequence_nr++;
  271. }
  272. spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
  273. hsr_stag->HSR_TLV_type = type;
  274. /* TODO: Why 12 in HSRv0? */
  275. hsr_stag->HSR_TLV_length = hsr->prot_version ?
  276. sizeof(struct hsr_sup_payload) : 12;
  277. /* Payload: MacAddressA */
  278. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  279. ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
  280. if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN))
  281. return;
  282. hsr_forward_skb(skb, master);
  283. return;
  284. }
  285. static void send_prp_supervision_frame(struct hsr_port *master,
  286. unsigned long *interval)
  287. {
  288. struct hsr_priv *hsr = master->hsr;
  289. struct hsr_sup_payload *hsr_sp;
  290. struct hsr_sup_tag *hsr_stag;
  291. unsigned long irqflags;
  292. struct sk_buff *skb;
  293. struct prp_rct *rct;
  294. u8 *tail;
  295. skb = hsr_init_skb(master, ETH_P_PRP);
  296. if (!skb) {
  297. WARN_ONCE(1, "PRP: Could not send supervision frame\n");
  298. return;
  299. }
  300. *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  301. hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
  302. set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
  303. set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
  304. /* From HSRv1 on we have separate supervision sequence numbers. */
  305. spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
  306. hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
  307. hsr->sup_sequence_nr++;
  308. hsr_stag->HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
  309. hsr_stag->HSR_TLV_length = sizeof(struct hsr_sup_payload);
  310. /* Payload: MacAddressA */
  311. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  312. ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
  313. if (skb_put_padto(skb, ETH_ZLEN + HSR_HLEN)) {
  314. spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
  315. return;
  316. }
  317. tail = skb_tail_pointer(skb) - HSR_HLEN;
  318. rct = (struct prp_rct *)tail;
  319. rct->PRP_suffix = htons(ETH_P_PRP);
  320. set_prp_LSDU_size(rct, HSR_V1_SUP_LSDUSIZE);
  321. rct->sequence_nr = htons(hsr->sequence_nr);
  322. hsr->sequence_nr++;
  323. spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
  324. hsr_forward_skb(skb, master);
  325. }
  326. /* Announce (supervision frame) timer function
  327. */
  328. static void hsr_announce(struct timer_list *t)
  329. {
  330. struct hsr_priv *hsr;
  331. struct hsr_port *master;
  332. unsigned long interval;
  333. hsr = from_timer(hsr, t, announce_timer);
  334. rcu_read_lock();
  335. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  336. hsr->proto_ops->send_sv_frame(master, &interval);
  337. if (is_admin_up(master->dev))
  338. mod_timer(&hsr->announce_timer, jiffies + interval);
  339. rcu_read_unlock();
  340. }
  341. void hsr_del_ports(struct hsr_priv *hsr)
  342. {
  343. struct hsr_port *port;
  344. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  345. if (port)
  346. hsr_del_port(port);
  347. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  348. if (port)
  349. hsr_del_port(port);
  350. port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  351. if (port)
  352. hsr_del_port(port);
  353. }
  354. static const struct net_device_ops hsr_device_ops = {
  355. .ndo_change_mtu = hsr_dev_change_mtu,
  356. .ndo_open = hsr_dev_open,
  357. .ndo_stop = hsr_dev_close,
  358. .ndo_start_xmit = hsr_dev_xmit,
  359. .ndo_fix_features = hsr_fix_features,
  360. };
  361. static struct device_type hsr_type = {
  362. .name = "hsr",
  363. };
  364. static struct hsr_proto_ops hsr_ops = {
  365. .send_sv_frame = send_hsr_supervision_frame,
  366. .create_tagged_frame = hsr_create_tagged_frame,
  367. .get_untagged_frame = hsr_get_untagged_frame,
  368. .fill_frame_info = hsr_fill_frame_info,
  369. .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
  370. };
  371. static struct hsr_proto_ops prp_ops = {
  372. .send_sv_frame = send_prp_supervision_frame,
  373. .create_tagged_frame = prp_create_tagged_frame,
  374. .get_untagged_frame = prp_get_untagged_frame,
  375. .drop_frame = prp_drop_frame,
  376. .fill_frame_info = prp_fill_frame_info,
  377. .handle_san_frame = prp_handle_san_frame,
  378. .update_san_info = prp_update_san_info,
  379. };
  380. void hsr_dev_setup(struct net_device *dev)
  381. {
  382. eth_hw_addr_random(dev);
  383. ether_setup(dev);
  384. dev->min_mtu = 0;
  385. dev->header_ops = &hsr_header_ops;
  386. dev->netdev_ops = &hsr_device_ops;
  387. SET_NETDEV_DEVTYPE(dev, &hsr_type);
  388. dev->priv_flags |= IFF_NO_QUEUE;
  389. dev->needs_free_netdev = true;
  390. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  391. NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
  392. NETIF_F_HW_VLAN_CTAG_TX;
  393. dev->features = dev->hw_features;
  394. /* Prevent recursive tx locking */
  395. dev->features |= NETIF_F_LLTX;
  396. /* VLAN on top of HSR needs testing and probably some work on
  397. * hsr_header_create() etc.
  398. */
  399. dev->features |= NETIF_F_VLAN_CHALLENGED;
  400. /* Not sure about this. Taken from bridge code. netdev_features.h says
  401. * it means "Does not change network namespaces".
  402. */
  403. dev->features |= NETIF_F_NETNS_LOCAL;
  404. }
  405. /* Return true if dev is a HSR master; return false otherwise.
  406. */
  407. inline bool is_hsr_master(struct net_device *dev)
  408. {
  409. return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
  410. }
  411. /* Default multicast address for HSR Supervision frames */
  412. static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
  413. 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
  414. };
  415. int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
  416. unsigned char multicast_spec, u8 protocol_version,
  417. struct netlink_ext_ack *extack)
  418. {
  419. bool unregister = false;
  420. struct hsr_priv *hsr;
  421. int res;
  422. hsr = netdev_priv(hsr_dev);
  423. INIT_LIST_HEAD(&hsr->ports);
  424. INIT_LIST_HEAD(&hsr->node_db);
  425. INIT_LIST_HEAD(&hsr->self_node_db);
  426. spin_lock_init(&hsr->list_lock);
  427. ether_addr_copy(hsr_dev->dev_addr, slave[0]->dev_addr);
  428. /* initialize protocol specific functions */
  429. if (protocol_version == PRP_V1) {
  430. /* For PRP, lan_id has most significant 3 bits holding
  431. * the net_id of PRP_LAN_ID
  432. */
  433. hsr->net_id = PRP_LAN_ID << 1;
  434. hsr->proto_ops = &prp_ops;
  435. } else {
  436. hsr->proto_ops = &hsr_ops;
  437. }
  438. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  439. res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
  440. slave[1]->dev_addr);
  441. if (res < 0)
  442. return res;
  443. spin_lock_init(&hsr->seqnr_lock);
  444. /* Overflow soon to find bugs easier: */
  445. hsr->sequence_nr = HSR_SEQNR_START;
  446. hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
  447. timer_setup(&hsr->announce_timer, hsr_announce, 0);
  448. timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
  449. ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
  450. hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
  451. hsr->prot_version = protocol_version;
  452. /* FIXME: should I modify the value of these?
  453. *
  454. * - hsr_dev->flags - i.e.
  455. * IFF_MASTER/SLAVE?
  456. * - hsr_dev->priv_flags - i.e.
  457. * IFF_EBRIDGE?
  458. * IFF_TX_SKB_SHARING?
  459. * IFF_HSR_MASTER/SLAVE?
  460. */
  461. /* Make sure the 1st call to netif_carrier_on() gets through */
  462. netif_carrier_off(hsr_dev);
  463. res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
  464. if (res)
  465. goto err_add_master;
  466. res = register_netdevice(hsr_dev);
  467. if (res)
  468. goto err_unregister;
  469. unregister = true;
  470. res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
  471. if (res)
  472. goto err_unregister;
  473. res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
  474. if (res)
  475. goto err_unregister;
  476. hsr_debugfs_init(hsr, hsr_dev);
  477. mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
  478. return 0;
  479. err_unregister:
  480. hsr_del_ports(hsr);
  481. err_add_master:
  482. hsr_del_self_node(hsr);
  483. if (unregister)
  484. unregister_netdevice(hsr_dev);
  485. return res;
  486. }