1 /*
2 * Copyright (c) 2016-2024 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 /* This test attaches a flow switch to itself.
30 */
31
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <darwintest.h>
39
40 #include "skywalk_test_common.h"
41 #include "skywalk_test_driver.h"
42 #include "skywalk_test_utils.h"
43
44 static void
skt_fswloop_common(const char * name)45 skt_fswloop_common(const char *name)
46 {
47 uuid_t attach;
48 int error;
49 struct sktc_nexus_attr attr = SKTC_NEXUS_ATTR_INIT();
50
51 strncpy((char *)attr.name, name, sizeof(nexus_name_t) - 1);
52 attr.type = NEXUS_TYPE_FLOW_SWITCH;
53 attr.anonymous = 1;
54
55 sktc_setup_nexus(&attr);
56
57 error = __os_nexus_ifattach(sktc_nexus_controller, sktc_instance_uuid,
58 NULL, sktc_instance_uuid, false, &attach);
59 SKTC_ASSERT_ERR(error == -1);
60 SKTC_ASSERT_ERR(errno == EINVAL);
61 }
62
63 static int
skt_fswloopfsw_main(int argc,char * argv[])64 skt_fswloopfsw_main(int argc, char *argv[])
65 {
66 skt_fswloop_common("skywalk_test_fswloop");
67 return 0;
68 }
69
70 struct skywalk_test skt_fswloopfsw = {
71 "fswloopfsw", "create a flow-switch and attach it to itself",
72 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_FLOWSWITCH,
73 skt_fswloopfsw_main,
74 };
75
76 void
skt_fswloop2_common(boolean_t add_netif)77 skt_fswloop2_common(boolean_t add_netif)
78 {
79 int error;
80 nexus_controller_t ncd;
81 uuid_t provider0, instance0, provider1, instance1, attach;
82 uuid_t feth0_attach, feth1_attach;
83 uuid_t netif_provider, netif0_instance, netif1_instance;
84 struct sktc_nexus_attr attr = SKTC_NEXUS_ATTR_INIT();
85
86 ncd = os_nexus_controller_create();
87 assert(ncd);
88
89 strncpy((char *)attr.name, "skt_fswloop2_zero",
90 sizeof(nexus_name_t) - 1);
91 attr.type = NEXUS_TYPE_FLOW_SWITCH;
92 attr.anonymous = 1;
93 sktc_build_nexus(ncd, &attr, &provider0, &instance0);
94
95 strncpy((char *)attr.name, "skt_fswloop2_one",
96 sizeof(nexus_name_t) - 1);
97 sktc_build_nexus(ncd, &attr, &provider1, &instance1);
98
99 if (add_netif) {
100 strncpy((char *)attr.name, "skt_netif_feth",
101 sizeof(nexus_name_t) - 1);
102 attr.type = NEXUS_TYPE_NET_IF;
103 sktc_build_nexus(ncd, &attr, &netif_provider, &netif0_instance);
104
105 error = __os_nexus_ifattach(ncd, netif0_instance, FETH0_NAME,
106 NULL, false, &feth0_attach);
107 SKTC_ASSERT_ERR(!error);
108
109 error = os_nexus_controller_alloc_provider_instance(ncd,
110 netif_provider, &netif1_instance);
111 SKTC_ASSERT_ERR(!error);
112
113 error = __os_nexus_ifattach(ncd, netif1_instance, FETH1_NAME,
114 NULL, false, &feth1_attach);
115 SKTC_ASSERT_ERR(!error);
116 }
117
118 error = __os_nexus_ifattach(ncd, instance1, NULL, instance0, false, &attach);
119 //T_LOG("%s:%d error %d errno %d\n", __func__, __LINE__, error, errno);
120 /* Can't attach a flowswitch to anything */
121 SKTC_ASSERT_ERR(error == -1);
122 SKTC_ASSERT_ERR(errno == EINVAL);
123
124 /* Now also try to attach them the other way around */
125 error = __os_nexus_ifattach(ncd, instance0, NULL, instance1, false, &attach);
126 //T_LOG("%s:%d error %d errno %d\n", __func__, __LINE__, error, errno);
127 /* Can't attach a flowswitch to anything */
128 SKTC_ASSERT_ERR(error == -1);
129 SKTC_ASSERT_ERR(errno == EINVAL);
130 }
131
132 int
skt_fswloop2ff_main(int argc,char * argv[])133 skt_fswloop2ff_main(int argc, char *argv[])
134 {
135 skt_fswloop2_common(false);
136 return 0;
137 }
138
139 struct skywalk_test skt_fswloop2ff = {
140 "fswloop2mm", "attach a flowswitch to a flowswitch without any netif",
141 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_FLOWSWITCH,
142 skt_fswloop2ff_main,
143 };
144
145 int
skt_fswloop2nff_main(int argc,char * argv[])146 skt_fswloop2nff_main(int argc, char *argv[])
147 {
148 skt_fswloop2_common(true);
149 return 0;
150 }
151
152 struct skywalk_test skt_fswloop2nmm = {
153 "fswloop2nmm", "attach a flowswitch to a flowswitch and back to itself",
154 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_NETIF | SK_FEATURE_NEXUS_FLOWSWITCH,
155 skt_fswloop2nff_main, { NULL },
156 sktc_ifnet_feth0_1_create, sktc_ifnet_feth0_1_destroy,
157 };
158