xref: /xnu-8020.121.3/bsd/skywalk/nexus/flowswitch/fsw_netagent.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 2016-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <skywalk/os_skywalk_private.h>
30 #include <skywalk/nexus/flowswitch/nx_flowswitch.h>
31 #include <skywalk/nexus/flowswitch/fsw_var.h>
32 
33 /* automatically register a netagent at constructor time */
34 static int fsw_netagent = 1;
35 
36 #if (DEVELOPMENT || DEBUG)
37 SYSCTL_INT(_kern_skywalk_flowswitch, OID_AUTO, netagent,
38     CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_netagent, 0, "");
39 #endif /* !DEVELOPMENT && !DEBUG */
40 
41 static packet_svc_class_t
fsw_netagent_tc2sc(uint32_t tc,uint32_t * scp)42 fsw_netagent_tc2sc(uint32_t tc, uint32_t *scp)
43 {
44 	uint32_t sc;
45 	int ret = 0;
46 
47 	switch (tc) {
48 	case SO_TC_BK_SYS:
49 		sc = PKT_SC_BK_SYS;
50 		break;
51 	case SO_TC_BK:
52 		sc = PKT_SC_BK;
53 		break;
54 	case SO_TC_BE:
55 		sc = PKT_SC_BE;
56 		break;
57 	case SO_TC_RD:
58 		sc = PKT_SC_RD;
59 		break;
60 	case SO_TC_OAM:
61 		sc = PKT_SC_OAM;
62 		break;
63 	case SO_TC_AV:
64 		sc = PKT_SC_AV;
65 		break;
66 	case SO_TC_RV:
67 		sc = PKT_SC_RV;
68 		break;
69 	case SO_TC_VI:
70 		sc = PKT_SC_VI;
71 		break;
72 	case SO_TC_NETSVC_SIG:
73 		sc = PKT_SC_SIG;
74 		break;
75 	case SO_TC_VO:
76 		sc = PKT_SC_VO;
77 		break;
78 	case SO_TC_CTL:
79 		sc = PKT_SC_CTL;
80 		break;
81 	default:
82 		sc = PKT_SC_BE;
83 		ret = -1;
84 		break;
85 	}
86 
87 	*scp = sc;
88 	return ret;
89 }
90 
91 static int
fsw_netagent_flow_add(struct nx_flowswitch * fsw,uuid_t flow_uuid,pid_t pid,void * context,struct necp_client_nexus_parameters * cparams,void ** results,size_t * results_length)92 fsw_netagent_flow_add(struct nx_flowswitch *fsw, uuid_t flow_uuid, pid_t pid,
93     void *context, struct necp_client_nexus_parameters *cparams,
94     void **results, size_t *results_length)
95 {
96 	struct nx_flow_req req;
97 	struct flow_owner *fo = NULL;
98 	size_t assign_message_length = 0;
99 	void *assign_message = NULL;
100 	struct necp_client_endpoint local_endpoint;
101 	struct necp_client_endpoint remote_endpoint;
102 	int error;
103 
104 	ASSERT(cparams != NULL);
105 	ASSERT(results != NULL && *results == NULL);
106 	ASSERT(results_length != NULL && *results_length == 0);
107 
108 	bzero(&req, sizeof(req));
109 	req.nfr_nx_port = NEXUS_PORT_ANY;
110 	req.nfr_flowadv_idx = FLOWADV_IDX_NONE;
111 	bcopy((void *)&cparams->local_addr, (void *)&req.nfr_saddr,
112 	    sizeof(cparams->local_addr));
113 	bcopy((void *)&cparams->remote_addr, (void *)&req.nfr_daddr,
114 	    sizeof(cparams->remote_addr));
115 	req.nfr_flags = (NXFLOWREQF_TRACK | NXFLOWREQF_FLOWADV);
116 	req.nfr_ip_protocol = cparams->ip_protocol;
117 	req.nfr_transport_protocol = cparams->transport_protocol;
118 	uuid_copy(req.nfr_flow_uuid, flow_uuid);
119 	uuid_copy(req.nfr_euuid, cparams->euuid);
120 	req.nfr_epid = cparams->epid;
121 	req.nfr_policy_id = (uint32_t)cparams->policy_id;
122 
123 	if (fsw_netagent_tc2sc(cparams->traffic_class,
124 	    &req.nfr_svc_class) != 0) {
125 		error = EINVAL;
126 		goto done;
127 	}
128 
129 	if (cparams->allow_qos_marking) {
130 		req.nfr_flags |= NXFLOWREQF_QOS_MARKING;
131 	}
132 	if (cparams->override_address_selection) {
133 		req.nfr_flags |= NXFLOWREQF_OVERRIDE_ADDRESS_SELECTION;
134 	}
135 	if (cparams->use_stable_address) {
136 		req.nfr_flags |= NXFLOWREQF_USE_STABLE_ADDRESS;
137 	}
138 	if (cparams->no_wake_from_sleep) {
139 		req.nfr_flags |= NXFLOWREQF_NOWAKEFROMSLEEP;
140 	}
141 
142 	req.nfr_context = context;
143 	req.nfr_pid = pid;
144 	req.nfr_port_reservation = cparams->port_reservation;
145 
146 	ASSERT(req.nfr_flags & NXFLOWREQF_FLOWADV);
147 	fo = fsw_flow_add(fsw, &req, &error);
148 	if (fo == NULL) {
149 		ASSERT(error != 0);
150 		ASSERT(req.nfr_flow_stats == NULL);
151 		goto done;
152 	}
153 
154 	ASSERT(error == 0);
155 	ASSERT(req.nfr_nx_port != NEXUS_PORT_ANY);
156 	ASSERT(!uuid_is_null(fo->fo_key));
157 	ASSERT(req.nfr_flowadv_idx != FLOWADV_IDX_NONE);
158 	ASSERT(req.nfr_flow_stats != NULL);
159 	ASSERT(flow_stats_refcnt(req.nfr_flow_stats) == 2);
160 
161 	bzero(&local_endpoint, sizeof(local_endpoint));
162 	bzero(&remote_endpoint, sizeof(remote_endpoint));
163 
164 	bcopy((void *)&req.nfr_saddr, (void *)&local_endpoint.u.sin6,
165 	    sizeof(req.nfr_saddr));
166 	bcopy((void *)&req.nfr_daddr, (void *)&remote_endpoint.u.sin6,
167 	    sizeof(req.nfr_daddr));
168 
169 	assign_message =
170 	    necp_create_nexus_assign_message(fsw->fsw_nx->nx_uuid,
171 	    req.nfr_nx_port, fo->fo_key, sizeof(fo->fo_key),
172 	    &local_endpoint, &remote_endpoint, NULL, req.nfr_flowadv_idx,
173 	    req.nfr_flow_stats, &assign_message_length);
174 
175 	if (assign_message != NULL) {
176 		req.nfr_flow_stats = NULL;
177 		ASSERT(error == 0);
178 		*results = assign_message;
179 		*results_length = assign_message_length;
180 	} else {
181 		error = ENOMEM;
182 	}
183 
184 done:
185 #if SK_LOG
186 	if (__improbable((sk_verbose & SK_VERB_FLOW) != 0)) {
187 		uuid_string_t uuidstr;
188 		SK_DF(SK_VERB_FLOW, "pid %d connect flow_uuid %s nx_port %d "
189 		    "(err %d)", pid, sk_uuid_unparse(flow_uuid, uuidstr),
190 		    (int)req.nfr_nx_port, error);
191 	}
192 #endif /* SK_LOG */
193 	if (error != 0) {
194 		ASSERT(*results == NULL);
195 		ASSERT(*results_length == 0);
196 		if (assign_message != NULL) {
197 			kfree_data(assign_message, assign_message_length);
198 			assign_message = NULL;
199 		}
200 		if (fo != NULL) {
201 			req.nfr_pid = pid;
202 			fsw_flow_del(fsw, &req, TRUE, NULL);
203 		}
204 		if (req.nfr_flow_stats != NULL) {
205 			flow_stats_release(req.nfr_flow_stats);
206 			req.nfr_flow_stats = NULL;
207 		}
208 	}
209 
210 	return error;
211 }
212 
213 static int
fsw_netagent_flow_del(struct nx_flowswitch * fsw,uuid_t flow_uuid,pid_t pid,bool nolinger,void * context,void * params)214 fsw_netagent_flow_del(struct nx_flowswitch *fsw, uuid_t flow_uuid, pid_t pid,
215     bool nolinger, void *context, void *params)
216 {
217 	struct nx_flow_req req;
218 	int error;
219 
220 	bzero(&req, sizeof(req));
221 	uuid_copy(req.nfr_flow_uuid, flow_uuid);
222 	req.nfr_proc = NULL;
223 	req.nfr_pid = pid;
224 	req.nfr_context = context;
225 
226 	error = fsw_flow_del(fsw, &req, nolinger, params);
227 
228 #if SK_LOG
229 	if (__improbable((sk_verbose & SK_VERB_FLOW) != 0)) {
230 		uuid_string_t uuidstr;
231 		SK_DF(SK_VERB_FLOW, "pid %d flow_uuid %s (err %d)",
232 		    pid, sk_uuid_unparse(flow_uuid, uuidstr), error);
233 	}
234 #endif /* SK_LOG */
235 
236 	return error;
237 }
238 
239 static int
fsw_netagent_event(u_int8_t event,uuid_t flow_uuid,pid_t pid,void * context,void * ctx,struct necp_client_agent_parameters * cparams,void ** results,size_t * results_length)240 fsw_netagent_event(u_int8_t event, uuid_t flow_uuid, pid_t pid, void *context,
241     void *ctx, struct necp_client_agent_parameters *cparams, void **results,
242     size_t *results_length)
243 {
244 	struct nx_flowswitch *fsw;
245 	int error = 0;
246 
247 	ASSERT(!uuid_is_null(flow_uuid));
248 
249 	fsw = (struct nx_flowswitch *)ctx;
250 	ASSERT(fsw != NULL);
251 
252 	switch (event) {
253 	case NETAGENT_EVENT_NEXUS_FLOW_INSERT:
254 		/* these are required for this event */
255 		ASSERT(cparams != NULL);
256 		ASSERT(results != NULL);
257 		ASSERT(results_length != NULL);
258 		*results = NULL;
259 		*results_length = 0;
260 		error = fsw_netagent_flow_add(fsw, flow_uuid, pid, context,
261 		    &cparams->u.nexus_request, results, results_length);
262 		break;
263 
264 	case NETAGENT_EVENT_NEXUS_FLOW_REMOVE:
265 	case NETAGENT_EVENT_NEXUS_FLOW_ABORT:
266 		/*
267 		 * A flow can be removed gracefully (FLOW_REMOVE) by
268 		 * the client, in which case we don't need to have it
269 		 * linger around to hold the namespace.  If the process
270 		 * crashed or if it's suspended, then we treat that
271 		 * as the abort case (FLOW_ABORT) where we'll hold on
272 		 * to the namespace if needed.
273 		 */
274 		error = fsw_netagent_flow_del(fsw, flow_uuid, pid,
275 		    (event == NETAGENT_EVENT_NEXUS_FLOW_REMOVE), context,
276 		    cparams);
277 		break;
278 
279 	default:
280 		/* events not handled */
281 		return 0;
282 	}
283 
284 	return error;
285 }
286 
287 int
fsw_netagent_register(struct nx_flowswitch * fsw,struct ifnet * ifp)288 fsw_netagent_register(struct nx_flowswitch *fsw, struct ifnet *ifp)
289 {
290 	struct netagent_nexus_agent agent;
291 	int error = 0;
292 
293 	_CASSERT(FLOWADV_IDX_NONE == UINT32_MAX);
294 	_CASSERT(NECP_FLOWADV_IDX_INVALID == FLOWADV_IDX_NONE);
295 
296 	if (!fsw_netagent) {
297 		return 0;
298 	}
299 
300 	fsw->fsw_agent_session = netagent_create(fsw_netagent_event, fsw);
301 	if (fsw->fsw_agent_session == NULL) {
302 		return ENOMEM;
303 	}
304 
305 	bzero(&agent, sizeof(agent));
306 	uuid_generate_random(agent.agent.netagent_uuid);
307 	uuid_copy(fsw->fsw_agent_uuid, agent.agent.netagent_uuid);
308 	(void) snprintf(agent.agent.netagent_domain,
309 	    sizeof(agent.agent.netagent_domain), "%s", "Skywalk");
310 	(void) snprintf(agent.agent.netagent_type,
311 	    sizeof(agent.agent.netagent_type), "%s", "FlowSwitch");
312 	(void) snprintf(agent.agent.netagent_desc,
313 	    sizeof(agent.agent.netagent_desc), "%s", "Userspace Networking");
314 	agent.agent.netagent_flags = NETAGENT_FLAG_ACTIVE;
315 	if (ifnet_needs_fsw_transport_netagent(ifp)) {
316 		agent.agent.netagent_flags |= (NETAGENT_FLAG_NEXUS_PROVIDER |
317 		    NETAGENT_FLAG_NEXUS_LISTENER);
318 	}
319 	if (ifnet_needs_fsw_ip_netagent(ifp)) {
320 		ASSERT((sk_features & SK_FEATURE_PROTONS) != 0);
321 		agent.agent.netagent_flags |= (NETAGENT_FLAG_CUSTOM_IP_NEXUS |
322 		    NETAGENT_FLAG_NEXUS_LISTENER);
323 	}
324 	agent.agent.netagent_data_size = sizeof(struct netagent_nexus);
325 	agent.nexus_data.frame_type = NETAGENT_NEXUS_FRAME_TYPE_INTERNET;
326 	agent.nexus_data.endpoint_assignment_type =
327 	    NETAGENT_NEXUS_ENDPOINT_TYPE_ADDRESS;
328 	agent.nexus_data.endpoint_request_types[0] =
329 	    NETAGENT_NEXUS_ENDPOINT_TYPE_ADDRESS;
330 	agent.nexus_data.endpoint_resolution_type_pairs[0] =
331 	    NETAGENT_NEXUS_ENDPOINT_TYPE_HOST;
332 	agent.nexus_data.endpoint_resolution_type_pairs[1] =
333 	    NETAGENT_NEXUS_ENDPOINT_TYPE_ADDRESS;
334 	agent.nexus_data.endpoint_resolution_type_pairs[2] =
335 	    NETAGENT_NEXUS_ENDPOINT_TYPE_BONJOUR;
336 	agent.nexus_data.endpoint_resolution_type_pairs[3] =
337 	    NETAGENT_NEXUS_ENDPOINT_TYPE_HOST;
338 	agent.nexus_data.endpoint_resolution_type_pairs[4] =
339 	    NETAGENT_NEXUS_ENDPOINT_TYPE_SRV;
340 	agent.nexus_data.endpoint_resolution_type_pairs[5] =
341 	    NETAGENT_NEXUS_ENDPOINT_TYPE_HOST;
342 	agent.nexus_data.nexus_max_buf_size =
343 	    fsw->fsw_nx->nx_prov->nxprov_params->nxp_buf_size;
344 	agent.nexus_data.nexus_flags |=
345 	    (NETAGENT_NEXUS_FLAG_SUPPORTS_USER_PACKET_POOL |
346 	    NETAGENT_NEXUS_FLAG_ASSERT_UNSUPPORTED);
347 
348 	error = netagent_register(fsw->fsw_agent_session, &agent.agent);
349 	if (error != 0) {
350 		goto fail_netagent_register;
351 	}
352 
353 	error = if_add_netagent(ifp, fsw->fsw_agent_uuid);
354 	if (error != 0) {
355 		goto fail_netagent_add;
356 	}
357 	fsw->fsw_state_flags |= FSW_STATEF_NETAGENT_ADDED;
358 	if (if_is_fsw_netagent_enabled()) {
359 		fsw->fsw_state_flags |= FSW_STATEF_NETAGENT_ENABLED;
360 	}
361 
362 	return 0;
363 
364 fail_netagent_add:
365 	netagent_unregister(fsw->fsw_agent_session);
366 
367 fail_netagent_register:
368 	netagent_destroy(fsw->fsw_agent_session);
369 	fsw->fsw_agent_session = NULL;
370 	uuid_clear(fsw->fsw_agent_uuid);
371 
372 	return error;
373 }
374 
375 void
fsw_netagent_unregister(struct nx_flowswitch * fsw,struct ifnet * ifp)376 fsw_netagent_unregister(struct nx_flowswitch *fsw, struct ifnet *ifp)
377 {
378 	if (!uuid_is_null(fsw->fsw_agent_uuid)) {
379 		if_delete_netagent(ifp, fsw->fsw_agent_uuid);
380 	}
381 
382 	if (fsw->fsw_agent_session != NULL) {
383 		netagent_destroy(fsw->fsw_agent_session);
384 		fsw->fsw_agent_session = NULL;
385 		uuid_clear(fsw->fsw_agent_uuid);
386 	}
387 }
388