if_laggproto.h revision 1.8
1/*	$NetBSD: if_laggproto.h,v 1.8 2021/10/12 08:30:58 yamaguchi Exp $	*/
2
3/*
4 * Copyright (c) 2021 Internet Initiative Japan Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef	_NET_LAGG_IF_LAGGPROTO_H_
30#define _NET_LAGG_IF_LAGGPROTO_H_
31
32struct lagg_softc;
33struct lagg_proto_softc;
34
35#define LAGG_MAX_PORTS	32
36#define LAGG_PORT_PRIO	0x8000U
37
38enum lagg_work_state {
39	LAGG_WORK_IDLE,
40	LAGG_WORK_ENQUEUED,
41	LAGG_WORK_STOPPING
42};
43struct lagg_work {
44	struct work	 lw_cookie;
45	void		(*lw_func)(struct lagg_work *, void *);
46	void		*lw_arg;
47	int		 lw_state;
48};
49
50static inline void
51lagg_work_set(struct lagg_work *w,
52    void (*func)(struct lagg_work *, void *), void *arg)
53{
54
55	w->lw_func = func;
56	w->lw_arg = arg;
57}
58
59struct workqueue *
60		lagg_workq_create(const char *, pri_t, int, int);
61void		lagg_workq_destroy(struct workqueue *);
62void		lagg_workq_add(struct workqueue *, struct lagg_work *);
63void		lagg_workq_wait(struct workqueue *, struct lagg_work *);
64
65struct lagg_port {
66	struct psref_target	 lp_psref;
67	struct ifnet		*lp_ifp;	/* physical interface */
68	struct lagg_softc	*lp_softc;	/* parent lagg */
69	void			*lp_proto_ctx;
70	bool			 lp_ifdetaching;
71	void			*lp_linkstate_hook;
72	void			*lp_ifdetach_hook;
73
74	uint32_t		 lp_prio;	/* port priority */
75	uint32_t		 lp_flags;	/* port flags */
76
77	u_char			 lp_iftype;
78	uint8_t			 lp_lladdr[ETHER_ADDR_LEN];
79	unsigned short		 lp_ifflags;
80	int			 lp_eccapenable;
81	uint64_t		 lp_ifcapenable;
82	uint64_t		 lp_mtu;
83
84	int			(*lp_ioctl)(struct ifnet *, u_long, void *);
85	int			(*lp_output)(struct ifnet *, struct mbuf *,
86				    const struct sockaddr *,
87				    const struct rtentry *);
88
89	SIMPLEQ_ENTRY(lagg_port)
90				 lp_entry;
91};
92
93struct lagg_proto {
94	lagg_proto	 pr_num;
95	void		(*pr_init)(void);
96	void		(*pr_fini)(void);
97	int		(*pr_attach)(struct lagg_softc *,
98			    struct lagg_proto_softc **);
99	void		(*pr_detach)(struct lagg_proto_softc *);
100	int		(*pr_up)(struct lagg_proto_softc *);
101	void		(*pr_down)(struct lagg_proto_softc *);
102	int		(*pr_transmit)(struct lagg_proto_softc *,
103			    struct mbuf *);
104	struct mbuf *	(*pr_input)(struct lagg_proto_softc *,
105			    struct lagg_port *, struct mbuf *);
106	int		(*pr_allocport)(struct lagg_proto_softc *,
107			    struct lagg_port *);
108	void		(*pr_freeport)(struct lagg_proto_softc *,
109			    struct lagg_port *);
110	void		(*pr_startport)(struct lagg_proto_softc *,
111			    struct lagg_port *);
112	void		(*pr_stopport)(struct lagg_proto_softc *,
113			    struct lagg_port *);
114	void		(*pr_protostat)(struct lagg_proto_softc *,
115			    struct laggreqproto *);
116	void		(*pr_portstat)(struct lagg_proto_softc *,
117			    struct lagg_port *, struct laggreqport *);
118	void		(*pr_linkstate)(struct lagg_proto_softc *,
119			    struct lagg_port *);
120	int		(*pr_ioctl)(struct lagg_proto_softc *,
121			    struct laggreqproto *);
122};
123
124struct lagg_variant {
125	lagg_proto	 lv_proto;
126	struct lagg_proto_softc
127			*lv_psc;
128
129	struct psref_target	lv_psref;
130};
131
132struct lagg_mc_entry {
133	LIST_ENTRY(lagg_mc_entry)
134				 mc_entry;
135	struct ether_multi	*mc_enm;
136	struct sockaddr_storage	 mc_addr;
137};
138
139struct lagg_vlantag {
140	uint16_t	 lvt_vtag;
141	TAILQ_ENTRY(lagg_vlantag)
142			 lvt_entry;
143};
144
145struct lagg_softc {
146	kmutex_t		 sc_lock;
147	struct ifmedia		 sc_media;
148	u_char			 sc_iftype;
149
150	/* interface link-layer address */
151	uint8_t			 sc_lladdr[ETHER_ADDR_LEN];
152	/* generated random lladdr */
153	uint8_t			 sc_lladdr_rand[ETHER_ADDR_LEN];
154
155	LIST_HEAD(, lagg_mc_entry)
156				 sc_mclist;
157	TAILQ_HEAD(, lagg_vlantag)
158				 sc_vtags;
159	pserialize_t		 sc_psz;
160	struct lagg_variant	*sc_var;
161	SIMPLEQ_HEAD(, lagg_port)
162				 sc_ports;
163	size_t			 sc_nports;
164	char			 sc_evgroup[16];
165	struct evcnt		 sc_novar;
166
167	struct sysctllog	*sc_sysctllog;
168	const struct sysctlnode	*sc_sysctlnode;
169	bool			 sc_hash_mac;
170	bool			 sc_hash_ipaddr;
171	bool			 sc_hash_ip6addr;
172	bool			 sc_hash_tcp;
173	bool			 sc_hash_udp;
174
175	/*
176	 * storage size of sc_if is a variable-length,
177	 * should be the last
178	 */
179	struct ifnet		 sc_if;
180};
181
182/*
183 * Locking notes:
184 * - sc_lock(LAGG_LOCK()) is an adaptive mutex and protects items
185 *   of struct lagg_softc
186 * - a lock in struct lagg_proto_softc, for example LACP_LOCK(), is
187 *   an adaptive mutex and protects member contained in the struct
188 * - sc_var is protected by both pselialize (sc_psz) and psref (lv_psref)
189 *    - Updates of sc_var is serialized by sc_lock
190 * - Items in sc_ports is protected by both psref (lp_psref) and
191 *   pserialize contained in struct lagg_proto_softc
192 *   - details are discribed in if_laggport.c and if_lagg_lacp.c
193 *   - Updates of items in sc_ports are serialized by sc_lock
194 * - an instance referenced by lp_proto_ctx in struct lagg_port is
195 *   protected by a lock in struct lagg_proto_softc
196 *
197 * Locking order:
198 * - IFNET_LOCK(sc_if) -> LAGG_LOCK -> ETHER_LOCK(sc_if) -> a lock in
199 *   struct lagg_port_softc
200 * - IFNET_LOCK(sc_if) -> LAGG_LOCK -> IFNET_LOCK(lp_ifp)
201 * - Currently, there is no combination of following locks
202 *   - IFNET_LOCK(lp_ifp) and a lock in struct lagg_proto_softc
203 *   - IFNET_LOCK(lp_ifp) and ETHER_LOCK(sc_if)
204 */
205#define LAGG_LOCK(_sc)		mutex_enter(&(_sc)->sc_lock)
206#define LAGG_UNLOCK(_sc)	mutex_exit(&(_sc)->sc_lock)
207#define LAGG_LOCKED(_sc)	mutex_owned(&(_sc)->sc_lock)
208#define LAGG_CLLADDR(_sc)	CLLADDR((_sc)->sc_if.if_sadl)
209
210#define	LAGG_PORTS_FOREACH(_sc, _lp)	\
211    SIMPLEQ_FOREACH((_lp), &(_sc)->sc_ports, lp_entry)
212#define	LAGG_PORTS_FIRST(_sc)	SIMPLEQ_FIRST(&(_sc)->sc_ports)
213#define LAGG_PORTS_EMPTY(_sc)	SIMPLEQ_EMPTY(&(_sc)->sc_ports)
214#define LAGG_PORT_IOCTL(_lp, _cmd, _data)	\
215	(_lp)->lp_ioctl == NULL ? ENOTTY :	\
216	(_lp)->lp_ioctl((_lp)->lp_ifp, (_cmd), (_data))
217
218
219static inline const void *
220lagg_m_extract(struct mbuf *m, size_t off, size_t reqlen, void *buf)
221{
222	ssize_t len;
223	const void *rv;
224
225	KASSERT(ISSET(m->m_flags, M_PKTHDR));
226	len = off + reqlen;
227
228	if (m->m_pkthdr.len < len) {
229		return NULL;
230	}
231
232	if (m->m_len >= len) {
233		rv = mtod(m, uint8_t *) + off;
234	} else {
235		m_copydata(m, off, reqlen, buf);
236		rv = buf;
237	}
238
239	return rv;
240}
241
242static inline int
243lagg_port_xmit(struct lagg_port *lp, struct mbuf *m)
244{
245
246	return if_transmit_lock(lp->lp_ifp, m);
247}
248
249static inline bool
250lagg_portactive(struct lagg_port *lp)
251{
252	struct ifnet *ifp;
253
254	ifp = lp->lp_ifp;
255
256	if (ifp->if_link_state != LINK_STATE_DOWN &&
257	    ISSET(ifp->if_flags, IFF_UP)) {
258		return true;
259	}
260
261	return false;
262}
263
264void		lagg_log(struct lagg_softc *, int, const char *, ...)
265		    __printflike(3, 4);
266void		lagg_port_getref(struct lagg_port *, struct psref *);
267void		lagg_port_putref(struct lagg_port *, struct psref *);
268void		lagg_enqueue(struct lagg_softc *,
269		    struct lagg_port *, struct mbuf *);
270uint32_t	lagg_hashmbuf(struct lagg_softc *, struct mbuf *);
271
272void		lagg_common_detach(struct lagg_proto_softc *);
273int		lagg_common_allocport(struct lagg_proto_softc *,
274		    struct lagg_port *);
275void		lagg_common_freeport(struct lagg_proto_softc *,
276		    struct lagg_port *);
277void		lagg_common_startport(struct lagg_proto_softc *,
278		    struct lagg_port *);
279void		lagg_common_stopport(struct lagg_proto_softc *,
280		    struct lagg_port *);
281void		lagg_common_linkstate(struct lagg_proto_softc *,
282		    struct lagg_port *);
283
284int		lagg_none_attach(struct lagg_softc *,
285		    struct lagg_proto_softc **);
286int		lagg_none_up(struct lagg_proto_softc *);
287
288int		lagg_fail_attach(struct lagg_softc *,
289		    struct lagg_proto_softc **);
290int		lagg_fail_transmit(struct lagg_proto_softc *, struct mbuf *);
291struct mbuf *	lagg_fail_input(struct lagg_proto_softc *, struct lagg_port *,
292		   struct mbuf *);
293void		lagg_fail_portstat(struct lagg_proto_softc *,
294		    struct lagg_port *, struct laggreqport *);
295int		lagg_fail_ioctl(struct lagg_proto_softc *,
296		    struct laggreqproto *);
297
298int		lagg_lb_attach(struct lagg_softc *, struct lagg_proto_softc **);
299void		lagg_lb_startport(struct lagg_proto_softc *,
300		    struct lagg_port *);
301void		lagg_lb_stopport(struct lagg_proto_softc *, struct lagg_port *);
302int		lagg_lb_transmit(struct lagg_proto_softc *, struct mbuf *);
303struct mbuf *	lagg_lb_input(struct lagg_proto_softc *, struct lagg_port *,
304		    struct mbuf *);
305void		lagg_lb_portstat(struct lagg_proto_softc *,
306		    struct lagg_port *, struct laggreqport *);
307
308int		lacp_attach(struct lagg_softc *, struct lagg_proto_softc **);
309void		lacp_detach(struct lagg_proto_softc *);
310int		lacp_up(struct lagg_proto_softc *);
311void		lacp_down(struct lagg_proto_softc *);
312int		lacp_transmit(struct lagg_proto_softc *, struct mbuf *);
313struct mbuf *	lacp_input(struct lagg_proto_softc *, struct lagg_port *,
314		    struct mbuf *);
315int		lacp_allocport(struct lagg_proto_softc *, struct lagg_port *);
316void		lacp_freeport(struct lagg_proto_softc *, struct lagg_port *);
317void		lacp_startport(struct lagg_proto_softc *, struct lagg_port *);
318void		lacp_stopport(struct lagg_proto_softc *, struct lagg_port *);
319void		lacp_protostat(struct lagg_proto_softc *,
320		    struct laggreqproto *);
321void		lacp_portstat(struct lagg_proto_softc *, struct lagg_port *,
322		    struct laggreqport *);
323void		lacp_linkstate(struct lagg_proto_softc *, struct lagg_port *);
324int		lacp_ioctl(struct lagg_proto_softc *, struct laggreqproto *);
325#endif
326