hsr_device.c 12 KB

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