xref: /linux/net/hsr/hsr_framereg.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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  * include file for HSR and PRP.
8  */
9 
10 #ifndef __HSR_FRAMEREG_H
11 #define __HSR_FRAMEREG_H
12 
13 #include "hsr_main.h"
14 
15 struct hsr_node;
16 
17 struct hsr_frame_info {
18 	struct sk_buff *skb_std;
19 	struct sk_buff *skb_hsr;
20 	struct sk_buff *skb_prp;
21 	struct hsr_port *port_rcv;
22 	struct hsr_node *node_src;
23 	u16 sequence_nr;
24 	bool is_supervision;
25 	bool is_proxy_supervision;
26 	bool is_vlan;
27 	bool is_local_dest;
28 	bool is_local_exclusive;
29 	bool is_from_san;
30 	bool dst_in_node_db;
31 	bool dst_in_proxy_node_db;
32 };
33 
34 void hsr_del_self_node(struct hsr_priv *hsr);
35 void hsr_del_nodes(struct list_head *node_db);
36 struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db,
37 			      struct sk_buff *skb, bool is_sup,
38 			      enum hsr_port_type rx_port);
39 void hsr_handle_sup_frame(struct hsr_frame_info *frame);
40 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr);
41 bool hsr_addr_is_redbox(struct hsr_priv *hsr, unsigned char *addr);
42 
43 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb);
44 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
45 			 struct hsr_port *port);
46 
47 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
48 			   u16 sequence_nr);
49 int hsr_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame);
50 
51 void hsr_prune_nodes(struct timer_list *t);
52 void hsr_prune_proxy_nodes(struct timer_list *t);
53 
54 int hsr_create_self_node(struct hsr_priv *hsr,
55 			 const unsigned char addr_a[ETH_ALEN],
56 			 const unsigned char addr_b[ETH_ALEN]);
57 
58 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
59 			unsigned char addr[ETH_ALEN]);
60 
61 int hsr_get_node_data(struct hsr_priv *hsr,
62 		      const unsigned char *addr,
63 		      unsigned char addr_b[ETH_ALEN],
64 		      unsigned int *addr_b_ifindex,
65 		      int *if1_age,
66 		      u16 *if1_seq,
67 		      int *if2_age,
68 		      u16 *if2_seq);
69 
70 void prp_handle_san_frame(bool san, enum hsr_port_type port,
71 			  struct hsr_node *node);
72 void prp_update_san_info(struct hsr_node *node, bool is_sup);
73 
74 bool hsr_is_node_in_db(struct list_head *node_db,
75 		       const unsigned char addr[ETH_ALEN]);
76 
77 int prp_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame);
78 
79 #if IS_ENABLED(CONFIG_KUNIT)
80 struct hsr_seq_block *hsr_get_seq_block(struct hsr_node *node, u16 block_idx);
81 #endif
82 
83 #define HSR_SEQ_BLOCK_SHIFT 7 /* 128 bits */
84 #define HSR_SEQ_BLOCK_SIZE (1 << HSR_SEQ_BLOCK_SHIFT)
85 #define HSR_SEQ_BLOCK_MASK (HSR_SEQ_BLOCK_SIZE - 1)
86 #define HSR_MAX_SEQ_BLOCKS 64
87 
88 #define hsr_seq_block_index(sequence_nr) ((sequence_nr) >> HSR_SEQ_BLOCK_SHIFT)
89 #define hsr_seq_block_bit(sequence_nr) ((sequence_nr) & HSR_SEQ_BLOCK_MASK)
90 
91 struct hsr_seq_block {
92 	unsigned long		time;
93 	u16			block_idx;
94 	/* Should be a flexible array member of what DECLARE_BITMAP() would
95 	 * produce.
96 	 */
97 	unsigned long		seq_nrs[][BITS_TO_LONGS(HSR_SEQ_BLOCK_SIZE)];
98 };
99 
100 struct hsr_node {
101 	struct list_head	mac_list;
102 	/* Protect R/W access seq_blocks */
103 	spinlock_t		seq_out_lock;
104 	unsigned char		macaddress_A[ETH_ALEN];
105 	unsigned char		macaddress_B[ETH_ALEN];
106 	/* Local slave through which AddrB frames are received from this node */
107 	enum hsr_port_type	addr_B_port;
108 	unsigned long		time_in[HSR_PT_PORTS];
109 	bool			time_in_stale[HSR_PT_PORTS];
110 	/* if the node is a SAN */
111 	bool			san_a;
112 	bool			san_b;
113 	bool			removed;
114 	/* Duplicate detection */
115 	struct xarray		seq_blocks;
116 	void			*block_buf;
117 	unsigned int		next_block;
118 	unsigned int		seq_port_cnt;
119 	struct rcu_head		rcu_head;
120 };
121 
hsr_seq_block_size(struct hsr_node * node)122 static inline size_t hsr_seq_block_size(struct hsr_node *node)
123 {
124 	WARN_ON_ONCE(node->seq_port_cnt == 0);
125 	return struct_size_t(struct hsr_seq_block, seq_nrs, node->seq_port_cnt);
126 }
127 
128 #endif /* __HSR_FRAMEREG_H */
129