1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
6 *
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
10 */
11
12 /*
13 * Copyright 2016 Joyent, Inc.
14 */
15
16 #ifndef _SYS_OVERLAY_IMPL_H
17 #define _SYS_OVERLAY_IMPL_H
18
19 /*
20 * Overlay device support
21 */
22
23 #include <sys/overlay.h>
24 #include <sys/overlay_common.h>
25 #include <sys/overlay_plugin.h>
26 #include <sys/overlay_target.h>
27 #include <sys/ksynch.h>
28 #include <sys/list.h>
29 #include <sys/avl.h>
30 #include <sys/ksocket.h>
31 #include <sys/socket.h>
32 #include <sys/refhash.h>
33 #include <sys/ethernet.h>
34 #include <sys/list.h>
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 #define OVEP_VERSION_ONE 0x1
41
42 typedef struct overlay_plugin {
43 kmutex_t ovp_mutex;
44 list_node_t ovp_link; /* overlay_plugin_lock */
45 uint_t ovp_active; /* ovp_mutex */
46 const char *ovp_name; /* RO */
47 const overlay_plugin_ops_t *ovp_ops; /* RO */
48 const char *const *ovp_props; /* RO */
49 uint_t ovp_nprops; /* RO */
50 uint_t ovp_id_size; /* RO */
51 overlay_plugin_flags_t ovp_flags; /* RO */
52 overlay_plugin_dest_t ovp_dest; /* RO */
53 } overlay_plugin_t;
54
55 typedef struct overlay_mux {
56 list_node_t omux_lnode;
57 ksocket_t omux_ksock; /* RO */
58 overlay_plugin_t *omux_plugin; /* RO: associated encap */
59 int omux_domain; /* RO: socket domain */
60 int omux_family; /* RO: socket family */
61 int omux_protocol; /* RO: socket protocol */
62 struct sockaddr *omux_addr; /* RO: socket address */
63 socklen_t omux_alen; /* RO: sockaddr len */
64 kmutex_t omux_lock; /* Protects everything below */
65 uint_t omux_count; /* Active instances */
66 avl_tree_t omux_devices; /* Tree of devices */
67 } overlay_mux_t;
68
69 typedef enum overlay_target_flag {
70 OVERLAY_T_TEARDOWN = 0x1
71 } overlay_target_flag_t;
72
73 typedef struct overlay_target {
74 kmutex_t ott_lock;
75 kcondvar_t ott_cond;
76 overlay_target_mode_t ott_mode; /* RO */
77 overlay_plugin_dest_t ott_dest; /* RO */
78 uint64_t ott_id; /* RO */
79 overlay_target_flag_t ott_flags; /* ott_lock */
80 uint_t ott_ocount; /* ott_lock */
81 union { /* ott_lock */
82 overlay_target_point_t ott_point;
83 struct overlay_target_dyn {
84 refhash_t *ott_dhash;
85 avl_tree_t ott_tree;
86 } ott_dyn;
87 } ott_u;
88 } overlay_target_t;
89
90 typedef enum overlay_dev_flag {
91 OVERLAY_F_ACTIVATED = 0x01, /* Activate ioctl completed */
92 OVERLAY_F_IN_MUX = 0x02, /* Currently in a mux */
93 OVERLAY_F_IN_TX = 0x04, /* Currently doing tx */
94 OVERLAY_F_IN_RX = 0x08, /* Currently doing rx */
95 OVERLAY_F_IOMASK = 0x0c, /* A mask for rx and tx */
96 OVERLAY_F_MDDROP = 0x10, /* Drop traffic for metadata update */
97 OVERLAY_F_STOPMASK = 0x1e, /* None set when stopping */
98 OVERLAY_F_VARPD = 0x20, /* varpd plugin exists */
99 OVERLAY_F_DEGRADED = 0x40, /* device is degraded */
100 OVERLAY_F_TXSTOPPED = 0x80, /* device needs max_tx_update */
101 OVERLAY_F_MASK = 0xff /* mask of everything */
102 } overlay_dev_flag_t;
103
104 typedef struct overlay_dev {
105 kmutex_t odd_lock;
106 kcondvar_t odd_iowait;
107 list_node_t odd_link; /* overlay_dev_lock */
108 mac_handle_t odd_mh; /* RO */
109 overlay_plugin_t *odd_plugin; /* RO */
110 datalink_id_t odd_linkid; /* RO */
111 void *odd_pvoid; /* RO -- only used by plugin */
112 uint_t odd_ref; /* protected by odd_lock */
113 uint_t odd_mtu; /* protected by odd_lock */
114 overlay_dev_flag_t odd_flags; /* protected by odd_lock */
115 uint_t odd_rxcount; /* protected by odd_lock */
116 uint_t odd_txcount; /* protected by odd_lock */
117 overlay_mux_t *odd_mux; /* protected by odd_lock */
118 uint64_t odd_vid; /* RO if active else odd_lock */
119 avl_node_t odd_muxnode; /* managed by mux */
120 overlay_target_t *odd_target; /* See big theory statement */
121 char odd_fmamsg[OVERLAY_STATUS_BUFLEN]; /* odd_lock */
122 } overlay_dev_t;
123
124 typedef enum overlay_target_entry_flags {
125 OVERLAY_ENTRY_F_PENDING = 0x01, /* lookup in progress */
126 OVERLAY_ENTRY_F_VALID = 0x02, /* entry is currently valid */
127 OVERLAY_ENTRY_F_DROP = 0x04, /* always drop target */
128 OVERLAY_ENTRY_F_VALID_MASK = 0x06
129 } overlay_target_entry_flags_t;
130
131 typedef struct overlay_target_entry {
132 kmutex_t ote_lock;
133 refhash_link_t ote_reflink; /* hashtable link */
134 avl_node_t ote_avllink; /* iteration link */
135 list_node_t ote_qlink;
136 overlay_target_entry_flags_t ote_flags; /* RW: state flags */
137 uint8_t ote_addr[ETHERADDRL]; /* RO: mac addr */
138 overlay_target_t *ote_ott; /* RO */
139 overlay_dev_t *ote_odd; /* RO */
140 overlay_target_point_t ote_dest; /* RW: destination */
141 mblk_t *ote_chead; /* RW: blocked mb chain head */
142 mblk_t *ote_ctail; /* RW: blocked mb chain tail */
143 size_t ote_mbsize; /* RW: outstanding mblk size */
144 hrtime_t ote_vtime; /* RW: valid timestamp */
145 } overlay_target_entry_t;
146
147
148 #define OVERLAY_CTL "overlay"
149
150 extern dev_info_t *overlay_dip;
151
152 extern mblk_t *overlay_m_tx(void *, mblk_t *);
153
154 typedef int (*overlay_dev_iter_f)(overlay_dev_t *, void *);
155 extern void overlay_dev_iter(overlay_dev_iter_f, void *);
156
157 extern void overlay_plugin_init(void);
158 extern overlay_plugin_t *overlay_plugin_lookup(const char *);
159 extern void overlay_plugin_rele(overlay_plugin_t *);
160 extern void overlay_plugin_fini(void);
161 typedef int (*overlay_plugin_walk_f)(overlay_plugin_t *, void *);
162 extern void overlay_plugin_walk(overlay_plugin_walk_f, void *);
163
164 extern void overlay_io_start(overlay_dev_t *, overlay_dev_flag_t);
165 extern void overlay_io_done(overlay_dev_t *, overlay_dev_flag_t);
166
167 extern void overlay_mux_init(void);
168 extern void overlay_mux_fini(void);
169
170 extern overlay_mux_t *overlay_mux_open(overlay_plugin_t *, int, int, int,
171 struct sockaddr *, socklen_t, int *);
172 extern void overlay_mux_close(overlay_mux_t *);
173 extern void overlay_mux_add_dev(overlay_mux_t *, overlay_dev_t *);
174 extern void overlay_mux_remove_dev(overlay_mux_t *, overlay_dev_t *);
175 extern int overlay_mux_tx(overlay_mux_t *, struct msghdr *, mblk_t *);
176
177 extern void overlay_prop_init(overlay_prop_handle_t);
178
179 extern void overlay_target_init(void);
180 extern int overlay_target_busy(void);
181 extern int overlay_target_open(dev_t *, int, int, cred_t *);
182 extern int overlay_target_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
183 extern int overlay_target_close(dev_t, int, int, cred_t *);
184 extern void overlay_target_free(overlay_dev_t *);
185
186 #define OVERLAY_TARGET_OK 0
187 #define OVERLAY_TARGET_DROP 1
188 #define OVERLAY_TARGET_ASYNC 2
189 extern int overlay_target_lookup(overlay_dev_t *, mblk_t *, struct sockaddr *,
190 socklen_t *);
191 extern void overlay_target_quiesce(overlay_target_t *);
192 extern void overlay_target_fini(void);
193
194 extern void overlay_fm_init(void);
195 extern void overlay_fm_fini(void);
196 extern void overlay_fm_degrade(overlay_dev_t *, const char *);
197 extern void overlay_fm_restore(overlay_dev_t *);
198
199 extern overlay_dev_t *overlay_hold_by_dlid(datalink_id_t);
200 extern void overlay_hold_rele(overlay_dev_t *);
201
202 #ifdef __cplusplus
203 }
204 #endif
205
206 #endif /* _SYS_OVERLAY_IMPL_H */