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