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