Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | /* * Copyright (c) 2015-2022 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <stdlib.h> #include <string.h> #include <strings.h> #include <unistd.h> #include <errno.h> #include <net/if_var.h> #include <skywalk/os_skywalk_private.h> #ifndef LIBSYSCALL_INTERFACE #error "LIBSYSCALL_INTERFACE not defined" #endif /* !LIBSYSCALL_INTERFACE */ nexus_attr_t os_nexus_attr_create(void) { struct nexus_attr *nxa; nxa = malloc(sizeof(*nxa)); if (nxa != NULL) { bzero(nxa, sizeof(*nxa)); } return nxa; } nexus_attr_t os_nexus_attr_clone(const nexus_attr_t nxa) { struct nexus_attr *nnxa = NULL; nnxa = os_nexus_attr_create(); if (nnxa != NULL && nxa != NULL) { bcopy(nxa, nnxa, sizeof(*nnxa)); } return nnxa; } int os_nexus_attr_set(const nexus_attr_t nxa, const nexus_attr_type_t type, const uint64_t value) { return __nexus_attr_set(nxa, type, value); } int os_nexus_attr_get(const nexus_attr_t nxa, const nexus_attr_type_t type, uint64_t *value) { return __nexus_attr_get(nxa, type, value); } void os_nexus_attr_destroy(nexus_attr_t nxa) { free(nxa); } nexus_controller_t os_nexus_controller_create(void) { struct nexus_controller *ncd = NULL; struct nxctl_init init; int fd; bzero(&init, sizeof(init)); init.ni_version = NEXUSCTL_INIT_CURRENT_VERSION; fd = __nexus_open(&init, sizeof(init)); if (fd == -1) { goto done; } ncd = malloc(sizeof(*ncd)); if (ncd == NULL) { (void) guarded_close_np(fd, &init.ni_guard); goto done; } bzero(ncd, sizeof(*ncd)); ncd->ncd_fd = fd; ncd->ncd_guard = init.ni_guard; done: return ncd; } int os_nexus_controller_get_fd(const nexus_controller_t ncd) { return ncd->ncd_fd; } int os_nexus_controller_register_provider(const nexus_controller_t ncd, const nexus_name_t name, const nexus_type_t type, const nexus_attr_t nxa, uuid_t *prov_uuid) { struct nxprov_reg reg; int err; if ((err = __nexus_provider_reg_prepare(®, name, type, nxa)) == 0) { err = __nexus_register(ncd->ncd_fd, ®, sizeof(reg), prov_uuid, sizeof(uuid_t)); } return err; } int os_nexus_controller_deregister_provider(const nexus_controller_t ncd, const uuid_t prov_uuid) { return __nexus_deregister(ncd->ncd_fd, prov_uuid, sizeof(uuid_t)); } int os_nexus_controller_alloc_provider_instance(const nexus_controller_t ncd, const uuid_t prov_uuid, uuid_t *nx_uuid) { return __nexus_create(ncd->ncd_fd, prov_uuid, sizeof(uuid_t), nx_uuid, sizeof(uuid_t)); } int os_nexus_controller_free_provider_instance(const nexus_controller_t ncd, const uuid_t nx_uuid) { return __nexus_destroy(ncd->ncd_fd, nx_uuid, sizeof(uuid_t)); } int os_nexus_controller_bind_provider_instance(const nexus_controller_t ncd, const uuid_t nx_uuid, const nexus_port_t port, const pid_t pid, const uuid_t exec_uuid, const void *key, const uint32_t key_len, uint32_t bind_flags) { struct nx_bind_req nbr; __nexus_bind_req_prepare(&nbr, nx_uuid, port, pid, exec_uuid, key, key_len, bind_flags); return __nexus_set_opt(ncd->ncd_fd, NXOPT_NEXUS_BIND, &nbr, sizeof(nbr)); } int os_nexus_controller_unbind_provider_instance(const nexus_controller_t ncd, const uuid_t nx_uuid, const nexus_port_t port) { struct nx_unbind_req nbu; __nexus_unbind_req_prepare(&nbu, nx_uuid, port); return __nexus_set_opt(ncd->ncd_fd, NXOPT_NEXUS_UNBIND, &nbu, sizeof(nbu)); } int os_nexus_controller_read_provider_attr(const nexus_controller_t ncd, const uuid_t prov_uuid, nexus_attr_t nxa) { struct nxprov_reg_ent nre; uint32_t nre_len = sizeof(nre); struct nxprov_params *p = &nre.npre_prov_params; int ret = 0; if (nxa == NULL) { return EINVAL; } bzero(&nre, sizeof(nre)); bcopy(prov_uuid, nre.npre_prov_uuid, sizeof(uuid_t)); ret = __nexus_get_opt(ncd->ncd_fd, NXOPT_NEXUS_PROV_ENTRY, &nre, &nre_len); if (ret == 0) { __nexus_attr_from_params(nxa, p); } return ret; } static int add_traffic_rule_inet(const nexus_controller_t ncd, const char *ifname, const struct ifnet_traffic_descriptor_inet *td, const struct ifnet_traffic_rule_action_steer *ra, const uint32_t flags, uuid_t *rule_uuid) { struct nxctl_add_traffic_rule_inet_iocargs args; int err; bzero(&args, sizeof(args)); if (ifname != NULL) { (void) strlcpy(args.atri_ifname, ifname, IFNAMSIZ); } bcopy(td, &args.atri_td, sizeof(args.atri_td)); bcopy(ra, &args.atri_ra, sizeof(args.atri_ra)); if ((flags & NXCTL_ADD_TRAFFIC_RULE_FLAG_PERSIST) != 0) { args.atri_flags |= NXIOC_ADD_TRAFFIC_RULE_FLAG_PERSIST; } err = ioctl(ncd->ncd_fd, NXIOC_ADD_TRAFFIC_RULE_INET, &args); if (err < 0) { return errno; } bcopy(&args.atri_uuid, rule_uuid, sizeof(args.atri_uuid)); return 0; } int os_nexus_controller_add_traffic_rule(const nexus_controller_t ncd, const char *ifname, const struct ifnet_traffic_descriptor_common *td, const struct ifnet_traffic_rule_action *ra, const uint32_t flags, uuid_t *rule_uuid) { /* only support the steer action for now */ if (ra->ra_type != IFNET_TRAFFIC_RULE_ACTION_STEER) { return ENOTSUP; } if (ra->ra_len != sizeof(struct ifnet_traffic_rule_action_steer)) { return EINVAL; } /* only support the inet descriptor type for now */ switch (td->itd_type) { case IFNET_TRAFFIC_DESCRIPTOR_TYPE_INET: { if (td->itd_len != sizeof(struct ifnet_traffic_descriptor_inet)) { return EINVAL; } return add_traffic_rule_inet(ncd, ifname, (const struct ifnet_traffic_descriptor_inet *)td, (const struct ifnet_traffic_rule_action_steer *)ra, flags, rule_uuid); } default: return ENOTSUP; } } int os_nexus_controller_remove_traffic_rule(const nexus_controller_t ncd, const uuid_t rule_uuid) { struct nxctl_remove_traffic_rule_iocargs args; int err; bzero(&args, sizeof(args)); bcopy(rule_uuid, &args.rtr_uuid, sizeof(args.rtr_uuid)); err = ioctl(ncd->ncd_fd, NXIOC_REMOVE_TRAFFIC_RULE, &args); if (err < 0) { return errno; } return 0; } static void inet_rule_iterate(void *buf, uint32_t count, nexus_traffic_rule_iterator_t itr, void *itr_arg) { struct nxctl_traffic_rule_inet_iocinfo *info = buf; struct nxctl_traffic_rule_generic_iocinfo *ginfo; struct nexus_traffic_rule_info itr_info; uint32_t c; for (c = 0; c < count; c++) { bzero(&itr_info, sizeof(itr_info)); ginfo = &info->tri_common; itr_info.nri_rule_uuid = &ginfo->trg_uuid; itr_info.nri_owner = ginfo->trg_procname; itr_info.nri_ifname = ginfo->trg_ifname; itr_info.nri_td = (struct ifnet_traffic_descriptor_common *)&info->tri_td; itr_info.nri_ra = (struct ifnet_traffic_rule_action *)&info->tri_ra; if (!itr(itr_arg, &itr_info)) { break; } info++; } } struct traffic_rule_type { uint8_t tr_type; uint32_t tr_size; uint32_t tr_count; void (*tr_iterate)(void *, uint32_t, nexus_traffic_rule_iterator_t, void *); }; #define NTRDEFAULTCOUNT 512 static struct traffic_rule_type traffic_rule_types[] = { {IFNET_TRAFFIC_DESCRIPTOR_TYPE_INET, sizeof(struct nxctl_traffic_rule_inet_iocinfo), NTRDEFAULTCOUNT, inet_rule_iterate}, }; #define NTRTYPES (sizeof(traffic_rule_types)/sizeof(struct traffic_rule_type)) int os_nexus_controller_iterate_traffic_rules(const nexus_controller_t ncd, nexus_traffic_rule_iterator_t itr, void *itr_arg) { struct nxctl_get_traffic_rules_iocargs args; struct traffic_rule_type *t; int i, err; for (i = 0; i < NTRTYPES; i++) { t = &traffic_rule_types[i]; bzero(&args, sizeof(args)); args.gtr_type = t->tr_type; args.gtr_size = t->tr_size; args.gtr_count = t->tr_count; args.gtr_buf = malloc(args.gtr_size * args.gtr_count); if (args.gtr_buf == NULL) { return ENOMEM; } err = ioctl(ncd->ncd_fd, NXIOC_GET_TRAFFIC_RULES, &args); if (err < 0) { err = errno; free(args.gtr_buf); return err; } if (args.gtr_count > 0) { t->tr_iterate(args.gtr_buf, args.gtr_count, itr, itr_arg); } free(args.gtr_buf); } return 0; } void os_nexus_controller_destroy(nexus_controller_t ncd) { if (ncd->ncd_fd != -1) { (void) guarded_close_np(ncd->ncd_fd, &ncd->ncd_guard); } free(ncd); } int __os_nexus_ifattach(const nexus_controller_t ncd, const uuid_t nx_uuid, const char *ifname, const uuid_t netif_uuid, boolean_t host, uuid_t *nx_if_uuid) { struct nx_cfg_req ncr; struct nx_spec_req nsr; int ret; bzero(&nsr, sizeof(nsr)); if (ifname != NULL) { (void) strlcpy(nsr.nsr_name, ifname, sizeof(nsr.nsr_name)); } else { bcopy(netif_uuid, nsr.nsr_uuid, sizeof(uuid_t)); nsr.nsr_flags |= NXSPECREQ_UUID; } if (host) { nsr.nsr_flags |= NXSPECREQ_HOST; } __nexus_config_req_prepare(&ncr, nx_uuid, NXCFG_CMD_ATTACH, &nsr, sizeof(nsr)); ret = __nexus_set_opt(ncd->ncd_fd, NXOPT_NEXUS_CONFIG, &ncr, sizeof(ncr)); if (ret == 0) { bcopy(nsr.nsr_if_uuid, nx_if_uuid, sizeof(uuid_t)); } return ret; } int __os_nexus_ifdetach(const nexus_controller_t ncd, const uuid_t nx_uuid, const uuid_t nx_if_uuid) { struct nx_cfg_req ncr; struct nx_spec_req nsr; bzero(&nsr, sizeof(nsr)); bcopy(nx_if_uuid, nsr.nsr_if_uuid, sizeof(uuid_t)); __nexus_config_req_prepare(&ncr, nx_uuid, NXCFG_CMD_DETACH, &nsr, sizeof(nsr)); return __nexus_set_opt(ncd->ncd_fd, NXOPT_NEXUS_CONFIG, &ncr, sizeof(ncr)); } int __os_nexus_flow_add(const nexus_controller_t ncd, const uuid_t nx_uuid, const struct nx_flow_req *nfr) { struct nx_cfg_req ncr; __nexus_config_req_prepare(&ncr, nx_uuid, NXCFG_CMD_FLOW_ADD, nfr, sizeof(*nfr)); return __nexus_set_opt(ncd->ncd_fd, NXOPT_NEXUS_CONFIG, &ncr, sizeof(ncr)); } int __os_nexus_flow_del(const nexus_controller_t ncd, const uuid_t nx_uuid, const struct nx_flow_req *nfr) { struct nx_cfg_req ncr; __nexus_config_req_prepare(&ncr, nx_uuid, NXCFG_CMD_FLOW_DEL, nfr, sizeof(*nfr)); return __nexus_set_opt(ncd->ncd_fd, NXOPT_NEXUS_CONFIG, &ncr, sizeof(ncr)); } static int __os_nexus_config_flow(const uuid_t nx_uuid, struct nx_flow_req *nfr) { struct nx_cfg_req ncr; __nexus_config_req_prepare(&ncr, nx_uuid, NXCFG_CMD_FLOW_CONFIG, nfr, sizeof(*nfr)); return __nexus_set_opt(__OS_NEXUS_SHARED_USER_CONTROLLER_FD, NXOPT_NEXUS_CONFIG, &ncr, sizeof(ncr)); } int os_nexus_flow_set_wake_from_sleep(const uuid_t nx_uuid, const uuid_t flow_uuid, bool enable) { struct nx_flow_req nfr = {0}; memcpy(nfr.nfr_flow_uuid, flow_uuid, sizeof(uuid_t)); nfr.nfr_flags = enable ? 0 : NXFLOWREQF_NOWAKEFROMSLEEP; return __os_nexus_config_flow(nx_uuid, &nfr); } int __os_nexus_get_llink_info(const nexus_controller_t ncd, const uuid_t nx_uuid, const struct nx_llink_info_req *nlir, size_t len) { struct nx_cfg_req ncr; __nexus_config_req_prepare(&ncr, nx_uuid, NXCFG_CMD_GET_LLINK_INFO, nlir, len); return __nexus_set_opt(ncd->ncd_fd, NXOPT_NEXUS_CONFIG, &ncr, sizeof(ncr)); } |