hsr_prp_main.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. *
  4. * Author(s):
  5. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  6. */
  7. #ifndef __HSR_PRP_MAIN_H
  8. #define __HSR_PRP_MAIN_H
  9. #include <linux/netdevice.h>
  10. #include <linux/list.h>
  11. #include <linux/if_vlan.h>
  12. #include <net/lredev.h>
  13. /* Time constants as specified in the HSR specification (IEC-62439-3 2010)
  14. * Table 8.
  15. * All values in milliseconds.
  16. */
  17. #define HSR_PRP_LIFE_CHECK_INTERVAL 2000 /* ms */
  18. #define HSR_PRP_NODE_FORGET_TIME 60000 /* ms */
  19. #define HSR_PRP_ANNOUNCE_INTERVAL 100 /* ms */
  20. /* By how much may slave1 and slave2 timestamps of latest received frame from
  21. * each node differ before we notify of communication problem?
  22. */
  23. #define HSR_PRP_MAX_SLAVE_DIFF 3000 /* ms */
  24. #define HSR_PRP_SEQNR_START (USHRT_MAX - 1024)
  25. #define HSR_PRP_SUP_SEQNR_START (HSR_PRP_SEQNR_START / 2)
  26. /* How often shall we check for broken ring and remove node entries older than
  27. * HSR_PRP_NODE_FORGET_TIME?
  28. */
  29. #define HSR_PRP_PRUNE_PERIOD 3000 /* ms */
  30. #define HSR_TLV_ANNOUNCE 22
  31. #define HSR_TLV_LIFE_CHECK 23
  32. /* PRP V1 life check for Duplicate discard */
  33. #define PRP_TLV_LIFE_CHECK_DD 20
  34. /* PRP V1 life check for Duplicate Accept */
  35. #define PRP_TLV_LIFE_CHECK_DA 21
  36. /* HSR Tag.
  37. * As defined in IEC-62439-3:2010, the HSR tag is really { ethertype = 0x88FB,
  38. * path, LSDU_size, sequence Nr }. But we let eth_header() create { h_dest,
  39. * h_source, h_proto = 0x88FB }, and add { path, LSDU_size, sequence Nr,
  40. * encapsulated protocol } instead.
  41. *
  42. * Field names as defined in the IEC:2010 standard for HSR.
  43. */
  44. struct hsr_tag {
  45. __be16 path_and_LSDU_size;
  46. __be16 sequence_nr;
  47. __be16 encap_proto;
  48. } __packed;
  49. #define HSR_PRP_HLEN 6
  50. #define HSR_PRP_V1_SUP_LSDUSIZE 52
  51. /* The helper functions below assumes that 'path' occupies the 4 most
  52. * significant bits of the 16-bit field shared by 'path' and 'LSDU_size' (or
  53. * equivalently, the 4 most significant bits of HSR tag byte 14).
  54. *
  55. * This is unclear in the IEC specification; its definition of MAC addresses
  56. * indicates the spec is written with the least significant bit first (to the
  57. * left). This, however, would mean that the LSDU field would be split in two
  58. * with the path field in-between, which seems strange. I'm guessing the MAC
  59. * address definition is in error.
  60. */
  61. static inline u16 get_hsr_tag_path(struct hsr_tag *ht)
  62. {
  63. return ntohs(ht->path_and_LSDU_size) >> 12;
  64. }
  65. static inline u16 get_hsr_tag_LSDU_size(struct hsr_tag *ht)
  66. {
  67. return ntohs(ht->path_and_LSDU_size) & 0x0FFF;
  68. }
  69. static inline void set_hsr_tag_path(struct hsr_tag *ht, u16 path)
  70. {
  71. ht->path_and_LSDU_size =
  72. htons((ntohs(ht->path_and_LSDU_size) & 0x0FFF) | (path << 12));
  73. }
  74. static inline void set_hsr_tag_LSDU_size(struct hsr_tag *ht, u16 LSDU_size)
  75. {
  76. ht->path_and_LSDU_size = htons((ntohs(ht->path_and_LSDU_size) &
  77. 0xF000) | (LSDU_size & 0x0FFF));
  78. }
  79. struct hsr_ethhdr {
  80. struct ethhdr ethhdr;
  81. struct hsr_tag hsr_tag;
  82. } __packed;
  83. struct hsr_vlan_ethhdr {
  84. struct vlan_ethhdr vlanhdr;
  85. struct hsr_tag hsr_tag;
  86. } __packed;
  87. /* HSR/PRP Supervision Frame data types.
  88. * Field names as defined in the IEC:2012 standard for HSR.
  89. */
  90. struct hsr_prp_sup_tag {
  91. __be16 path_and_HSR_ver;
  92. __be16 sequence_nr;
  93. __u8 HSR_TLV_type;
  94. __u8 HSR_TLV_length;
  95. } __packed;
  96. struct hsr_prp_sup_payload {
  97. unsigned char macaddress_A[ETH_ALEN];
  98. } __packed;
  99. static inline u16 get_hsr_stag_path(struct hsr_prp_sup_tag *hst)
  100. {
  101. return get_hsr_tag_path((struct hsr_tag *)hst);
  102. }
  103. static inline u16 get_hsr_stag_HSR_ver(struct hsr_prp_sup_tag *hst)
  104. {
  105. return get_hsr_tag_LSDU_size((struct hsr_tag *)hst);
  106. }
  107. static inline void set_hsr_stag_path(struct hsr_prp_sup_tag *hst, u16 path)
  108. {
  109. set_hsr_tag_path((struct hsr_tag *)hst, path);
  110. }
  111. static inline void set_hsr_stag_HSR_ver(struct hsr_prp_sup_tag *hst,
  112. u16 HSR_ver)
  113. {
  114. set_hsr_tag_LSDU_size((struct hsr_tag *)hst, HSR_ver);
  115. }
  116. struct hsrv0_ethhdr_sp {
  117. struct ethhdr ethhdr;
  118. struct hsr_prp_sup_tag hsr_sup;
  119. } __packed;
  120. struct hsrv0_ethhdr_vlan_sp {
  121. struct ethhdr ethhdr;
  122. struct vlan_hdr vlanHdr;
  123. struct hsr_prp_sup_tag hsr_sup;
  124. } __packed;
  125. struct hsrv1_ethhdr_sp {
  126. struct ethhdr ethhdr;
  127. struct hsr_tag hsr;
  128. struct hsr_prp_sup_tag hsr_sup;
  129. } __packed;
  130. struct hsrv1_ethhdr_vlan_sp {
  131. struct ethhdr ethhdr;
  132. struct vlan_hdr vlanHdr;
  133. struct hsr_tag hsr;
  134. struct hsr_prp_sup_tag hsr_sup;
  135. } __packed;
  136. enum hsr_prp_port_type {
  137. HSR_PRP_PT_NONE = 0, /* Must be 0, used by framereg */
  138. HSR_PRP_PT_SLAVE_A,
  139. HSR_PRP_PT_SLAVE_B,
  140. HSR_PRP_PT_INTERLINK,
  141. HSR_PRP_PT_MASTER,
  142. HSR_PRP_PT_PORTS, /* This must be the last item in the enum */
  143. };
  144. /* PRP Redunancy Control Trailor (RCT).
  145. * As defined in IEC-62439-4:2012, the PRP RCT is really { sequence Nr,
  146. * Lan indentifier (LanId), LSDU_size and PRP_suffix = 0x88FB }.
  147. *
  148. * Field names as defined in the IEC:2012 standard for PRP.
  149. */
  150. struct prp_rct {
  151. __be16 sequence_nr;
  152. __be16 lan_id_and_LSDU_size;
  153. __be16 PRP_suffix;
  154. } __packed;
  155. static inline u16 get_prp_LSDU_size(struct prp_rct *rct)
  156. {
  157. return ntohs(rct->lan_id_and_LSDU_size) & 0x0FFF;
  158. }
  159. static inline void set_prp_lan_id(struct prp_rct *rct, u16 lan_id)
  160. {
  161. rct->lan_id_and_LSDU_size = htons((ntohs(rct->lan_id_and_LSDU_size) &
  162. 0x0FFF) | (lan_id << 12));
  163. }
  164. static inline void set_prp_LSDU_size(struct prp_rct *rct, u16 LSDU_size)
  165. {
  166. rct->lan_id_and_LSDU_size = htons(
  167. (ntohs(rct->lan_id_and_LSDU_size) & 0xF000) |
  168. (LSDU_size & 0x0FFF));
  169. }
  170. struct hsr_prp_debug_stats {
  171. u32 cnt_tx_sup;
  172. u32 cnt_rx_sup_a;
  173. u32 cnt_rx_sup_b;
  174. };
  175. struct hsr_prp_port {
  176. struct list_head port_list;
  177. struct net_device *dev;
  178. struct hsr_prp_priv *priv;
  179. enum hsr_prp_port_type type;
  180. };
  181. #define HSR 0
  182. #define PRP 1
  183. struct hsr_prp_priv {
  184. struct rcu_head rcu_head;
  185. struct list_head ports;
  186. struct list_head node_db; /* Known HSR nodes */
  187. struct list_head self_node_db; /* MACs of slaves */
  188. struct timer_list announce_timer; /* Supervision frame dispatch */
  189. struct timer_list prune_timer;
  190. unsigned int rx_offloaded : 1; /* lre handle in hw */
  191. unsigned int l2_fwd_offloaded : 1; /* L2 forward in hw */
  192. struct hsr_prp_debug_stats dbg_stats; /* debug stats */
  193. struct lre_stats lre_stats; /* lre interface stats */
  194. int announce_count;
  195. u16 sequence_nr;
  196. u16 sup_sequence_nr; /* For HSRv1 separate seq_nr for supervision */
  197. u8 prot_version; /* Indicate if HSRv0 or HSRv1. */
  198. spinlock_t seqnr_lock; /* locking for sequence_nr */
  199. #define HSR_V0 0
  200. #define HSR_V1 1
  201. #define PRP_V1 2
  202. #define PRP_LAN_ID 0x5 /* 0x1010 for A and 0x1011 for B. Bit 0 is set
  203. * based on SLAVE_A or SLAVE_B
  204. */
  205. u8 net_id; /* for PRP, it occupies most significant 3 bits
  206. * of lan_id
  207. */
  208. /* Below are used when SV frames are to be sent with VLAN tag */
  209. u8 use_vlan_for_sv;
  210. u16 sv_frame_vid;
  211. u8 sv_frame_dei;
  212. u8 sv_frame_pcp;
  213. /* To enable/disable SV frame transmission */
  214. u8 disable_sv_frame;
  215. /* value of hsr mode */
  216. enum iec62439_3_hsr_modes hsr_mode;
  217. /* PRP Transparent Reception */
  218. enum iec62439_3_tr_modes prp_tr;
  219. /* Duplicate discard mode */
  220. enum iec62439_3_dd_modes dd_mode;
  221. /* Clear Node Table command */
  222. enum iec62439_3_clear_nt_cmd clear_nt_cmd;
  223. u32 dlrmt; /* duplicate list reside max time */
  224. unsigned char sup_multicast_addr[ETH_ALEN];
  225. #ifdef CONFIG_DEBUG_FS
  226. struct dentry *root_dir;
  227. struct dentry *lre_info_file;
  228. #endif
  229. #ifdef CONFIG_PROC_FS
  230. struct proc_dir_entry *dir;
  231. struct proc_dir_entry *hsr_mode_file;
  232. struct proc_dir_entry *dd_mode_file;
  233. struct proc_dir_entry *prp_tr_file;
  234. struct proc_dir_entry *clear_nt_file;
  235. struct proc_dir_entry *dlrmt_file;
  236. struct proc_dir_entry *lre_stats_file;
  237. struct proc_dir_entry *node_table_file;
  238. struct proc_dir_entry *disable_sv_file;
  239. #endif
  240. };
  241. #define hsr_prp_for_each_port(priv, port) \
  242. list_for_each_entry_rcu((port), &(priv)->ports, port_list)
  243. struct hsr_prp_port *hsr_prp_get_port(struct hsr_prp_priv *priv,
  244. enum hsr_prp_port_type pt);
  245. /* Caller must ensure skb is a valid HSR frame */
  246. static inline u16 hsr_get_skb_sequence_nr(struct sk_buff *skb)
  247. {
  248. struct hsr_ethhdr *hsr_ethhdr;
  249. hsr_ethhdr = (struct hsr_ethhdr *)skb_mac_header(skb);
  250. return ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
  251. }
  252. static inline struct prp_rct *skb_get_PRP_rct(struct sk_buff *skb)
  253. {
  254. unsigned char *tail = skb_tail_pointer(skb) - HSR_PRP_HLEN;
  255. struct prp_rct *rct = (struct prp_rct *)tail;
  256. if (rct->PRP_suffix == htons(ETH_P_PRP))
  257. return rct;
  258. return NULL;
  259. }
  260. /* Assume caller has confirmed this skb is PRP suffixed */
  261. static inline u16 prp_get_skb_sequence_nr(struct prp_rct *rct)
  262. {
  263. return ntohs(rct->sequence_nr);
  264. }
  265. static inline u16 get_prp_lan_id(struct prp_rct *rct)
  266. {
  267. return ntohs(rct->lan_id_and_LSDU_size) >> 12;
  268. }
  269. /* assume there is a valid rct */
  270. static inline bool prp_check_lsdu_size(struct sk_buff *skb,
  271. struct prp_rct *rct,
  272. bool is_sup)
  273. {
  274. struct ethhdr *ethhdr;
  275. int expected_lsdu_size;
  276. if (is_sup) {
  277. expected_lsdu_size = HSR_PRP_V1_SUP_LSDUSIZE;
  278. } else {
  279. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  280. expected_lsdu_size = skb->len - 14;
  281. if (ethhdr->h_proto == htons(ETH_P_8021Q))
  282. expected_lsdu_size -= 4;
  283. }
  284. return (expected_lsdu_size == get_prp_LSDU_size(rct));
  285. }
  286. int hsr_prp_register_notifier(u8 proto);
  287. void hsr_prp_unregister_notifier(u8 proto);
  288. #define INC_CNT_TX_AB(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  289. (priv)->lre_stats.cnt_tx_a++ : \
  290. (priv)->lre_stats.cnt_tx_b++)
  291. #define INC_CNT_TX_C(priv) ((priv)->lre_stats.cnt_tx_c++)
  292. #define INC_CNT_RX_WRONG_LAN_AB(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  293. (priv)->lre_stats.cnt_errwronglan_a++ : \
  294. (priv)->lre_stats.cnt_errwronglan_b++)
  295. #define INC_CNT_RX_AB(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  296. (priv)->lre_stats.cnt_rx_a++ : \
  297. (priv)->lre_stats.cnt_rx_b++)
  298. #define INC_CNT_RX_C(priv) ((priv)->lre_stats.cnt_rx_c++)
  299. #define INC_CNT_RX_ERROR_AB(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  300. (priv)->lre_stats.cnt_errors_a++ : \
  301. (priv)->lre_stats.cnt_errors_b++)
  302. #define INC_CNT_OWN_RX_AB(type, priv) (((type) == HSR_PRP_PT_SLAVE_A) ? \
  303. (priv)->lre_stats.cnt_own_rx_a++ : \
  304. (priv)->lre_stats.cnt_own_rx_b++)
  305. #define INC_CNT_TX_SUP(priv) ((priv)->dbg_stats.cnt_tx_sup++)
  306. #define INC_CNT_RX_SUP_A(priv) ((priv)->dbg_stats.cnt_rx_sup_a++)
  307. #define INC_CNT_RX_SUP_B(priv) ((priv)->dbg_stats.cnt_rx_sup_b++)
  308. #if IS_ENABLED(CONFIG_DEBUG_FS)
  309. int hsr_prp_debugfs_init(struct hsr_prp_priv *priv, struct net_device *ndev);
  310. void hsr_prp_debugfs_term(struct hsr_prp_priv *priv);
  311. #else
  312. static inline int hsr_prp_debugfs_init(struct hsr_prp_priv *priv,
  313. struct net_device *ndev)
  314. {
  315. return 0;
  316. }
  317. static inline void hsr_prp_debugfs_term(struct hsr_prp_priv *priv)
  318. {}
  319. #endif
  320. #ifdef CONFIG_PROC_FS
  321. int hsr_prp_create_procfs(struct hsr_prp_priv *priv, struct net_device *ndev);
  322. void hsr_prp_remove_procfs(struct hsr_prp_priv *priv, struct net_device *ndev);
  323. #else
  324. static inline int hsr_prp_create_procfs(struct hsr_prp_priv *priv,
  325. struct net_device *ndev)
  326. {
  327. return 0;
  328. }
  329. static inline void hsr_prp_remove_procfs(struct hsr_prp_priv *priv,
  330. struct net_device *ndev)
  331. {}
  332. #endif
  333. int hsr_prp_lredev_attr_set(struct hsr_prp_priv *priv,
  334. struct lredev_attr *attr);
  335. int hsr_prp_lredev_attr_get(struct hsr_prp_priv *priv,
  336. struct lredev_attr *attr);
  337. int hsr_prp_lredev_get_node_table(struct hsr_prp_priv *priv,
  338. struct lre_node_table_entry table[],
  339. int size);
  340. int hsr_prp_lredev_get_lre_stats(struct hsr_prp_priv *priv,
  341. struct lre_stats *stats);
  342. #endif /* __HSR_PRP_MAIN_H */