hsr_prp_main.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #ifndef __HSR_PRP_PRIVATE_H
  12. #define __HSR_PRP_PRIVATE_H
  13. #include <linux/netdevice.h>
  14. #include <linux/list.h>
  15. #include <linux/if_vlan.h>
  16. /* Time constants as specified in the HSR specification (IEC-62439-3 2010)
  17. * Table 8.
  18. * All values in milliseconds.
  19. */
  20. #define HSR_PRP_LIFE_CHECK_INTERVAL 2000 /* ms */
  21. #define HSR_PRP_NODE_FORGET_TIME 60000 /* ms */
  22. #define HSR_PRP_ANNOUNCE_INTERVAL 100 /* ms */
  23. /* By how much may slave1 and slave2 timestamps of latest received frame from
  24. * each node differ before we notify of communication problem?
  25. */
  26. #define HSR_PRP_MAX_SLAVE_DIFF 3000 /* ms */
  27. #define HSR_PRP_SEQNR_START (USHRT_MAX - 1024)
  28. #define HSR_PRP_SUP_SEQNR_START (HSR_PRP_SEQNR_START / 2)
  29. /* How often shall we check for broken ring and remove node entries older than
  30. * HSR_NODE_FORGET_TIME?
  31. */
  32. #define HSR_PRP_PRUNE_PERIOD 3000 /* ms */
  33. #define HSR_TLV_ANNOUNCE 22
  34. #define HSR_TLV_LIFE_CHECK 23
  35. /* PRP V1 life check for Duplicate discard */
  36. #define PRP_TLV_LIFE_CHECK_DD 20
  37. /* PRP V1 life check for Duplicate Accept */
  38. #define PRP_TLV_LIFE_CHECK_DA 21
  39. /* HSR Tag.
  40. * As defined in IEC-62439-3:2010, the HSR tag is really { ethertype = 0x88FB,
  41. * path, LSDU_size, sequence Nr }. But we let eth_header() create { h_dest,
  42. * h_source, h_proto = 0x88FB }, and add { path, LSDU_size, sequence Nr,
  43. * encapsulated protocol } instead.
  44. *
  45. * Field names as defined in the IEC:2010 standard for HSR.
  46. */
  47. struct hsr_tag {
  48. __be16 path_and_LSDU_size;
  49. __be16 sequence_nr;
  50. __be16 encap_proto;
  51. } __packed;
  52. #define HSR_PRP_HLEN 6
  53. #define HSR_PRP_V1_SUP_LSDUSIZE 52
  54. /* The helper functions below assumes that 'path' occupies the 4 most
  55. * significant bits of the 16-bit field shared by 'path' and 'LSDU_size' (or
  56. * equivalently, the 4 most significant bits of HSR tag byte 14).
  57. *
  58. * This is unclear in the IEC specification; its definition of MAC addresses
  59. * indicates the spec is written with the least significant bit first (to the
  60. * left). This, however, would mean that the LSDU field would be split in two
  61. * with the path field in-between, which seems strange. I'm guessing the MAC
  62. * address definition is in error.
  63. */
  64. static inline u16 get_hsr_tag_path(struct hsr_tag *ht)
  65. {
  66. return ntohs(ht->path_and_LSDU_size) >> 12;
  67. }
  68. static inline u16 get_hsr_tag_LSDU_size(struct hsr_tag *ht)
  69. {
  70. return ntohs(ht->path_and_LSDU_size) & 0x0FFF;
  71. }
  72. static inline void set_hsr_tag_path(struct hsr_tag *ht, u16 path)
  73. {
  74. ht->path_and_LSDU_size =
  75. htons((ntohs(ht->path_and_LSDU_size) & 0x0FFF) | (path << 12));
  76. }
  77. static inline void set_hsr_tag_LSDU_size(struct hsr_tag *ht, u16 LSDU_size)
  78. {
  79. ht->path_and_LSDU_size = htons(
  80. (ntohs(ht->path_and_LSDU_size) & 0xF000) |
  81. (LSDU_size & 0x0FFF));
  82. }
  83. struct hsr_ethhdr {
  84. struct ethhdr ethhdr;
  85. struct hsr_tag hsr_tag;
  86. } __packed;
  87. struct hsr_vlan_ethhdr {
  88. struct vlan_ethhdr vlanhdr;
  89. struct hsr_tag hsr_tag;
  90. } __packed;
  91. /* HSR/PRP Supervision Frame data types.
  92. * Field names as defined in the IEC:2012 standard for HSR.
  93. */
  94. struct hsr_prp_sup_tag {
  95. __be16 path_and_HSR_Ver;
  96. __be16 sequence_nr;
  97. __u8 HSR_TLV_type;
  98. __u8 HSR_TLV_length;
  99. } __packed;
  100. struct hsr_prp_sup_payload {
  101. unsigned char mac_address_a[ETH_ALEN];
  102. } __packed;
  103. static inline u16 get_hsr_stag_path(struct hsr_prp_sup_tag *hst)
  104. {
  105. return get_hsr_tag_path((struct hsr_tag *)hst);
  106. }
  107. static inline u16 get_hsr_stag_HSR_ver(struct hsr_prp_sup_tag *hst)
  108. {
  109. return get_hsr_tag_LSDU_size((struct hsr_tag *)hst);
  110. }
  111. static inline void set_hsr_stag_path(struct hsr_prp_sup_tag *hst, u16 path)
  112. {
  113. set_hsr_tag_path((struct hsr_tag *)hst, path);
  114. }
  115. static inline void set_hsr_stag_HSR_ver(struct hsr_prp_sup_tag *hst,
  116. u16 HSR_ver)
  117. {
  118. set_hsr_tag_LSDU_size((struct hsr_tag *)hst, HSR_ver);
  119. }
  120. struct hsrv0_ethhdr_sp {
  121. struct ethhdr ethhdr;
  122. struct hsr_prp_sup_tag hsr_sup;
  123. } __packed;
  124. struct hsrv1_ethhdr_sp {
  125. struct ethhdr ethhdr;
  126. struct hsr_tag hsr;
  127. struct hsr_prp_sup_tag hsr_sup;
  128. } __packed;
  129. enum hsr_prp_port_type {
  130. HSR_PRP_PT_NONE = 0, /* Must be 0, used by framereg */
  131. HSR_PRP_PT_SLAVE_A,
  132. HSR_PRP_PT_SLAVE_B,
  133. HSR_PRP_PT_INTERLINK,
  134. HSR_PRP_PT_MASTER,
  135. HSR_PRP_PT_PORTS, /* This must be the last item in the enum */
  136. };
  137. /* PRP Redunancy Control Trailor (RCT).
  138. * As defined in IEC-62439-4:2012, the PRP RCT is really { sequence Nr,
  139. * Lan indentifier (LanId), LSDU_size and PRP_suffix = 0x88FB }.
  140. *
  141. * Field names as defined in the IEC:2012 standard for PRP.
  142. */
  143. struct prp_rct {
  144. __be16 sequence_nr;
  145. __be16 lan_id_and_LSDU_size;
  146. __be16 PRP_suffix;
  147. } __packed;
  148. static inline u16 get_prp_LSDU_size(struct prp_rct *rct)
  149. {
  150. return ntohs(rct->lan_id_and_LSDU_size) & 0x0FFF;
  151. }
  152. static inline void set_prp_lan_id(struct prp_rct *rct, u16 lan_id)
  153. {
  154. rct->lan_id_and_LSDU_size = htons(
  155. (ntohs(rct->lan_id_and_LSDU_size) & 0x0FFF) |
  156. (lan_id << 12));
  157. }
  158. static inline void set_prp_LSDU_size(struct prp_rct *rct, u16 LSDU_size)
  159. {
  160. rct->lan_id_and_LSDU_size = htons(
  161. (ntohs(rct->lan_id_and_LSDU_size) & 0xF000) |
  162. (LSDU_size & 0x0FFF));
  163. }
  164. struct hsr_prp_lre_if_stats {
  165. u32 cnt_tx_a;
  166. u32 cnt_tx_b;
  167. u32 cnt_rx_wrong_lan_a;
  168. u32 cnt_rx_wrong_lan_b;
  169. u32 cnt_rx_a;
  170. u32 cnt_rx_b;
  171. u32 cnt_rx_errors_a;
  172. u32 cnt_rx_errors_b;
  173. u32 cnt_own_rx_a; /* For HSR only */
  174. u32 cnt_own_rx_b; /* For HSR only */
  175. u32 cnt_tx_sup;
  176. };
  177. struct hsr_prp_port {
  178. struct list_head port_list;
  179. struct net_device *dev;
  180. struct hsr_prp_priv *priv;
  181. enum hsr_prp_port_type type;
  182. };
  183. #define HSR 0
  184. #define PRP 1
  185. /* PRP duplicate discard modes */
  186. #define IEC62439_3_PRP_DA 1
  187. #define IEC62439_3_PRP_DD 2
  188. #define IEC62439_3_HSR_MODE_H 1
  189. #define IEC62439_3_HSR_MODE_N 2
  190. #define IEC62439_3_HSR_MODE_T 3
  191. #define IEC62439_3_HSR_MODE_U 4
  192. #define IEC62439_3_HSR_MODE_M 5
  193. struct hsr_prp_priv {
  194. struct rcu_head rcu_head;
  195. struct list_head ports;
  196. struct list_head node_db; /* Known HSR nodes */
  197. struct list_head self_node_db; /* MACs of slaves */
  198. struct timer_list announce_timer; /* Supervision frame dispatch */
  199. struct timer_list prune_timer;
  200. bool rx_offloaded; /* lre handle in hw */
  201. bool l2_fwd_offloaded; /* L2 forward in hw */
  202. struct hsr_prp_lre_if_stats stats; /* lre interface stats */
  203. int announce_count;
  204. u16 sequence_nr;
  205. u16 sup_sequence_nr; /* For HSRv1 separate seq_nr for supervision */
  206. #define HSR_V0 0
  207. #define HSR_V1 1
  208. #define PRP_V1 2
  209. u8 prot_version; /* Indicate if HSRv0 or HSRv1 or PRPv1 */
  210. #define PRP_LAN_ID 0x5 /* 0x1010 for A and 0x1011 for B. Bit 0 is set
  211. * based on SLAVE_A or SLAVE_B
  212. */
  213. u8 net_id; /* for PRP, it occupies most significant 3 bits
  214. * of lan_id
  215. */
  216. u8 hsr_mode; /* value of hsr mode */
  217. u8 dup_discard_mode; /* Duplicate Discard mode for PRP */
  218. spinlock_t seqnr_lock; /* locking for sequence_nr */
  219. unsigned char sup_multicast_addr[ETH_ALEN];
  220. #ifdef CONFIG_DEBUG_FS
  221. struct dentry *root_dir;
  222. struct dentry *node_tbl_file;
  223. struct dentry *stats_file;
  224. struct dentry *hsr_mode_file;
  225. struct dentry *dd_mode_file;
  226. #endif
  227. };
  228. #define hsr_prp_for_each_port(hsr_prp, port) \
  229. list_for_each_entry_rcu((port), &(hsr_prp)->ports, port_list)
  230. struct hsr_prp_port *hsr_prp_get_port(struct hsr_prp_priv *hsr_prp,
  231. enum hsr_prp_port_type pt);
  232. int hsr_prp_netdev_notify(struct notifier_block *nb, unsigned long event,
  233. void *ptr);
  234. /* Caller must ensure skb is a valid HSR frame */
  235. static inline u16 hsr_get_skb_sequence_nr(struct sk_buff *skb)
  236. {
  237. struct hsr_ethhdr *hsr_ethhdr;
  238. hsr_ethhdr = (struct hsr_ethhdr *)skb_mac_header(skb);
  239. return ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
  240. }
  241. static inline struct prp_rct *skb_get_PRP_rct(struct sk_buff *skb)
  242. {
  243. unsigned char *tail = skb_tail_pointer(skb) - HSR_PRP_HLEN;
  244. struct prp_rct *rct = (struct prp_rct *)tail;
  245. if (rct->PRP_suffix == htons(ETH_P_PRP))
  246. return rct;
  247. return NULL;
  248. }
  249. /* Assume caller has confirmed this skb is PRP suffixed */
  250. static inline u16 prp_get_skb_sequence_nr(struct prp_rct *rct)
  251. {
  252. return ntohs(rct->sequence_nr);
  253. }
  254. static inline u16 get_prp_lan_id(struct prp_rct *rct)
  255. {
  256. return ntohs(rct->lan_id_and_LSDU_size) >> 12;
  257. }
  258. /* assume there is a valid rct */
  259. static inline bool prp_check_lsdu_size(struct sk_buff *skb,
  260. struct prp_rct *rct,
  261. bool is_sup)
  262. {
  263. struct ethhdr *ethhdr;
  264. int expected_lsdu_size;
  265. if (is_sup) {
  266. expected_lsdu_size = HSR_PRP_V1_SUP_LSDUSIZE;
  267. } else {
  268. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  269. expected_lsdu_size = skb->len - 14;
  270. if (ethhdr->h_proto == htons(ETH_P_8021Q))
  271. expected_lsdu_size -= 4;
  272. }
  273. return (expected_lsdu_size == get_prp_LSDU_size(rct));
  274. }
  275. int hsr_prp_register_notifier(u8 proto);
  276. void hsr_prp_unregister_notifier(u8 proto);
  277. #define INC_CNT_TX(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  278. priv->stats.cnt_tx_a++ : priv->stats.cnt_tx_b++)
  279. #define INC_CNT_RX_WRONG_LAN(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  280. priv->stats.cnt_rx_wrong_lan_a++ : \
  281. priv->stats.cnt_rx_wrong_lan_b++)
  282. #define INC_CNT_RX(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  283. priv->stats.cnt_rx_a++ : priv->stats.cnt_rx_b++)
  284. #define INC_CNT_RX_ERROR(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  285. priv->stats.cnt_rx_errors_a++ : priv->stats.cnt_rx_errors_b++)
  286. #define INC_CNT_OWN_RX(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  287. priv->stats.cnt_own_rx_a++ : priv->stats.cnt_own_rx_b++)
  288. #define INC_CNT_TX_SUP(priv) ((priv)->stats.cnt_tx_sup++)
  289. #if IS_ENABLED(CONFIG_DEBUG_FS)
  290. int hsr_prp_debugfs_init(struct hsr_prp_priv *priv,
  291. struct net_device *hsr_prp_dev);
  292. void hsr_prp_debugfs_term(struct hsr_prp_priv *priv);
  293. #else
  294. static inline int hsr_prp_debugfs_init(struct hsr_prp_priv *priv,
  295. struct net_device *hsr_prp_dev)
  296. {
  297. return 0;
  298. }
  299. static inline void hsr_prp_debugfs_term(struct hsr_prp_priv *priv)
  300. {}
  301. #endif
  302. #endif /* __HSR_PRP_PRIVATE_H */