hsr_device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* 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.
  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. #include <linux/netdevice.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/pkt_sched.h>
  19. #include "hsr_device.h"
  20. #include "hsr_slave.h"
  21. #include "hsr_framereg.h"
  22. #include "hsr_main.h"
  23. #include "hsr_forward.h"
  24. static bool is_admin_up(struct net_device *dev)
  25. {
  26. return dev && (dev->flags & IFF_UP);
  27. }
  28. static bool is_slave_up(struct net_device *dev)
  29. {
  30. return dev && is_admin_up(dev) && netif_oper_up(dev);
  31. }
  32. static void __hsr_set_operstate(struct net_device *dev, int transition)
  33. {
  34. write_lock_bh(&dev_base_lock);
  35. if (dev->operstate != transition) {
  36. dev->operstate = transition;
  37. write_unlock_bh(&dev_base_lock);
  38. netdev_state_change(dev);
  39. } else {
  40. write_unlock_bh(&dev_base_lock);
  41. }
  42. }
  43. static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
  44. {
  45. if (!is_admin_up(master->dev)) {
  46. __hsr_set_operstate(master->dev, IF_OPER_DOWN);
  47. return;
  48. }
  49. if (has_carrier)
  50. __hsr_set_operstate(master->dev, IF_OPER_UP);
  51. else
  52. __hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN);
  53. }
  54. static bool hsr_check_carrier(struct hsr_port *master)
  55. {
  56. struct hsr_port *port;
  57. bool has_carrier;
  58. has_carrier = false;
  59. rcu_read_lock();
  60. hsr_for_each_port(master->hsr, port)
  61. if ((port->type != HSR_PT_MASTER) && is_slave_up(port->dev)) {
  62. has_carrier = true;
  63. break;
  64. }
  65. rcu_read_unlock();
  66. if (has_carrier)
  67. netif_carrier_on(master->dev);
  68. else
  69. netif_carrier_off(master->dev);
  70. return has_carrier;
  71. }
  72. static void hsr_check_announce(struct net_device *hsr_dev,
  73. unsigned char old_operstate)
  74. {
  75. struct hsr_priv *hsr;
  76. hsr = netdev_priv(hsr_dev);
  77. if ((hsr_dev->operstate == IF_OPER_UP)
  78. && (old_operstate != IF_OPER_UP)) {
  79. /* Went up */
  80. hsr->announce_count = 0;
  81. hsr->announce_timer.expires = jiffies +
  82. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  83. add_timer(&hsr->announce_timer);
  84. }
  85. if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP))
  86. /* Went down */
  87. del_timer(&hsr->announce_timer);
  88. }
  89. void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
  90. {
  91. struct hsr_port *master;
  92. unsigned char old_operstate;
  93. bool has_carrier;
  94. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  95. /* netif_stacked_transfer_operstate() cannot be used here since
  96. * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
  97. */
  98. old_operstate = master->dev->operstate;
  99. has_carrier = hsr_check_carrier(master);
  100. hsr_set_operstate(master, has_carrier);
  101. hsr_check_announce(master->dev, old_operstate);
  102. }
  103. int hsr_get_max_mtu(struct hsr_priv *hsr)
  104. {
  105. unsigned int mtu_max;
  106. struct hsr_port *port;
  107. mtu_max = ETH_DATA_LEN;
  108. rcu_read_lock();
  109. hsr_for_each_port(hsr, port)
  110. if (port->type != HSR_PT_MASTER)
  111. mtu_max = min(port->dev->mtu, mtu_max);
  112. rcu_read_unlock();
  113. if (mtu_max < HSR_HLEN)
  114. return 0;
  115. return mtu_max - HSR_HLEN;
  116. }
  117. static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
  118. {
  119. struct hsr_priv *hsr;
  120. struct hsr_port *master;
  121. hsr = netdev_priv(dev);
  122. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  123. if (new_mtu > hsr_get_max_mtu(hsr)) {
  124. 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",
  125. HSR_HLEN);
  126. return -EINVAL;
  127. }
  128. dev->mtu = new_mtu;
  129. return 0;
  130. }
  131. static int hsr_dev_open(struct net_device *dev)
  132. {
  133. struct hsr_priv *hsr;
  134. struct hsr_port *port;
  135. char designation;
  136. hsr = netdev_priv(dev);
  137. designation = '\0';
  138. rcu_read_lock();
  139. hsr_for_each_port(hsr, port) {
  140. if (port->type == HSR_PT_MASTER)
  141. continue;
  142. switch (port->type) {
  143. case HSR_PT_SLAVE_A:
  144. designation = 'A';
  145. break;
  146. case HSR_PT_SLAVE_B:
  147. designation = 'B';
  148. break;
  149. default:
  150. designation = '?';
  151. }
  152. if (!is_slave_up(port->dev))
  153. netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a fully working HSR network\n",
  154. designation, port->dev->name);
  155. }
  156. rcu_read_unlock();
  157. if (designation == '\0')
  158. netdev_warn(dev, "No slave devices configured\n");
  159. return 0;
  160. }
  161. static int hsr_dev_close(struct net_device *dev)
  162. {
  163. /* Nothing to do here. */
  164. return 0;
  165. }
  166. static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
  167. netdev_features_t features)
  168. {
  169. netdev_features_t mask;
  170. struct hsr_port *port;
  171. mask = features;
  172. /* Mask out all features that, if supported by one device, should be
  173. * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
  174. *
  175. * Anything that's off in mask will not be enabled - so only things
  176. * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
  177. * may become enabled.
  178. */
  179. features &= ~NETIF_F_ONE_FOR_ALL;
  180. hsr_for_each_port(hsr, port)
  181. features = netdev_increment_features(features,
  182. port->dev->features,
  183. mask);
  184. return features;
  185. }
  186. static netdev_features_t hsr_fix_features(struct net_device *dev,
  187. netdev_features_t features)
  188. {
  189. struct hsr_priv *hsr = netdev_priv(dev);
  190. return hsr_features_recompute(hsr, features);
  191. }
  192. static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  193. {
  194. struct hsr_priv *hsr = netdev_priv(dev);
  195. struct hsr_port *master;
  196. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  197. skb->dev = master->dev;
  198. hsr_forward_skb(skb, master);
  199. return NETDEV_TX_OK;
  200. }
  201. static const struct header_ops hsr_header_ops = {
  202. .create = eth_header,
  203. .parse = eth_header_parse,
  204. };
  205. static void send_hsr_supervision_frame(struct hsr_port *master,
  206. u8 type, u8 hsrVer)
  207. {
  208. struct sk_buff *skb;
  209. int hlen, tlen;
  210. struct hsr_tag *hsr_tag;
  211. struct hsr_sup_tag *hsr_stag;
  212. struct hsr_sup_payload *hsr_sp;
  213. unsigned long irqflags;
  214. hlen = LL_RESERVED_SPACE(master->dev);
  215. tlen = master->dev->needed_tailroom;
  216. skb = dev_alloc_skb(
  217. sizeof(struct hsr_tag) +
  218. sizeof(struct hsr_sup_tag) +
  219. sizeof(struct hsr_sup_payload) + hlen + tlen);
  220. if (skb == NULL)
  221. return;
  222. skb_reserve(skb, hlen);
  223. skb->dev = master->dev;
  224. skb->protocol = htons(hsrVer ? ETH_P_HSR : ETH_P_PRP);
  225. skb->priority = TC_PRIO_CONTROL;
  226. if (dev_hard_header(skb, skb->dev, (hsrVer ? ETH_P_HSR : ETH_P_PRP),
  227. master->hsr->sup_multicast_addr,
  228. skb->dev->dev_addr, skb->len) <= 0)
  229. goto out;
  230. skb_reset_mac_header(skb);
  231. if (hsrVer > 0) {
  232. hsr_tag = (typeof(hsr_tag)) skb_put(skb, sizeof(struct hsr_tag));
  233. hsr_tag->encap_proto = htons(ETH_P_PRP);
  234. set_hsr_tag_LSDU_size(hsr_tag, HSR_V1_SUP_LSDUSIZE);
  235. }
  236. hsr_stag = (typeof(hsr_stag)) skb_put(skb, sizeof(struct hsr_sup_tag));
  237. set_hsr_stag_path(hsr_stag, (hsrVer ? 0x0 : 0xf));
  238. set_hsr_stag_HSR_Ver(hsr_stag, hsrVer);
  239. /* From HSRv1 on we have separate supervision sequence numbers. */
  240. spin_lock_irqsave(&master->hsr->seqnr_lock, irqflags);
  241. if (hsrVer > 0) {
  242. hsr_stag->sequence_nr = htons(master->hsr->sup_sequence_nr);
  243. hsr_tag->sequence_nr = htons(master->hsr->sequence_nr);
  244. master->hsr->sup_sequence_nr++;
  245. master->hsr->sequence_nr++;
  246. } else {
  247. hsr_stag->sequence_nr = htons(master->hsr->sequence_nr);
  248. master->hsr->sequence_nr++;
  249. }
  250. spin_unlock_irqrestore(&master->hsr->seqnr_lock, irqflags);
  251. hsr_stag->HSR_TLV_Type = type;
  252. /* TODO: Why 12 in HSRv0? */
  253. hsr_stag->HSR_TLV_Length = hsrVer ? sizeof(struct hsr_sup_payload) : 12;
  254. /* Payload: MacAddressA */
  255. hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(struct hsr_sup_payload));
  256. ether_addr_copy(hsr_sp->MacAddressA, master->dev->dev_addr);
  257. skb_put_padto(skb, ETH_ZLEN + HSR_HLEN);
  258. hsr_forward_skb(skb, master);
  259. return;
  260. out:
  261. WARN_ONCE(1, "HSR: Could not send supervision frame\n");
  262. kfree_skb(skb);
  263. }
  264. /* Announce (supervision frame) timer function
  265. */
  266. static void hsr_announce(unsigned long data)
  267. {
  268. struct hsr_priv *hsr;
  269. struct hsr_port *master;
  270. hsr = (struct hsr_priv *) data;
  271. rcu_read_lock();
  272. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  273. if (hsr->announce_count < 3 && hsr->protVersion == 0) {
  274. send_hsr_supervision_frame(master, HSR_TLV_ANNOUNCE,
  275. hsr->protVersion);
  276. hsr->announce_count++;
  277. hsr->announce_timer.expires = jiffies +
  278. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  279. } else {
  280. send_hsr_supervision_frame(master, HSR_TLV_LIFE_CHECK,
  281. hsr->protVersion);
  282. hsr->announce_timer.expires = jiffies +
  283. msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  284. }
  285. if (is_admin_up(master->dev))
  286. add_timer(&hsr->announce_timer);
  287. rcu_read_unlock();
  288. }
  289. /* According to comments in the declaration of struct net_device, this function
  290. * is "Called from unregister, can be used to call free_netdev". Ok then...
  291. */
  292. static void hsr_dev_destroy(struct net_device *hsr_dev)
  293. {
  294. struct hsr_priv *hsr;
  295. struct hsr_port *port;
  296. hsr = netdev_priv(hsr_dev);
  297. rtnl_lock();
  298. hsr_for_each_port(hsr, port)
  299. hsr_del_port(port);
  300. rtnl_unlock();
  301. del_timer_sync(&hsr->prune_timer);
  302. del_timer_sync(&hsr->announce_timer);
  303. synchronize_rcu();
  304. free_netdev(hsr_dev);
  305. }
  306. static const struct net_device_ops hsr_device_ops = {
  307. .ndo_change_mtu = hsr_dev_change_mtu,
  308. .ndo_open = hsr_dev_open,
  309. .ndo_stop = hsr_dev_close,
  310. .ndo_start_xmit = hsr_dev_xmit,
  311. .ndo_fix_features = hsr_fix_features,
  312. };
  313. static struct device_type hsr_type = {
  314. .name = "hsr",
  315. };
  316. void hsr_dev_setup(struct net_device *dev)
  317. {
  318. random_ether_addr(dev->dev_addr);
  319. ether_setup(dev);
  320. dev->header_ops = &hsr_header_ops;
  321. dev->netdev_ops = &hsr_device_ops;
  322. SET_NETDEV_DEVTYPE(dev, &hsr_type);
  323. dev->priv_flags |= IFF_NO_QUEUE;
  324. dev->destructor = hsr_dev_destroy;
  325. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  326. NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
  327. NETIF_F_HW_VLAN_CTAG_TX;
  328. dev->features = dev->hw_features;
  329. /* Prevent recursive tx locking */
  330. dev->features |= NETIF_F_LLTX;
  331. /* VLAN on top of HSR needs testing and probably some work on
  332. * hsr_header_create() etc.
  333. */
  334. dev->features |= NETIF_F_VLAN_CHALLENGED;
  335. /* Not sure about this. Taken from bridge code. netdev_features.h says
  336. * it means "Does not change network namespaces".
  337. */
  338. dev->features |= NETIF_F_NETNS_LOCAL;
  339. }
  340. /* Return true if dev is a HSR master; return false otherwise.
  341. */
  342. inline bool is_hsr_master(struct net_device *dev)
  343. {
  344. return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
  345. }
  346. /* Default multicast address for HSR Supervision frames */
  347. static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
  348. 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
  349. };
  350. int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
  351. unsigned char multicast_spec, u8 protocol_version)
  352. {
  353. struct hsr_priv *hsr;
  354. struct hsr_port *port;
  355. int res;
  356. hsr = netdev_priv(hsr_dev);
  357. INIT_LIST_HEAD(&hsr->ports);
  358. INIT_LIST_HEAD(&hsr->node_db);
  359. INIT_LIST_HEAD(&hsr->self_node_db);
  360. ether_addr_copy(hsr_dev->dev_addr, slave[0]->dev_addr);
  361. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  362. res = hsr_create_self_node(&hsr->self_node_db, hsr_dev->dev_addr,
  363. slave[1]->dev_addr);
  364. if (res < 0)
  365. return res;
  366. spin_lock_init(&hsr->seqnr_lock);
  367. /* Overflow soon to find bugs easier: */
  368. hsr->sequence_nr = HSR_SEQNR_START;
  369. hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
  370. setup_timer(&hsr->announce_timer, hsr_announce, (unsigned long)hsr);
  371. setup_timer(&hsr->prune_timer, hsr_prune_nodes, (unsigned long)hsr);
  372. ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
  373. hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
  374. hsr->protVersion = protocol_version;
  375. /* FIXME: should I modify the value of these?
  376. *
  377. * - hsr_dev->flags - i.e.
  378. * IFF_MASTER/SLAVE?
  379. * - hsr_dev->priv_flags - i.e.
  380. * IFF_EBRIDGE?
  381. * IFF_TX_SKB_SHARING?
  382. * IFF_HSR_MASTER/SLAVE?
  383. */
  384. /* Make sure the 1st call to netif_carrier_on() gets through */
  385. netif_carrier_off(hsr_dev);
  386. res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER);
  387. if (res)
  388. return res;
  389. res = register_netdevice(hsr_dev);
  390. if (res)
  391. goto fail;
  392. res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A);
  393. if (res)
  394. goto fail;
  395. res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B);
  396. if (res)
  397. goto fail;
  398. mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
  399. return 0;
  400. fail:
  401. hsr_for_each_port(hsr, port)
  402. hsr_del_port(port);
  403. return res;
  404. }