hsr_device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* Copyright 2011-2013 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.
  7. *
  8. * Author(s):
  9. * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
  10. *
  11. * This file contains device methods for creating, using and destroying
  12. * virtual HSR devices.
  13. */
  14. #include <linux/netdevice.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/pkt_sched.h>
  20. #include "hsr_device.h"
  21. #include "hsr_framereg.h"
  22. #include "hsr_main.h"
  23. static bool is_admin_up(struct net_device *dev)
  24. {
  25. return dev && (dev->flags & IFF_UP);
  26. }
  27. static bool is_slave_up(struct net_device *dev)
  28. {
  29. return dev && is_admin_up(dev) && netif_oper_up(dev);
  30. }
  31. static void __hsr_set_operstate(struct net_device *dev, int transition)
  32. {
  33. write_lock_bh(&dev_base_lock);
  34. if (dev->operstate != transition) {
  35. dev->operstate = transition;
  36. write_unlock_bh(&dev_base_lock);
  37. netdev_state_change(dev);
  38. } else {
  39. write_unlock_bh(&dev_base_lock);
  40. }
  41. }
  42. void hsr_set_operstate(struct net_device *hsr_dev, struct net_device *slave1,
  43. struct net_device *slave2)
  44. {
  45. if (!is_admin_up(hsr_dev)) {
  46. __hsr_set_operstate(hsr_dev, IF_OPER_DOWN);
  47. return;
  48. }
  49. if (is_slave_up(slave1) || is_slave_up(slave2))
  50. __hsr_set_operstate(hsr_dev, IF_OPER_UP);
  51. else
  52. __hsr_set_operstate(hsr_dev, IF_OPER_LOWERLAYERDOWN);
  53. }
  54. void hsr_set_carrier(struct net_device *hsr_dev, struct net_device *slave1,
  55. struct net_device *slave2)
  56. {
  57. if (is_slave_up(slave1) || is_slave_up(slave2))
  58. netif_carrier_on(hsr_dev);
  59. else
  60. netif_carrier_off(hsr_dev);
  61. }
  62. void hsr_check_announce(struct net_device *hsr_dev, int old_operstate)
  63. {
  64. struct hsr_priv *hsr_priv;
  65. hsr_priv = netdev_priv(hsr_dev);
  66. if ((hsr_dev->operstate == IF_OPER_UP) && (old_operstate != IF_OPER_UP)) {
  67. /* Went up */
  68. hsr_priv->announce_count = 0;
  69. hsr_priv->announce_timer.expires = jiffies +
  70. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  71. add_timer(&hsr_priv->announce_timer);
  72. }
  73. if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP))
  74. /* Went down */
  75. del_timer(&hsr_priv->announce_timer);
  76. }
  77. int hsr_get_max_mtu(struct hsr_priv *hsr_priv)
  78. {
  79. int mtu_max;
  80. if (hsr_priv->slave[0] && hsr_priv->slave[1])
  81. mtu_max = min(hsr_priv->slave[0]->mtu, hsr_priv->slave[1]->mtu);
  82. else if (hsr_priv->slave[0])
  83. mtu_max = hsr_priv->slave[0]->mtu;
  84. else if (hsr_priv->slave[1])
  85. mtu_max = hsr_priv->slave[1]->mtu;
  86. else
  87. mtu_max = HSR_TAGLEN;
  88. return mtu_max - HSR_TAGLEN;
  89. }
  90. static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
  91. {
  92. struct hsr_priv *hsr_priv;
  93. hsr_priv = netdev_priv(dev);
  94. if (new_mtu > hsr_get_max_mtu(hsr_priv)) {
  95. netdev_info(hsr_priv->dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
  96. HSR_TAGLEN);
  97. return -EINVAL;
  98. }
  99. dev->mtu = new_mtu;
  100. return 0;
  101. }
  102. static int hsr_dev_open(struct net_device *dev)
  103. {
  104. struct hsr_priv *hsr_priv;
  105. int i;
  106. char *slave_name;
  107. hsr_priv = netdev_priv(dev);
  108. for (i = 0; i < HSR_MAX_SLAVE; i++) {
  109. if (hsr_priv->slave[i])
  110. slave_name = hsr_priv->slave[i]->name;
  111. else
  112. slave_name = "null";
  113. if (!is_slave_up(hsr_priv->slave[i]))
  114. netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a working HSR network\n",
  115. 'A' + i, slave_name);
  116. }
  117. return 0;
  118. }
  119. static int hsr_dev_close(struct net_device *dev)
  120. {
  121. /* Nothing to do here. We could try to restore the state of the slaves
  122. * to what they were before being changed by the hsr master dev's state,
  123. * but they might have been changed manually in the mean time too, so
  124. * taking them up or down here might be confusing and is probably not a
  125. * good idea.
  126. */
  127. return 0;
  128. }
  129. static void hsr_fill_tag(struct hsr_ethhdr *hsr_ethhdr, struct hsr_priv *hsr_priv)
  130. {
  131. unsigned long irqflags;
  132. /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values
  133. * between 0001-1001 ("ring identifier", for regular HSR frames),
  134. * or 1111 ("HSR management", supervision frames). Unfortunately, the
  135. * spec writers forgot to explain what a "ring identifier" is, or
  136. * how it is used. So we just set this to 0001 for regular frames,
  137. * and 1111 for supervision frames.
  138. */
  139. set_hsr_tag_path(&hsr_ethhdr->hsr_tag, 0x1);
  140. /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet
  141. * frame is the content of the frame located between the Length/Type
  142. * field and the Frame Check Sequence."
  143. *
  144. * IEC 62439-3, p 48, specifies the "original LPDU" to include the
  145. * original "LT" field (what "LT" means is not explained anywhere as
  146. * far as I can see - perhaps "Length/Type"?). So LSDU_size might
  147. * equal original length + 2.
  148. * Also, the fact that this field is not used anywhere (might be used
  149. * by a RedBox connecting HSR and PRP nets?) means I cannot test its
  150. * correctness. Instead of guessing, I set this to 0 here, to make any
  151. * problems immediately apparent. Anyone using this driver with PRP/HSR
  152. * RedBoxes might need to fix this...
  153. */
  154. set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, 0);
  155. spin_lock_irqsave(&hsr_priv->seqnr_lock, irqflags);
  156. hsr_ethhdr->hsr_tag.sequence_nr = htons(hsr_priv->sequence_nr);
  157. hsr_priv->sequence_nr++;
  158. spin_unlock_irqrestore(&hsr_priv->seqnr_lock, irqflags);
  159. hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto;
  160. hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP);
  161. }
  162. static int slave_xmit(struct sk_buff *skb, struct hsr_priv *hsr_priv,
  163. enum hsr_dev_idx dev_idx)
  164. {
  165. struct hsr_ethhdr *hsr_ethhdr;
  166. hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
  167. skb->dev = hsr_priv->slave[dev_idx];
  168. hsr_addr_subst_dest(hsr_priv, &hsr_ethhdr->ethhdr, dev_idx);
  169. /* Address substitution (IEC62439-3 pp 26, 50): replace mac
  170. * address of outgoing frame with that of the outgoing slave's.
  171. */
  172. memcpy(hsr_ethhdr->ethhdr.h_source, skb->dev->dev_addr, ETH_ALEN);
  173. return dev_queue_xmit(skb);
  174. }
  175. static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  176. {
  177. struct hsr_priv *hsr_priv;
  178. struct hsr_ethhdr *hsr_ethhdr;
  179. struct sk_buff *skb2;
  180. int res1, res2;
  181. hsr_priv = netdev_priv(dev);
  182. hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
  183. if ((skb->protocol != htons(ETH_P_PRP)) ||
  184. (hsr_ethhdr->ethhdr.h_proto != htons(ETH_P_PRP))) {
  185. hsr_fill_tag(hsr_ethhdr, hsr_priv);
  186. skb->protocol = htons(ETH_P_PRP);
  187. }
  188. skb2 = pskb_copy(skb, GFP_ATOMIC);
  189. res1 = NET_XMIT_DROP;
  190. if (likely(hsr_priv->slave[HSR_DEV_SLAVE_A]))
  191. res1 = slave_xmit(skb, hsr_priv, HSR_DEV_SLAVE_A);
  192. res2 = NET_XMIT_DROP;
  193. if (likely(skb2 && hsr_priv->slave[HSR_DEV_SLAVE_B]))
  194. res2 = slave_xmit(skb2, hsr_priv, HSR_DEV_SLAVE_B);
  195. if (likely(res1 == NET_XMIT_SUCCESS || res1 == NET_XMIT_CN ||
  196. res2 == NET_XMIT_SUCCESS || res2 == NET_XMIT_CN)) {
  197. hsr_priv->dev->stats.tx_packets++;
  198. hsr_priv->dev->stats.tx_bytes += skb->len;
  199. } else {
  200. hsr_priv->dev->stats.tx_dropped++;
  201. }
  202. return NETDEV_TX_OK;
  203. }
  204. static int hsr_header_create(struct sk_buff *skb, struct net_device *dev,
  205. unsigned short type, const void *daddr,
  206. const void *saddr, unsigned int len)
  207. {
  208. int res;
  209. /* Make room for the HSR tag now. We will fill it in later (in
  210. * hsr_dev_xmit)
  211. */
  212. if (skb_headroom(skb) < HSR_TAGLEN + ETH_HLEN)
  213. return -ENOBUFS;
  214. skb_push(skb, HSR_TAGLEN);
  215. /* To allow VLAN/HSR combos we should probably use
  216. * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_TAGLEN);
  217. * here instead. It would require other changes too, though - e.g.
  218. * separate headers for each slave etc...
  219. */
  220. res = eth_header(skb, dev, type, daddr, saddr, len + HSR_TAGLEN);
  221. if (res <= 0)
  222. return res;
  223. skb_reset_mac_header(skb);
  224. return res + HSR_TAGLEN;
  225. }
  226. static const struct header_ops hsr_header_ops = {
  227. .create = hsr_header_create,
  228. .parse = eth_header_parse,
  229. };
  230. /* HSR:2010 supervision frames should be padded so that the whole frame,
  231. * including headers and FCS, is 64 bytes (without VLAN).
  232. */
  233. static int hsr_pad(int size)
  234. {
  235. const int min_size = ETH_ZLEN - HSR_TAGLEN - ETH_HLEN;
  236. if (size >= min_size)
  237. return size;
  238. return min_size;
  239. }
  240. static void send_hsr_supervision_frame(struct net_device *hsr_dev, u8 type)
  241. {
  242. struct hsr_priv *hsr_priv;
  243. struct sk_buff *skb;
  244. int hlen, tlen;
  245. struct hsr_sup_tag *hsr_stag;
  246. struct hsr_sup_payload *hsr_sp;
  247. unsigned long irqflags;
  248. hlen = LL_RESERVED_SPACE(hsr_dev);
  249. tlen = hsr_dev->needed_tailroom;
  250. skb = alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload)) + hlen + tlen,
  251. GFP_ATOMIC);
  252. if (skb == NULL)
  253. return;
  254. hsr_priv = netdev_priv(hsr_dev);
  255. skb_reserve(skb, hlen);
  256. skb->dev = hsr_dev;
  257. skb->protocol = htons(ETH_P_PRP);
  258. skb->priority = TC_PRIO_CONTROL;
  259. if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
  260. hsr_priv->sup_multicast_addr,
  261. skb->dev->dev_addr, skb->len) < 0)
  262. goto out;
  263. skb_pull(skb, sizeof(struct ethhdr));
  264. hsr_stag = (typeof(hsr_stag)) skb->data;
  265. set_hsr_stag_path(hsr_stag, 0xf);
  266. set_hsr_stag_HSR_Ver(hsr_stag, 0);
  267. spin_lock_irqsave(&hsr_priv->seqnr_lock, irqflags);
  268. hsr_stag->sequence_nr = htons(hsr_priv->sequence_nr);
  269. hsr_priv->sequence_nr++;
  270. spin_unlock_irqrestore(&hsr_priv->seqnr_lock, irqflags);
  271. hsr_stag->HSR_TLV_Type = type;
  272. hsr_stag->HSR_TLV_Length = 12;
  273. skb_push(skb, sizeof(struct ethhdr));
  274. /* Payload: MacAddressA */
  275. hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(*hsr_sp));
  276. memcpy(hsr_sp->MacAddressA, hsr_dev->dev_addr, ETH_ALEN);
  277. dev_queue_xmit(skb);
  278. return;
  279. out:
  280. kfree_skb(skb);
  281. }
  282. /* Announce (supervision frame) timer function
  283. */
  284. static void hsr_announce(unsigned long data)
  285. {
  286. struct hsr_priv *hsr_priv;
  287. hsr_priv = (struct hsr_priv *) data;
  288. if (hsr_priv->announce_count < 3) {
  289. send_hsr_supervision_frame(hsr_priv->dev, HSR_TLV_ANNOUNCE);
  290. hsr_priv->announce_count++;
  291. } else {
  292. send_hsr_supervision_frame(hsr_priv->dev, HSR_TLV_LIFE_CHECK);
  293. }
  294. if (hsr_priv->announce_count < 3)
  295. hsr_priv->announce_timer.expires = jiffies +
  296. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  297. else
  298. hsr_priv->announce_timer.expires = jiffies +
  299. msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  300. if (is_admin_up(hsr_priv->dev))
  301. add_timer(&hsr_priv->announce_timer);
  302. }
  303. static void restore_slaves(struct net_device *hsr_dev)
  304. {
  305. struct hsr_priv *hsr_priv;
  306. int i;
  307. int res;
  308. hsr_priv = netdev_priv(hsr_dev);
  309. rtnl_lock();
  310. /* Restore promiscuity */
  311. for (i = 0; i < HSR_MAX_SLAVE; i++) {
  312. if (!hsr_priv->slave[i])
  313. continue;
  314. res = dev_set_promiscuity(hsr_priv->slave[i], -1);
  315. if (res)
  316. netdev_info(hsr_dev,
  317. "Cannot restore slave promiscuity (%s, %d)\n",
  318. hsr_priv->slave[i]->name, res);
  319. }
  320. rtnl_unlock();
  321. }
  322. static void reclaim_hsr_dev(struct rcu_head *rh)
  323. {
  324. struct hsr_priv *hsr_priv;
  325. hsr_priv = container_of(rh, struct hsr_priv, rcu_head);
  326. free_netdev(hsr_priv->dev);
  327. }
  328. /* According to comments in the declaration of struct net_device, this function
  329. * is "Called from unregister, can be used to call free_netdev". Ok then...
  330. */
  331. static void hsr_dev_destroy(struct net_device *hsr_dev)
  332. {
  333. struct hsr_priv *hsr_priv;
  334. hsr_priv = netdev_priv(hsr_dev);
  335. del_timer(&hsr_priv->announce_timer);
  336. unregister_hsr_master(hsr_priv); /* calls list_del_rcu on hsr_priv */
  337. restore_slaves(hsr_dev);
  338. call_rcu(&hsr_priv->rcu_head, reclaim_hsr_dev); /* reclaim hsr_priv */
  339. }
  340. static const struct net_device_ops hsr_device_ops = {
  341. .ndo_change_mtu = hsr_dev_change_mtu,
  342. .ndo_open = hsr_dev_open,
  343. .ndo_stop = hsr_dev_close,
  344. .ndo_start_xmit = hsr_dev_xmit,
  345. };
  346. void hsr_dev_setup(struct net_device *dev)
  347. {
  348. random_ether_addr(dev->dev_addr);
  349. ether_setup(dev);
  350. dev->header_ops = &hsr_header_ops;
  351. dev->netdev_ops = &hsr_device_ops;
  352. dev->tx_queue_len = 0;
  353. dev->destructor = hsr_dev_destroy;
  354. }
  355. /* Return true if dev is a HSR master; return false otherwise.
  356. */
  357. bool is_hsr_master(struct net_device *dev)
  358. {
  359. return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
  360. }
  361. static int check_slave_ok(struct net_device *dev)
  362. {
  363. /* Don't allow HSR on non-ethernet like devices */
  364. if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) ||
  365. (dev->addr_len != ETH_ALEN)) {
  366. netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n");
  367. return -EINVAL;
  368. }
  369. /* Don't allow enslaving hsr devices */
  370. if (is_hsr_master(dev)) {
  371. netdev_info(dev, "Cannot create trees of HSR devices.\n");
  372. return -EINVAL;
  373. }
  374. if (is_hsr_slave(dev)) {
  375. netdev_info(dev, "This device is already a HSR slave.\n");
  376. return -EINVAL;
  377. }
  378. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  379. netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n");
  380. return -EINVAL;
  381. }
  382. /* HSR over bonded devices has not been tested, but I'm not sure it
  383. * won't work...
  384. */
  385. return 0;
  386. }
  387. /* Default multicast address for HSR Supervision frames */
  388. static const unsigned char def_multicast_addr[ETH_ALEN] = {
  389. 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
  390. };
  391. int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
  392. unsigned char multicast_spec)
  393. {
  394. struct hsr_priv *hsr_priv;
  395. int i;
  396. int res;
  397. hsr_priv = netdev_priv(hsr_dev);
  398. hsr_priv->dev = hsr_dev;
  399. INIT_LIST_HEAD(&hsr_priv->node_db);
  400. INIT_LIST_HEAD(&hsr_priv->self_node_db);
  401. for (i = 0; i < HSR_MAX_SLAVE; i++)
  402. hsr_priv->slave[i] = slave[i];
  403. spin_lock_init(&hsr_priv->seqnr_lock);
  404. /* Overflow soon to find bugs easier: */
  405. hsr_priv->sequence_nr = USHRT_MAX - 1024;
  406. init_timer(&hsr_priv->announce_timer);
  407. hsr_priv->announce_timer.function = hsr_announce;
  408. hsr_priv->announce_timer.data = (unsigned long) hsr_priv;
  409. memcpy(hsr_priv->sup_multicast_addr, def_multicast_addr, ETH_ALEN);
  410. hsr_priv->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
  411. /* FIXME: should I modify the value of these?
  412. *
  413. * - hsr_dev->flags - i.e.
  414. * IFF_MASTER/SLAVE?
  415. * - hsr_dev->priv_flags - i.e.
  416. * IFF_EBRIDGE?
  417. * IFF_TX_SKB_SHARING?
  418. * IFF_HSR_MASTER/SLAVE?
  419. */
  420. for (i = 0; i < HSR_MAX_SLAVE; i++) {
  421. res = check_slave_ok(slave[i]);
  422. if (res)
  423. return res;
  424. }
  425. hsr_dev->features = slave[0]->features & slave[1]->features;
  426. /* Prevent recursive tx locking */
  427. hsr_dev->features |= NETIF_F_LLTX;
  428. /* VLAN on top of HSR needs testing and probably some work on
  429. * hsr_header_create() etc.
  430. */
  431. hsr_dev->features |= NETIF_F_VLAN_CHALLENGED;
  432. /* Set hsr_dev's MAC address to that of mac_slave1 */
  433. memcpy(hsr_dev->dev_addr, hsr_priv->slave[0]->dev_addr, ETH_ALEN);
  434. /* Set required header length */
  435. for (i = 0; i < HSR_MAX_SLAVE; i++) {
  436. if (slave[i]->hard_header_len + HSR_TAGLEN >
  437. hsr_dev->hard_header_len)
  438. hsr_dev->hard_header_len =
  439. slave[i]->hard_header_len + HSR_TAGLEN;
  440. }
  441. /* MTU */
  442. for (i = 0; i < HSR_MAX_SLAVE; i++)
  443. if (slave[i]->mtu - HSR_TAGLEN < hsr_dev->mtu)
  444. hsr_dev->mtu = slave[i]->mtu - HSR_TAGLEN;
  445. /* Make sure the 1st call to netif_carrier_on() gets through */
  446. netif_carrier_off(hsr_dev);
  447. /* Promiscuity */
  448. for (i = 0; i < HSR_MAX_SLAVE; i++) {
  449. res = dev_set_promiscuity(slave[i], 1);
  450. if (res) {
  451. netdev_info(hsr_dev, "Cannot set slave promiscuity (%s, %d)\n",
  452. slave[i]->name, res);
  453. goto fail;
  454. }
  455. }
  456. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  457. res = hsr_create_self_node(&hsr_priv->self_node_db,
  458. hsr_dev->dev_addr,
  459. hsr_priv->slave[1]->dev_addr);
  460. if (res < 0)
  461. goto fail;
  462. res = register_netdevice(hsr_dev);
  463. if (res)
  464. goto fail;
  465. register_hsr_master(hsr_priv);
  466. return 0;
  467. fail:
  468. restore_slaves(hsr_dev);
  469. return res;
  470. }