hsr_main.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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, [email protected]
  6. *
  7. * include file for HSR and PRP.
  8. */
  9. #ifndef __HSR_PRIVATE_H
  10. #define __HSR_PRIVATE_H
  11. #include <linux/netdevice.h>
  12. #include <linux/list.h>
  13. #include <linux/if_vlan.h>
  14. #include <linux/if_hsr.h>
  15. /* Time constants as specified in the HSR specification (IEC-62439-3 2010)
  16. * Table 8.
  17. * All values in milliseconds.
  18. */
  19. #define HSR_LIFE_CHECK_INTERVAL 2000 /* ms */
  20. #define HSR_NODE_FORGET_TIME 60000 /* ms */
  21. #define HSR_ANNOUNCE_INTERVAL 100 /* ms */
  22. #define HSR_ENTRY_FORGET_TIME 400 /* 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 MAX_SLAVE_DIFF 3000 /* ms */
  27. #define HSR_SEQNR_START (USHRT_MAX - 1024)
  28. #define HSR_SUP_SEQNR_START (HSR_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 PRUNE_PERIOD 3000 /* ms */
  33. #define HSR_TLV_EOT 0 /* End of TLVs */
  34. #define HSR_TLV_ANNOUNCE 22
  35. #define HSR_TLV_LIFE_CHECK 23
  36. /* PRP V1 life check for Duplicate discard */
  37. #define PRP_TLV_LIFE_CHECK_DD 20
  38. /* PRP V1 life check for Duplicate Accept */
  39. #define PRP_TLV_LIFE_CHECK_DA 21
  40. /* PRP V1 life redundancy box MAC address */
  41. #define PRP_TLV_REDBOX_MAC 30
  42. #define HSR_V1_SUP_LSDUSIZE 52
  43. /* The helper functions below assumes that 'path' occupies the 4 most
  44. * significant bits of the 16-bit field shared by 'path' and 'LSDU_size' (or
  45. * equivalently, the 4 most significant bits of HSR tag byte 14).
  46. *
  47. * This is unclear in the IEC specification; its definition of MAC addresses
  48. * indicates the spec is written with the least significant bit first (to the
  49. * left). This, however, would mean that the LSDU field would be split in two
  50. * with the path field in-between, which seems strange. I'm guessing the MAC
  51. * address definition is in error.
  52. */
  53. static inline void set_hsr_tag_path(struct hsr_tag *ht, u16 path)
  54. {
  55. ht->path_and_LSDU_size =
  56. htons((ntohs(ht->path_and_LSDU_size) & 0x0FFF) | (path << 12));
  57. }
  58. static inline void set_hsr_tag_LSDU_size(struct hsr_tag *ht, u16 LSDU_size)
  59. {
  60. ht->path_and_LSDU_size = htons((ntohs(ht->path_and_LSDU_size) &
  61. 0xF000) | (LSDU_size & 0x0FFF));
  62. }
  63. struct hsr_ethhdr {
  64. struct ethhdr ethhdr;
  65. struct hsr_tag hsr_tag;
  66. } __packed;
  67. struct hsr_vlan_ethhdr {
  68. struct vlan_ethhdr vlanhdr;
  69. struct hsr_tag hsr_tag;
  70. } __packed;
  71. struct hsr_sup_tlv {
  72. u8 HSR_TLV_type;
  73. u8 HSR_TLV_length;
  74. } __packed;
  75. /* HSR/PRP Supervision Frame data types.
  76. * Field names as defined in the IEC:2010 standard for HSR.
  77. */
  78. struct hsr_sup_tag {
  79. __be16 path_and_HSR_ver;
  80. __be16 sequence_nr;
  81. struct hsr_sup_tlv tlv;
  82. } __packed;
  83. struct hsr_sup_payload {
  84. unsigned char macaddress_A[ETH_ALEN];
  85. } __packed;
  86. static inline void set_hsr_stag_path(struct hsr_sup_tag *hst, u16 path)
  87. {
  88. set_hsr_tag_path((struct hsr_tag *)hst, path);
  89. }
  90. static inline void set_hsr_stag_HSR_ver(struct hsr_sup_tag *hst, u16 HSR_ver)
  91. {
  92. set_hsr_tag_LSDU_size((struct hsr_tag *)hst, HSR_ver);
  93. }
  94. struct hsrv0_ethhdr_sp {
  95. struct ethhdr ethhdr;
  96. struct hsr_sup_tag hsr_sup;
  97. } __packed;
  98. struct hsrv1_ethhdr_sp {
  99. struct ethhdr ethhdr;
  100. struct hsr_tag hsr;
  101. struct hsr_sup_tag hsr_sup;
  102. } __packed;
  103. enum hsr_port_type {
  104. HSR_PT_NONE = 0, /* Must be 0, used by framereg */
  105. HSR_PT_SLAVE_A,
  106. HSR_PT_SLAVE_B,
  107. HSR_PT_INTERLINK,
  108. HSR_PT_MASTER,
  109. HSR_PT_PORTS, /* This must be the last item in the enum */
  110. };
  111. /* PRP Redunancy Control Trailor (RCT).
  112. * As defined in IEC-62439-4:2012, the PRP RCT is really { sequence Nr,
  113. * Lan indentifier (LanId), LSDU_size and PRP_suffix = 0x88FB }.
  114. *
  115. * Field names as defined in the IEC:2012 standard for PRP.
  116. */
  117. struct prp_rct {
  118. __be16 sequence_nr;
  119. __be16 lan_id_and_LSDU_size;
  120. __be16 PRP_suffix;
  121. } __packed;
  122. static inline u16 get_prp_LSDU_size(struct prp_rct *rct)
  123. {
  124. return ntohs(rct->lan_id_and_LSDU_size) & 0x0FFF;
  125. }
  126. static inline void set_prp_lan_id(struct prp_rct *rct, u16 lan_id)
  127. {
  128. rct->lan_id_and_LSDU_size = htons((ntohs(rct->lan_id_and_LSDU_size) &
  129. 0x0FFF) | (lan_id << 12));
  130. }
  131. static inline void set_prp_LSDU_size(struct prp_rct *rct, u16 LSDU_size)
  132. {
  133. rct->lan_id_and_LSDU_size = htons((ntohs(rct->lan_id_and_LSDU_size) &
  134. 0xF000) | (LSDU_size & 0x0FFF));
  135. }
  136. struct hsr_port {
  137. struct list_head port_list;
  138. struct net_device *dev;
  139. struct hsr_priv *hsr;
  140. enum hsr_port_type type;
  141. };
  142. struct hsr_frame_info;
  143. struct hsr_node;
  144. struct hsr_proto_ops {
  145. /* format and send supervision frame */
  146. void (*send_sv_frame)(struct hsr_port *port, unsigned long *interval);
  147. void (*handle_san_frame)(bool san, enum hsr_port_type port,
  148. struct hsr_node *node);
  149. bool (*drop_frame)(struct hsr_frame_info *frame, struct hsr_port *port);
  150. struct sk_buff * (*get_untagged_frame)(struct hsr_frame_info *frame,
  151. struct hsr_port *port);
  152. struct sk_buff * (*create_tagged_frame)(struct hsr_frame_info *frame,
  153. struct hsr_port *port);
  154. int (*fill_frame_info)(__be16 proto, struct sk_buff *skb,
  155. struct hsr_frame_info *frame);
  156. bool (*invalid_dan_ingress_frame)(__be16 protocol);
  157. void (*update_san_info)(struct hsr_node *node, bool is_sup);
  158. };
  159. struct hsr_priv {
  160. struct rcu_head rcu_head;
  161. struct list_head ports;
  162. struct list_head node_db; /* Known HSR nodes */
  163. struct list_head self_node_db; /* MACs of slaves */
  164. struct timer_list announce_timer; /* Supervision frame dispatch */
  165. struct timer_list prune_timer;
  166. int announce_count;
  167. u16 sequence_nr;
  168. u16 sup_sequence_nr; /* For HSRv1 separate seq_nr for supervision */
  169. enum hsr_version prot_version; /* Indicate if HSRv0, HSRv1 or PRPv1 */
  170. spinlock_t seqnr_lock; /* locking for sequence_nr */
  171. spinlock_t list_lock; /* locking for node list */
  172. struct hsr_proto_ops *proto_ops;
  173. #define PRP_LAN_ID 0x5 /* 0x1010 for A and 0x1011 for B. Bit 0 is set
  174. * based on SLAVE_A or SLAVE_B
  175. */
  176. u8 net_id; /* for PRP, it occupies most significant 3 bits
  177. * of lan_id
  178. */
  179. unsigned char sup_multicast_addr[ETH_ALEN] __aligned(sizeof(u16));
  180. /* Align to u16 boundary to avoid unaligned access
  181. * in ether_addr_equal
  182. */
  183. #ifdef CONFIG_DEBUG_FS
  184. struct dentry *node_tbl_root;
  185. #endif
  186. };
  187. #define hsr_for_each_port(hsr, port) \
  188. list_for_each_entry_rcu((port), &(hsr)->ports, port_list)
  189. struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt);
  190. /* Caller must ensure skb is a valid HSR frame */
  191. static inline u16 hsr_get_skb_sequence_nr(struct sk_buff *skb)
  192. {
  193. struct hsr_ethhdr *hsr_ethhdr;
  194. hsr_ethhdr = (struct hsr_ethhdr *)skb_mac_header(skb);
  195. return ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
  196. }
  197. static inline struct prp_rct *skb_get_PRP_rct(struct sk_buff *skb)
  198. {
  199. unsigned char *tail = skb_tail_pointer(skb) - HSR_HLEN;
  200. struct prp_rct *rct = (struct prp_rct *)tail;
  201. if (rct->PRP_suffix == htons(ETH_P_PRP))
  202. return rct;
  203. return NULL;
  204. }
  205. /* Assume caller has confirmed this skb is PRP suffixed */
  206. static inline u16 prp_get_skb_sequence_nr(struct prp_rct *rct)
  207. {
  208. return ntohs(rct->sequence_nr);
  209. }
  210. /* assume there is a valid rct */
  211. static inline bool prp_check_lsdu_size(struct sk_buff *skb,
  212. struct prp_rct *rct,
  213. bool is_sup)
  214. {
  215. struct ethhdr *ethhdr;
  216. int expected_lsdu_size;
  217. if (is_sup) {
  218. expected_lsdu_size = HSR_V1_SUP_LSDUSIZE;
  219. } else {
  220. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  221. expected_lsdu_size = skb->len - 14;
  222. if (ethhdr->h_proto == htons(ETH_P_8021Q))
  223. expected_lsdu_size -= 4;
  224. }
  225. return (expected_lsdu_size == get_prp_LSDU_size(rct));
  226. }
  227. #if IS_ENABLED(CONFIG_DEBUG_FS)
  228. void hsr_debugfs_rename(struct net_device *dev);
  229. void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev);
  230. void hsr_debugfs_term(struct hsr_priv *priv);
  231. void hsr_debugfs_create_root(void);
  232. void hsr_debugfs_remove_root(void);
  233. #else
  234. static inline void hsr_debugfs_rename(struct net_device *dev)
  235. {
  236. }
  237. static inline void hsr_debugfs_init(struct hsr_priv *priv,
  238. struct net_device *hsr_dev)
  239. {}
  240. static inline void hsr_debugfs_term(struct hsr_priv *priv)
  241. {}
  242. static inline void hsr_debugfs_create_root(void)
  243. {}
  244. static inline void hsr_debugfs_remove_root(void)
  245. {}
  246. #endif
  247. #endif /* __HSR_PRIVATE_H */