Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /* Copyright 2011-2014 Autronica Fire and Security AS
0003  *
0004  * Author(s):
0005  *  2011-2014 Arvid Brodin, arvid.brodin@alten.se
0006  *
0007  * include file for HSR and PRP.
0008  */
0009 
0010 #ifndef __HSR_PRIVATE_H
0011 #define __HSR_PRIVATE_H
0012 
0013 #include <linux/netdevice.h>
0014 #include <linux/list.h>
0015 #include <linux/if_vlan.h>
0016 #include <linux/if_hsr.h>
0017 
0018 /* Time constants as specified in the HSR specification (IEC-62439-3 2010)
0019  * Table 8.
0020  * All values in milliseconds.
0021  */
0022 #define HSR_LIFE_CHECK_INTERVAL      2000 /* ms */
0023 #define HSR_NODE_FORGET_TIME        60000 /* ms */
0024 #define HSR_ANNOUNCE_INTERVAL         100 /* ms */
0025 #define HSR_ENTRY_FORGET_TIME         400 /* ms */
0026 
0027 /* By how much may slave1 and slave2 timestamps of latest received frame from
0028  * each node differ before we notify of communication problem?
0029  */
0030 #define MAX_SLAVE_DIFF           3000 /* ms */
0031 #define HSR_SEQNR_START         (USHRT_MAX - 1024)
0032 #define HSR_SUP_SEQNR_START     (HSR_SEQNR_START / 2)
0033 
0034 /* How often shall we check for broken ring and remove node entries older than
0035  * HSR_NODE_FORGET_TIME?
0036  */
0037 #define PRUNE_PERIOD             3000 /* ms */
0038 #define HSR_TLV_EOT                0  /* End of TLVs */
0039 #define HSR_TLV_ANNOUNCE           22
0040 #define HSR_TLV_LIFE_CHECK         23
0041 /* PRP V1 life check for Duplicate discard */
0042 #define PRP_TLV_LIFE_CHECK_DD          20
0043 /* PRP V1 life check for Duplicate Accept */
0044 #define PRP_TLV_LIFE_CHECK_DA          21
0045 /* PRP V1 life redundancy box MAC address */
0046 #define PRP_TLV_REDBOX_MAC         30
0047 
0048 #define HSR_V1_SUP_LSDUSIZE     52
0049 
0050 #define HSR_HSIZE_SHIFT 8
0051 #define HSR_HSIZE   BIT(HSR_HSIZE_SHIFT)
0052 
0053 /* The helper functions below assumes that 'path' occupies the 4 most
0054  * significant bits of the 16-bit field shared by 'path' and 'LSDU_size' (or
0055  * equivalently, the 4 most significant bits of HSR tag byte 14).
0056  *
0057  * This is unclear in the IEC specification; its definition of MAC addresses
0058  * indicates the spec is written with the least significant bit first (to the
0059  * left). This, however, would mean that the LSDU field would be split in two
0060  * with the path field in-between, which seems strange. I'm guessing the MAC
0061  * address definition is in error.
0062  */
0063 
0064 static inline void set_hsr_tag_path(struct hsr_tag *ht, u16 path)
0065 {
0066     ht->path_and_LSDU_size =
0067         htons((ntohs(ht->path_and_LSDU_size) & 0x0FFF) | (path << 12));
0068 }
0069 
0070 static inline void set_hsr_tag_LSDU_size(struct hsr_tag *ht, u16 LSDU_size)
0071 {
0072     ht->path_and_LSDU_size = htons((ntohs(ht->path_and_LSDU_size) &
0073                        0xF000) | (LSDU_size & 0x0FFF));
0074 }
0075 
0076 struct hsr_ethhdr {
0077     struct ethhdr   ethhdr;
0078     struct hsr_tag  hsr_tag;
0079 } __packed;
0080 
0081 struct hsr_vlan_ethhdr {
0082     struct vlan_ethhdr vlanhdr;
0083     struct hsr_tag  hsr_tag;
0084 } __packed;
0085 
0086 struct hsr_sup_tlv {
0087     u8      HSR_TLV_type;
0088     u8      HSR_TLV_length;
0089 };
0090 
0091 /* HSR/PRP Supervision Frame data types.
0092  * Field names as defined in the IEC:2010 standard for HSR.
0093  */
0094 struct hsr_sup_tag {
0095     __be16              path_and_HSR_ver;
0096     __be16              sequence_nr;
0097     struct hsr_sup_tlv  tlv;
0098 } __packed;
0099 
0100 struct hsr_sup_payload {
0101     unsigned char   macaddress_A[ETH_ALEN];
0102 } __packed;
0103 
0104 static inline void set_hsr_stag_path(struct hsr_sup_tag *hst, u16 path)
0105 {
0106     set_hsr_tag_path((struct hsr_tag *)hst, path);
0107 }
0108 
0109 static inline void set_hsr_stag_HSR_ver(struct hsr_sup_tag *hst, u16 HSR_ver)
0110 {
0111     set_hsr_tag_LSDU_size((struct hsr_tag *)hst, HSR_ver);
0112 }
0113 
0114 struct hsrv0_ethhdr_sp {
0115     struct ethhdr       ethhdr;
0116     struct hsr_sup_tag  hsr_sup;
0117 } __packed;
0118 
0119 struct hsrv1_ethhdr_sp {
0120     struct ethhdr       ethhdr;
0121     struct hsr_tag      hsr;
0122     struct hsr_sup_tag  hsr_sup;
0123 } __packed;
0124 
0125 enum hsr_port_type {
0126     HSR_PT_NONE = 0,    /* Must be 0, used by framereg */
0127     HSR_PT_SLAVE_A,
0128     HSR_PT_SLAVE_B,
0129     HSR_PT_INTERLINK,
0130     HSR_PT_MASTER,
0131     HSR_PT_PORTS,   /* This must be the last item in the enum */
0132 };
0133 
0134 /* PRP Redunancy Control Trailor (RCT).
0135  * As defined in IEC-62439-4:2012, the PRP RCT is really { sequence Nr,
0136  * Lan indentifier (LanId), LSDU_size and PRP_suffix = 0x88FB }.
0137  *
0138  * Field names as defined in the IEC:2012 standard for PRP.
0139  */
0140 struct prp_rct {
0141     __be16          sequence_nr;
0142     __be16          lan_id_and_LSDU_size;
0143     __be16          PRP_suffix;
0144 } __packed;
0145 
0146 static inline u16 get_prp_LSDU_size(struct prp_rct *rct)
0147 {
0148     return ntohs(rct->lan_id_and_LSDU_size) & 0x0FFF;
0149 }
0150 
0151 static inline void set_prp_lan_id(struct prp_rct *rct, u16 lan_id)
0152 {
0153     rct->lan_id_and_LSDU_size = htons((ntohs(rct->lan_id_and_LSDU_size) &
0154                       0x0FFF) | (lan_id << 12));
0155 }
0156 static inline void set_prp_LSDU_size(struct prp_rct *rct, u16 LSDU_size)
0157 {
0158     rct->lan_id_and_LSDU_size = htons((ntohs(rct->lan_id_and_LSDU_size) &
0159                       0xF000) | (LSDU_size & 0x0FFF));
0160 }
0161 
0162 struct hsr_port {
0163     struct list_head    port_list;
0164     struct net_device   *dev;
0165     struct hsr_priv     *hsr;
0166     enum hsr_port_type  type;
0167 };
0168 
0169 struct hsr_frame_info;
0170 struct hsr_node;
0171 
0172 struct hsr_proto_ops {
0173     /* format and send supervision frame */
0174     void (*send_sv_frame)(struct hsr_port *port, unsigned long *interval);
0175     void (*handle_san_frame)(bool san, enum hsr_port_type port,
0176                  struct hsr_node *node);
0177     bool (*drop_frame)(struct hsr_frame_info *frame, struct hsr_port *port);
0178     struct sk_buff * (*get_untagged_frame)(struct hsr_frame_info *frame,
0179                            struct hsr_port *port);
0180     struct sk_buff * (*create_tagged_frame)(struct hsr_frame_info *frame,
0181                         struct hsr_port *port);
0182     int (*fill_frame_info)(__be16 proto, struct sk_buff *skb,
0183                    struct hsr_frame_info *frame);
0184     bool (*invalid_dan_ingress_frame)(__be16 protocol);
0185     void (*update_san_info)(struct hsr_node *node, bool is_sup);
0186 };
0187 
0188 struct hsr_priv {
0189     struct rcu_head     rcu_head;
0190     struct list_head    ports;
0191     struct hlist_head   node_db[HSR_HSIZE]; /* Known HSR nodes */
0192     struct hlist_head   self_node_db;   /* MACs of slaves */
0193     struct timer_list   announce_timer; /* Supervision frame dispatch */
0194     struct timer_list   prune_timer;
0195     int announce_count;
0196     u16 sequence_nr;
0197     u16 sup_sequence_nr;    /* For HSRv1 separate seq_nr for supervision */
0198     enum hsr_version prot_version;  /* Indicate if HSRv0, HSRv1 or PRPv1 */
0199     spinlock_t seqnr_lock;  /* locking for sequence_nr */
0200     spinlock_t list_lock;   /* locking for node list */
0201     struct hsr_proto_ops    *proto_ops;
0202     u32 hash_buckets;
0203     u32 hash_seed;
0204 #define PRP_LAN_ID  0x5     /* 0x1010 for A and 0x1011 for B. Bit 0 is set
0205                  * based on SLAVE_A or SLAVE_B
0206                  */
0207     u8 net_id;      /* for PRP, it occupies most significant 3 bits
0208                  * of lan_id
0209                  */
0210     unsigned char       sup_multicast_addr[ETH_ALEN] __aligned(sizeof(u16));
0211                 /* Align to u16 boundary to avoid unaligned access
0212                  * in ether_addr_equal
0213                  */
0214 #ifdef  CONFIG_DEBUG_FS
0215     struct dentry *node_tbl_root;
0216 #endif
0217 };
0218 
0219 #define hsr_for_each_port(hsr, port) \
0220     list_for_each_entry_rcu((port), &(hsr)->ports, port_list)
0221 
0222 struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt);
0223 
0224 /* Caller must ensure skb is a valid HSR frame */
0225 static inline u16 hsr_get_skb_sequence_nr(struct sk_buff *skb)
0226 {
0227     struct hsr_ethhdr *hsr_ethhdr;
0228 
0229     hsr_ethhdr = (struct hsr_ethhdr *)skb_mac_header(skb);
0230     return ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
0231 }
0232 
0233 static inline struct prp_rct *skb_get_PRP_rct(struct sk_buff *skb)
0234 {
0235     unsigned char *tail = skb_tail_pointer(skb) - HSR_HLEN;
0236 
0237     struct prp_rct *rct = (struct prp_rct *)tail;
0238 
0239     if (rct->PRP_suffix == htons(ETH_P_PRP))
0240         return rct;
0241 
0242     return NULL;
0243 }
0244 
0245 /* Assume caller has confirmed this skb is PRP suffixed */
0246 static inline u16 prp_get_skb_sequence_nr(struct prp_rct *rct)
0247 {
0248     return ntohs(rct->sequence_nr);
0249 }
0250 
0251 /* assume there is a valid rct */
0252 static inline bool prp_check_lsdu_size(struct sk_buff *skb,
0253                        struct prp_rct *rct,
0254                        bool is_sup)
0255 {
0256     struct ethhdr *ethhdr;
0257     int expected_lsdu_size;
0258 
0259     if (is_sup) {
0260         expected_lsdu_size = HSR_V1_SUP_LSDUSIZE;
0261     } else {
0262         ethhdr = (struct ethhdr *)skb_mac_header(skb);
0263         expected_lsdu_size = skb->len - 14;
0264         if (ethhdr->h_proto == htons(ETH_P_8021Q))
0265             expected_lsdu_size -= 4;
0266     }
0267 
0268     return (expected_lsdu_size == get_prp_LSDU_size(rct));
0269 }
0270 
0271 #if IS_ENABLED(CONFIG_DEBUG_FS)
0272 void hsr_debugfs_rename(struct net_device *dev);
0273 void hsr_debugfs_init(struct hsr_priv *priv, struct net_device *hsr_dev);
0274 void hsr_debugfs_term(struct hsr_priv *priv);
0275 void hsr_debugfs_create_root(void);
0276 void hsr_debugfs_remove_root(void);
0277 #else
0278 static inline void hsr_debugfs_rename(struct net_device *dev)
0279 {
0280 }
0281 static inline void hsr_debugfs_init(struct hsr_priv *priv,
0282                     struct net_device *hsr_dev)
0283 {}
0284 static inline void hsr_debugfs_term(struct hsr_priv *priv)
0285 {}
0286 static inline void hsr_debugfs_create_root(void)
0287 {}
0288 static inline void hsr_debugfs_remove_root(void)
0289 {}
0290 #endif
0291 
0292 #endif /*  __HSR_PRIVATE_H */