1 /*
2 * Copyright (c) 2021-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 #include <sys/sysctl.h>
29 #include "skywalk_test_driver.h"
30 #include "skywalk_test_utils.h"
31 #include "skywalk_test_common.h"
32
33 #define TEST_LPORT 12345
34 #define TEST_RPORT 45678
35 #define TEST_QSET_ID 0x0001
36
37 static void
fill_traffic_descriptor_v4(struct ifnet_traffic_descriptor_inet * td)38 fill_traffic_descriptor_v4(struct ifnet_traffic_descriptor_inet *td)
39 {
40 struct in_addr feth0_addr, feth1_addr;
41
42 bzero(td, sizeof(*td));
43
44 td->inet_common.itd_type = IFNET_TRAFFIC_DESCRIPTOR_TYPE_INET;
45 td->inet_common.itd_len = sizeof(*td);
46 td->inet_common.itd_flags = IFNET_TRAFFIC_DESCRIPTOR_FLAG_INBOUND |
47 IFNET_TRAFFIC_DESCRIPTOR_FLAG_OUTBOUND;
48
49 td->inet_mask = IFNET_TRAFFIC_DESCRIPTOR_INET_IPVER |
50 IFNET_TRAFFIC_DESCRIPTOR_INET_PROTO |
51 IFNET_TRAFFIC_DESCRIPTOR_INET_LADDR |
52 IFNET_TRAFFIC_DESCRIPTOR_INET_RADDR |
53 IFNET_TRAFFIC_DESCRIPTOR_INET_LPORT |
54 IFNET_TRAFFIC_DESCRIPTOR_INET_RPORT;
55
56 td->inet_ipver = IPVERSION;
57 td->inet_proto = IPPROTO_TCP;
58
59 feth0_addr = sktc_feth0_in_addr();
60 td->inet_laddr.iia_v4addr = feth0_addr.s_addr;
61 feth1_addr = sktc_feth1_in_addr();
62 td->inet_raddr.iia_v4addr = feth1_addr.s_addr;
63
64 td->inet_lport = htons(TEST_LPORT);
65 td->inet_rport = htons(TEST_RPORT);
66 }
67
68 static void
fill_traffic_descriptor_v6(struct ifnet_traffic_descriptor_inet * td)69 fill_traffic_descriptor_v6(struct ifnet_traffic_descriptor_inet *td)
70 {
71 bzero(td, sizeof(*td));
72
73 td->inet_common.itd_type = IFNET_TRAFFIC_DESCRIPTOR_TYPE_INET;
74 td->inet_common.itd_len = sizeof(*td);
75 td->inet_common.itd_flags = IFNET_TRAFFIC_DESCRIPTOR_FLAG_INBOUND |
76 IFNET_TRAFFIC_DESCRIPTOR_FLAG_OUTBOUND;
77
78 td->inet_mask = IFNET_TRAFFIC_DESCRIPTOR_INET_IPVER |
79 IFNET_TRAFFIC_DESCRIPTOR_INET_PROTO |
80 IFNET_TRAFFIC_DESCRIPTOR_INET_LADDR |
81 IFNET_TRAFFIC_DESCRIPTOR_INET_RADDR |
82 IFNET_TRAFFIC_DESCRIPTOR_INET_LPORT |
83 IFNET_TRAFFIC_DESCRIPTOR_INET_RPORT;
84
85 td->inet_ipver = IPV6_VERSION;
86 td->inet_proto = IPPROTO_TCP;
87
88 sktc_feth0_inet6_addr((in6_addr_t *)&td->inet_laddr);
89 sktc_feth1_inet6_addr((in6_addr_t *)&td->inet_raddr);
90 td->inet_lport = htons(TEST_LPORT);
91 td->inet_rport = htons(TEST_RPORT);
92 }
93
94 static void
fill_traffic_rule_action(struct ifnet_traffic_rule_action_steer * ra)95 fill_traffic_rule_action(struct ifnet_traffic_rule_action_steer *ra)
96 {
97 bzero(ra, sizeof(*ra));
98
99 ra->ras_common.ra_type = IFNET_TRAFFIC_RULE_ACTION_STEER;
100 ra->ras_common.ra_len = sizeof(*ra);
101 ra->ras_qset_id = TEST_QSET_ID;
102 }
103
104 static int
skt_steering_main(int argc,char * argv[])105 skt_steering_main(int argc, char *argv[])
106 {
107 nexus_controller_t ctl;
108 struct ifnet_traffic_descriptor_inet td;
109 struct ifnet_traffic_rule_action_steer ra;
110 uuid_t v4_rule, v6_rule;
111 int err;
112
113 ctl = os_nexus_controller_create();
114 assert(ctl != NULL);
115
116 fill_traffic_rule_action(&ra);
117
118 fill_traffic_descriptor_v4(&td);
119 err = os_nexus_controller_add_traffic_rule(ctl, FETH0_NAME,
120 (struct ifnet_traffic_descriptor_common *)&td,
121 (struct ifnet_traffic_rule_action *)&ra, 0, &v4_rule);
122 assert(err == 0);
123
124 fill_traffic_descriptor_v6(&td);
125 err = os_nexus_controller_add_traffic_rule(ctl, FETH0_NAME,
126 (struct ifnet_traffic_descriptor_common *)&td,
127 (struct ifnet_traffic_rule_action *)&ra, 0, &v6_rule);
128 assert(err == 0);
129
130 err = os_nexus_controller_remove_traffic_rule(ctl, v4_rule);
131 assert(err == 0);
132
133 err = os_nexus_controller_remove_traffic_rule(ctl, v6_rule);
134 assert(err == 0);
135
136 os_nexus_controller_destroy(ctl);
137 return 0;
138 }
139
140 static uint32_t skt_netif_nxctl_check;
141 static void
skt_steering_init(void)142 skt_steering_init(void)
143 {
144 uint32_t nxctl_check = 1;
145 size_t len = sizeof(skt_netif_nxctl_check);
146
147 assert(sysctlbyname("kern.skywalk.disable_nxctl_check",
148 &skt_netif_nxctl_check, &len, &nxctl_check,
149 sizeof(nxctl_check)) == 0);
150 sktc_ifnet_feth_pair_create(FETH_FLAGS_NATIVE |
151 FETH_FLAGS_NXATTACH);
152 }
153
154 static void
skt_steering_fini(void)155 skt_steering_fini(void)
156 {
157 assert(sysctlbyname("kern.skywalk.disable_nxctl_check",
158 NULL, NULL, &skt_netif_nxctl_check,
159 sizeof(skt_netif_nxctl_check)) == 0);
160 sktc_ifnet_feth_pair_destroy();
161 }
162
163 struct skywalk_test skt_steering = {
164 "steering",
165 "steering rules test",
166 SK_FEATURE_SKYWALK | SK_FEATURE_NEXUS_NETIF | SK_FEATURE_DEV_OR_DEBUG,
167 skt_steering_main,
168 { NULL },
169 skt_steering_init, skt_steering_fini,
170 };
171