1 /*
2 * Copyright (c) 2015-2023 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 /*
30 * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 */
53
54 /*
55 * BSD LICENSE
56 *
57 * Copyright(c) 2015 NEC Europe Ltd. All rights reserved.
58 * All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * * Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * * Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in
68 * the documentation and/or other materials provided with the
69 * distribution.
70 * * Neither the name of NEC Europe Ltd. nor the names of
71 * its contributors may be used to endorse or promote products derived
72 * from this software without specific prior written permission.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
75 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
76 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
77 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
78 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
81 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
82 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
83 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85 */
86
87 #include <skywalk/os_skywalk_private.h>
88 #include <skywalk/nexus/flowswitch/nx_flowswitch.h>
89 #include <skywalk/nexus/flowswitch/fsw_var.h>
90 #include <skywalk/nexus/netif/nx_netif.h>
91 #include <skywalk/nexus/netif/nx_netif_compat.h>
92 #include <kern/sched_prim.h>
93 #include <sys/kdebug.h>
94 #include <sys/sdt.h>
95 #include <net/bpf.h>
96 #include <net/if_ports_used.h>
97 #include <net/pktap.h>
98 #include <net/droptap.h>
99 #include <net/pktsched/pktsched_netem.h>
100 #include <netinet/tcp.h>
101 #include <netinet/udp.h>
102 #include <netinet/ip.h>
103 #include <netinet/ip6.h>
104 #include <netinet/in_var.h>
105
106 extern kern_return_t thread_terminate(thread_t);
107
108 #define FSW_ZONE_MAX 256
109 #define FSW_ZONE_NAME "skywalk.nx.fsw"
110
111 static uint64_t fsw_reap_last __sk_aligned(8);
112 static uint64_t fsw_want_purge __sk_aligned(8);
113
114 #define NX_FSW_FE_TABLESZ 256 /* some power of 2 */
115 static uint32_t fsw_fe_table_size = NX_FSW_FE_TABLESZ;
116
117 #define NX_FSW_FOB_HASHSZ 31 /* some mersenne prime */
118 static uint32_t fsw_flow_owner_buckets = NX_FSW_FOB_HASHSZ;
119
120 #define NX_FSW_FRB_HASHSZ 128 /* some power of 2 */
121 static uint32_t fsw_flow_route_buckets = NX_FSW_FRB_HASHSZ;
122
123 #define NX_FSW_FRIB_HASHSZ 13 /* some mersenne prime */
124 static uint32_t fsw_flow_route_id_buckets = NX_FSW_FRIB_HASHSZ;
125
126 #define NX_FSW_FLOW_REAP_INTERVAL 1 /* seconds */
127 static uint32_t fsw_flow_reap_interval = NX_FSW_FLOW_REAP_INTERVAL;
128
129 #define NX_FSW_RX_STALL_THRES 10 /* seconds */
130 static uint32_t fsw_rx_stall_thresh = NX_FSW_RX_STALL_THRES;
131
132 #define NX_FSW_RX_STALL_DEFUNCT 1 /* defunct Rx-stalled channel (0 = disable) */
133 static uint32_t fsw_rx_stall_defunct = NX_FSW_RX_STALL_DEFUNCT;
134
135 #define NX_FSW_FLOW_PURGE_THRES 0 /* purge every N reaps (0 = disable) */
136 static uint32_t fsw_flow_purge_thresh = NX_FSW_FLOW_PURGE_THRES;
137
138 #define FSW_REAP_IVAL (MAX(1, fsw_flow_reap_interval))
139 #define FSW_REAP_SK_THRES (FSW_REAP_IVAL << 5)
140 #define FSW_REAP_IF_THRES (FSW_REAP_IVAL << 5)
141 #define FSW_DRAIN_CH_THRES (FSW_REAP_IVAL << 5)
142 #define FSW_IFSTATS_THRES 1
143
144 #define NX_FSW_CHANNEL_REAP_THRES 1000 /* threshold (bytes/sec) for reaping*/
145 uint64_t fsw_channel_reap_thresh = NX_FSW_CHANNEL_REAP_THRES;
146
147 #define RX_BUFLET_BATCH_COUNT 64 /* max batch size for buflet allocation */
148
149 uint32_t fsw_rx_batch = NX_FSW_RXBATCH; /* # of packets per batch (RX) */
150 uint32_t fsw_tx_batch = NX_FSW_TXBATCH; /* # of packets per batch (TX) */
151 uint32_t fsw_gso_batch = 8;
152 #if (DEVELOPMENT || DEBUG)
153 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, rx_batch,
154 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_rx_batch, 0,
155 "flowswitch Rx batch size");
156 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, tx_batch,
157 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_tx_batch, 0,
158 "flowswitch Tx batch size");
159 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, gso_batch,
160 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_gso_batch, 0,
161 "flowswitch GSO batch size");
162 SYSCTL_QUAD(_kern_skywalk_flowswitch, OID_AUTO, reap_throughput,
163 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_channel_reap_thresh,
164 "flowswitch channel reap threshold throughput (bytes/sec)");
165 #endif /* !DEVELOPMENT && !DEBUG */
166
167 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, rx_agg_tcp,
168 CTLFLAG_RW | CTLFLAG_LOCKED, &sk_fsw_rx_agg_tcp, 0,
169 "flowswitch RX aggregation for tcp flows (enable/disable)");
170 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, rx_agg_tcp_host,
171 CTLFLAG_RW | CTLFLAG_LOCKED, &sk_fsw_rx_agg_tcp_host, 0,
172 "flowswitch RX aggregation for tcp kernel path (0/1/2 (off/on/auto))");
173 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, gso_mtu,
174 CTLFLAG_RW | CTLFLAG_LOCKED, &sk_fsw_gso_mtu, 0,
175 "flowswitch GSO for tcp flows (mtu > 0: enable, mtu == 0: disable)");
176
177 /*
178 * IP reassembly
179 * The "kern.skywalk.flowswitch.ip_reass" sysctl can be used to force
180 * enable/disable the reassembly routine regardless of whether the
181 * transport netagent is enabled or not.
182 *
183 * 'fsw_ip_reass' is a tri-state:
184 * 0 means force IP reassembly off
185 * 1 means force IP reassembly on
186 * 2 means don't force the value, use what's appropriate for this flowswitch
187 */
188 #define FSW_IP_REASS_FORCE_OFF 0
189 #define FSW_IP_REASS_FORCE_ON 1
190 #define FSW_IP_REASS_NO_FORCE 2
191
192 uint32_t fsw_ip_reass = FSW_IP_REASS_NO_FORCE;
193
194 static int
195 fsw_ip_reass_sysctl SYSCTL_HANDLER_ARGS
196 {
197 #pragma unused(oidp, arg1, arg2)
198 unsigned int new_value;
199 int changed;
200 int error;
201
202 error = sysctl_io_number(req, fsw_ip_reass, sizeof(fsw_ip_reass),
203 &new_value, &changed);
204 if (error == 0 && changed != 0) {
205 if (new_value > FSW_IP_REASS_NO_FORCE) {
206 return EINVAL;
207 }
208 fsw_ip_reass = new_value;
209 }
210 return error;
211 }
212
213 SYSCTL_PROC(_kern_skywalk_flowswitch, OID_AUTO, ip_reass,
214 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
215 0, 0, fsw_ip_reass_sysctl, "IU",
216 "adjust flowswitch IP reassembly");
217
218 #if (DEVELOPMENT || DEBUG)
219 static uint64_t _fsw_inject_error = 0;
220 #define _FSW_INJECT_ERROR(_en, _ev, _ec, _f, ...) \
221 _SK_INJECT_ERROR(_fsw_inject_error, _en, _ev, _ec, \
222 &FSW_STATS_VAL(_FSW_STATS_ERROR_INJECTIONS), _f, __VA_ARGS__)
223
224 #define _FSW_INJECT_ERROR_SET(_en, _f, ...) do { \
225 if (__improbable(((_fsw_inject_error) & (1ULL << (_en))) != 0)) { \
226 SK_DF(SK_VERB_ERROR_INJECT, "injecting error %d", (_en));\
227 if ((_f) != NULL) \
228 (_f)(__VA_ARGS__); \
229 } \
230 } while (0)
231
232 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, flow_owner_buckets,
233 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_flow_owner_buckets, 0, "");
234 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, fe_table_size,
235 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_fe_table_size, 0, "");
236 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, flow_route_buckets,
237 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_flow_route_buckets, 0, "");
238 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO,
239 flow_route_id_buckets, CTLFLAG_RW | CTLFLAG_LOCKED,
240 &fsw_flow_route_id_buckets, 0, "");
241 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, flow_reap_interval,
242 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_flow_reap_interval, 0, "");
243 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, rx_stall_thresh,
244 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_rx_stall_thresh, 0, "");
245 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, rx_stall_defunct,
246 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_rx_stall_defunct, 0, "");
247 SYSCTL_UINT(_kern_skywalk_flowswitch, OID_AUTO, flow_purge_thresh,
248 CTLFLAG_RW | CTLFLAG_LOCKED, &fsw_flow_purge_thresh, 0, "");
249 SYSCTL_QUAD(_kern_skywalk_flowswitch, OID_AUTO, fsw_inject_error,
250 CTLFLAG_RW | CTLFLAG_LOCKED, &_fsw_inject_error, "");
251 #else
252 #define _FSW_INJECT_ERROR(_en, _ev, _ec, _f, ...) do { } while (0)
253 #define _FSW_INJECT_ERROR_SET(_en, _f, ...) do { } while (0)
254 #endif /* !DEVELOPMENT && !DEBUG */
255
256 static void fsw_linger_remove_internal(struct flow_entry_linger_head *,
257 struct flow_entry *);
258 static void fsw_reap_thread_func(void *, wait_result_t);
259 static void fsw_reap_thread_cont(void *, wait_result_t);
260 static void fsw_purge_cache(struct nx_flowswitch *, boolean_t);
261 static void fsw_drain_channels(struct nx_flowswitch *, uint64_t, boolean_t);
262 static uint32_t fsw_process_deferred(struct nx_flowswitch *);
263 static uint32_t fsw_process_linger(struct nx_flowswitch *, uint32_t *);
264
265 static int copy_packet_from_dev(struct nx_flowswitch *, struct __kern_packet *,
266 struct __kern_packet *);
267
268 static void fsw_ifp_inc_traffic_class_in_pkt(struct ifnet *, kern_packet_t);
269 static void fsw_ifp_inc_traffic_class_out_pkt(struct ifnet *, uint32_t,
270 uint32_t, uint32_t);
271
272 static int __fsw_dp_inited = 0;
273
274 int
fsw_dp_init(void)275 fsw_dp_init(void)
276 {
277 _CASSERT(FSW_VP_DEV == 0);
278 _CASSERT(FSW_VP_HOST == 1);
279 _CASSERT((FSW_VP_HOST + FSW_VP_DEV) < FSW_VP_USER_MIN);
280 _CASSERT((FSW_VP_HOST + FSW_VP_DEV) < NEXUS_PORT_FLOW_SWITCH_CLIENT);
281
282 ASSERT(!__fsw_dp_inited);
283
284 flow_mgr_init();
285 flow_init();
286
287 __fsw_dp_inited = 1;
288
289 return 0;
290 }
291
292 void
fsw_dp_uninit(void)293 fsw_dp_uninit(void)
294 {
295 if (__fsw_dp_inited) {
296 flow_fini();
297 flow_mgr_fini();
298
299 __fsw_dp_inited = 0;
300 }
301 }
302
303 static void
dp_free_pktq(struct nx_flowswitch * fsw __sk_unused,struct pktq * pktq)304 dp_free_pktq(struct nx_flowswitch *fsw __sk_unused, struct pktq *pktq)
305 {
306 pp_free_pktq(pktq);
307 }
308
309 #define dp_drop_pktq(fsw, pktq, outgoing, _reason, line, _flags) do { \
310 uint32_t _len = KPKTQ_LEN(pktq); \
311 if (KPKTQ_EMPTY(pktq)) { \
312 ASSERT(_len == 0); \
313 return; \
314 } \
315 SK_DF(SK_VERB_FSW_DP | SK_VERB_DROP, "drop %d packets", _len); \
316 FSW_STATS_ADD(FSW_STATS_DROP, _len); \
317 DTRACE_SKYWALK1(fsw__dp__drop, int, _len); \
318 if (__probable(droptap_total_tap_count == 0)) { \
319 dp_free_pktq(fsw, pktq); \
320 break; \
321 } \
322 drop_func_t dropfunc; \
323 dropfunc = (outgoing) ? droptap_output_packet : droptap_input_packet; \
324 struct __kern_packet *kpkt = KPKTQ_FIRST(pktq); \
325 struct __kern_packet *next_pkt; \
326 for (; kpkt != NULL; kpkt = next_pkt) { \
327 next_pkt = kpkt->pkt_nextpkt; \
328 dropfunc(SK_PKT2PH(kpkt), _reason, __func__, line, _flags, \
329 fsw->fsw_ifp, kpkt->pkt_qum.qum_pid, NULL, -1, NULL, \
330 0, 0); \
331 } \
332 dp_free_pktq(fsw, pktq); \
333 } while (0)
334
335 #define dp_drop_pkt_single(fsw, pkt, outgoing, _reason, _flags) do { \
336 SK_DF(SK_VERB_FSW_DP | SK_VERB_DROP, "drop 1 packet"); \
337 FSW_STATS_ADD(FSW_STATS_DROP, 1); \
338 if (__probable(droptap_total_tap_count == 0)) { \
339 pp_free_packet_single(pkt); \
340 break; \
341 } \
342 drop_func_t dropfunc; \
343 dropfunc = (outgoing) ? droptap_output_packet : droptap_input_packet; \
344 dropfunc(SK_PKT2PH(pkt), _reason, __func__, __LINE__, _flags, \
345 fsw->fsw_ifp, (pkt)->pkt_qum.qum_pid, NULL, -1, NULL, 0, 0); \
346 pp_free_packet_single(pkt); \
347 } while (0)
348
349 #define dp_drop_pkt_chain(pkt, outgoing, _reason, _flags) do { \
350 if (__probable(droptap_total_tap_count == 0)) { \
351 pp_free_packet_chain(pkt, NULL); \
352 break; \
353 } \
354 drop_func_t dropfunc; \
355 dropfunc = (outgoing) ? droptap_output_packet : droptap_input_packet; \
356 struct __kern_packet *next_pkt; \
357 for (; pkt != NULL; pkt = next_pkt) { \
358 next_pkt = pkt->pkt_nextpkt; \
359 dropfunc(SK_PKT2PH(pkt), _reason, __func__, __LINE__, _flags, \
360 NULL, pkt->pkt_qum.qum_pid, NULL, -1, NULL, \
361 0, 0); \
362 } \
363 pp_free_packet_chain(pkt, NULL); \
364 } while (0)
365
366
367 SK_NO_INLINE_ATTRIBUTE
368 void
fsw_snoop(struct nx_flowswitch * fsw,struct flow_entry * fe,struct pktq * pktq,bool input)369 fsw_snoop(struct nx_flowswitch *fsw, struct flow_entry *fe, struct pktq *pktq,
370 bool input)
371 {
372 pid_t pid;
373 char proc_name_buf[FLOW_PROCESS_NAME_LENGTH];
374 const char *__null_terminated proc_name = NULL;
375 pid_t epid;
376 char eproc_name_buf[FLOW_PROCESS_NAME_LENGTH];
377 const char *__null_terminated eproc_name = NULL;
378 sa_family_t af;
379 bool tap_early = false;
380 struct __kern_packet *pkt;
381
382 ASSERT(fe != NULL);
383 ASSERT(fsw->fsw_ifp != NULL);
384
385 if (fe->fe_nx_port == FSW_VP_HOST) {
386 /* allow packets to be tapped before aggregation happens */
387 tap_early = (input && fe->fe_key.fk_proto == IPPROTO_TCP);
388 if (!tap_early) {
389 /* all other traffic will be tapped in the dlil input path */
390 return;
391 }
392 }
393 if (fe->fe_key.fk_ipver == IPVERSION) {
394 af = AF_INET;
395 } else if (fe->fe_key.fk_ipver == IPV6_VERSION) {
396 af = AF_INET6;
397 } else {
398 return;
399 }
400
401 pid = fe->fe_pid;
402 if (fe->fe_proc_name[0] != '\0') {
403 proc_name = strbufcpy(proc_name_buf, sizeof(proc_name_buf),
404 fe->fe_proc_name, sizeof(fe->fe_proc_name));
405 }
406 epid = fe->fe_epid;
407 if (fe->fe_eproc_name[0] != '\0') {
408 eproc_name = strbufcpy(eproc_name_buf, sizeof(eproc_name_buf),
409 fe->fe_eproc_name, sizeof(fe->fe_eproc_name));
410 }
411 if (input) {
412 KPKTQ_FOREACH(pkt, pktq) {
413 pktap_input_packet(fsw->fsw_ifp, af,
414 fsw->fsw_ifp_dlt, pid, proc_name, epid,
415 eproc_name, SK_PKT2PH(pkt), NULL, 0,
416 IPPROTO_TCP, fe->fe_flowid,
417 tap_early ? PTH_FLAG_SOCKET: PTH_FLAG_NEXUS_CHAN);
418 }
419 } else {
420 KPKTQ_FOREACH(pkt, pktq) {
421 pktap_output_packet(fsw->fsw_ifp, af,
422 fsw->fsw_ifp_dlt, pid, proc_name, epid,
423 eproc_name, SK_PKT2PH(pkt), NULL, 0,
424 0, 0, PTH_FLAG_NEXUS_CHAN);
425 }
426 }
427 }
428
429 #if (DEVELOPMENT || DEBUG)
430 static void
_fsw_error35_handler(int step,struct flow_route * fr,struct __kern_packet * pkt,int * ret)431 _fsw_error35_handler(int step, struct flow_route *fr, struct __kern_packet *pkt,
432 int *ret)
433 {
434 static boolean_t _err35_flag_modified = FALSE;
435
436 switch (step) {
437 case 1:
438 if ((fr->fr_flags & (FLOWRTF_RESOLVED | FLOWRTF_HAS_LLINFO)) ==
439 (FLOWRTF_RESOLVED | FLOWRTF_HAS_LLINFO)) {
440 fr->fr_flags &= ~FLOWRTF_RESOLVED;
441 _err35_flag_modified = TRUE;
442 }
443 break;
444
445 case 2:
446 if (!_err35_flag_modified) {
447 return;
448 }
449 if (pkt->pkt_pflags & PKT_F_MBUF_DATA) {
450 m_freem(pkt->pkt_mbuf);
451 pkt->pkt_pflags &= ~PKT_F_MBUF_DATA;
452 pkt->pkt_mbuf = NULL;
453 }
454 *ret = EJUSTRETURN;
455 fr->fr_flags |= FLOWRTF_RESOLVED;
456 _err35_flag_modified = FALSE;
457 break;
458
459 default:
460 VERIFY(0);
461 /* not reached */
462 }
463 }
464
465 static void
_fsw_error36_handler(int step,struct flow_route * fr,int * ret)466 _fsw_error36_handler(int step, struct flow_route *fr, int *ret)
467 {
468 static boolean_t _err36_flag_modified = FALSE;
469
470 switch (step) {
471 case 1:
472 if ((fr->fr_flags & (FLOWRTF_RESOLVED | FLOWRTF_HAS_LLINFO)) ==
473 (FLOWRTF_RESOLVED | FLOWRTF_HAS_LLINFO)) {
474 fr->fr_flags &= ~FLOWRTF_RESOLVED;
475 _err36_flag_modified = TRUE;
476 }
477 break;
478
479 case 2:
480 if (!_err36_flag_modified) {
481 return;
482 }
483 *ret = ENETUNREACH;
484 fr->fr_flags |= FLOWRTF_RESOLVED;
485 _err36_flag_modified = FALSE;
486 break;
487
488 default:
489 VERIFY(0);
490 /* not reached */
491 }
492 }
493 #else /* !DEVELOPMENT && !DEBUG */
494 #define _fsw_error35_handler(...)
495 #define _fsw_error36_handler(...)
496 #endif /* DEVELOPMENT || DEBUG */
497
498 /*
499 * Check if the source packet content can fit into the destination
500 * ring's packet. Returns TRUE if the source packet can fit.
501 * Note: Failures could be caused by misconfigured packet pool sizes,
502 * missing packet size check again MTU or if the source packet is from
503 * a compat netif and the attached mbuf is larger than MTU due to LRO.
504 */
505 static inline boolean_t
validate_pkt_len(struct __kern_packet * spkt,kern_packet_t dph,uint32_t skip_l2hlen,uint32_t l2hlen,uint16_t headroom,uint32_t * copy_len)506 validate_pkt_len(struct __kern_packet *spkt, kern_packet_t dph,
507 uint32_t skip_l2hlen, uint32_t l2hlen, uint16_t headroom,
508 uint32_t *copy_len)
509 {
510 uint32_t tlen = 0;
511 uint32_t splen = spkt->pkt_length - skip_l2hlen;
512
513 if (l2hlen != 0) {
514 VERIFY(skip_l2hlen == 0);
515 tlen += l2hlen;
516 } else if ((spkt->pkt_link_flags & PKT_LINKF_ETHFCS) != 0) {
517 splen -= ETHER_CRC_LEN;
518 }
519
520 tlen += splen;
521 *copy_len = splen;
522
523 return tlen <= ((__packet_get_buflet_count(dph) *
524 PP_BUF_SIZE_DEF(SK_PTR_ADDR_KPKT(dph)->pkt_qum.qum_pp)) -
525 headroom);
526 }
527
528 #if SK_LOG
529 /* Hoisted out of line to reduce kernel stack footprint */
530 SK_LOG_ATTRIBUTE
531 static void
copy_packet_from_dev_log(struct __kern_packet * spkt,struct __kern_packet * dpkt,struct proc * p)532 copy_packet_from_dev_log(struct __kern_packet *spkt,
533 struct __kern_packet *dpkt, struct proc *p)
534 {
535 uint64_t logflags = ((SK_VERB_FSW | SK_VERB_RX) |
536 ((spkt->pkt_pflags & PKT_F_MBUF_DATA) ?
537 SK_VERB_COPY_MBUF : SK_VERB_COPY));
538 char *daddr;
539 uint32_t pkt_len;
540
541 MD_BUFLET_ADDR_ABS(dpkt, daddr);
542 pkt_len = __packet_get_real_data_length(dpkt);
543 SK_DF(logflags, "%s(%d) splen %u dplen %u hr %u l2 %u",
544 sk_proc_name_address(p), sk_proc_pid(p), spkt->pkt_length,
545 dpkt->pkt_length, (uint32_t)dpkt->pkt_headroom,
546 (uint32_t)dpkt->pkt_l2_len);
547 SK_DF(logflags | SK_VERB_DUMP, "%s",
548 sk_dump("buf", daddr, pkt_len, 128, NULL, 0));
549 }
550 #else
551 #define copy_packet_from_dev_log(...)
552 #endif /* SK_LOG */
553
554
555 static inline int
copy_packet_from_dev(struct nx_flowswitch * fsw,struct __kern_packet * spkt,struct __kern_packet * dpkt)556 copy_packet_from_dev(struct nx_flowswitch *fsw, struct __kern_packet *spkt,
557 struct __kern_packet *dpkt)
558 {
559 /*
560 * source and destination nexus don't share the packet pool
561 * sync operation here is to
562 * - alloc packet for the rx(dst) ring
563 * - copy data/metadata from src packet to dst packet
564 * - attach alloc'd packet to rx(dst) ring
565 */
566 kern_packet_t dph = SK_PTR_ENCODE(dpkt,
567 METADATA_TYPE(dpkt), METADATA_SUBTYPE(dpkt));
568 kern_packet_t sph = SK_PTR_ENCODE(spkt, METADATA_TYPE(spkt),
569 METADATA_SUBTYPE(spkt));
570 boolean_t do_cksum_rx;
571 uint16_t skip_l2h_len = spkt->pkt_l2_len;
572 uint16_t iphlen;
573 uint32_t dlen;
574 int err;
575
576 if (__improbable(!validate_pkt_len(spkt, dph, skip_l2h_len, 0, 0,
577 &dlen))) {
578 SK_ERR("bufcnt %d, bufsz %d", __packet_get_buflet_count(dph),
579 PP_BUF_SIZE_DEF(dpkt->pkt_qum.qum_pp));
580 FSW_STATS_INC(FSW_STATS_RX_COPY_BAD_LEN);
581 return EINVAL;
582 }
583
584 /* Copy packet metadata */
585 _QUM_COPY(&(spkt)->pkt_qum, &(dpkt)->pkt_qum);
586 _PKT_COPY(spkt, dpkt);
587 ASSERT(!(dpkt->pkt_qum.qum_qflags & QUM_F_KERNEL_ONLY) ||
588 PP_KERNEL_ONLY(dpkt->pkt_qum.qum_pp));
589 ASSERT(dpkt->pkt_mbuf == NULL);
590
591 dpkt->pkt_headroom = 0;
592 dpkt->pkt_l2_len = 0;
593
594 /* don't include IP header from partial sum */
595 if (__probable((spkt->pkt_qum_qflags & QUM_F_FLOW_CLASSIFIED) != 0)) {
596 iphlen = spkt->pkt_flow_ip_hlen;
597 do_cksum_rx = sk_cksum_rx;
598 } else {
599 iphlen = 0;
600 do_cksum_rx = FALSE;
601 }
602
603 /* Copy packet payload */
604 if ((spkt->pkt_pflags & PKT_F_MBUF_DATA) &&
605 (spkt->pkt_pflags & PKT_F_TRUNCATED)) {
606 FSW_STATS_INC(FSW_STATS_RX_COPY_MBUF2PKT);
607 /*
608 * Source packet has truncated contents (just enough for
609 * the classifer) of an mbuf from the compat driver; copy
610 * the entire entire mbuf contents to destination packet.
611 */
612 m_adj(spkt->pkt_mbuf, skip_l2h_len);
613 ASSERT((uint32_t)m_pktlen(spkt->pkt_mbuf) >= dlen);
614 fsw->fsw_pkt_copy_from_mbuf(NR_RX, dph, 0,
615 spkt->pkt_mbuf, 0, dlen, do_cksum_rx, iphlen);
616 } else {
617 FSW_STATS_INC(FSW_STATS_RX_COPY_PKT2PKT);
618 /*
619 * Source packet has full contents, either from an mbuf
620 * that came up from the compat driver, or because it
621 * originated on the native driver; copy to destination.
622 */
623 fsw->fsw_pkt_copy_from_pkt(NR_RX, dph, 0, sph,
624 (spkt->pkt_headroom + spkt->pkt_l2_len), dlen, do_cksum_rx,
625 iphlen, 0, FALSE);
626 }
627
628 #if DEBUG || DEVELOPMENT
629 if (__improbable(pkt_trailers > 0)) {
630 dlen += pkt_add_trailers(dph, dlen, iphlen);
631 }
632 #endif /* DEBUG || DEVELOPMENT */
633
634 /* Finalize and attach packet to Rx ring */
635 METADATA_ADJUST_LEN(dpkt, 0, 0);
636 err = __packet_finalize(dph);
637 VERIFY(err == 0);
638
639 copy_packet_from_dev_log(spkt, dpkt, kernproc);
640
641 if (spkt->pkt_pflags & PKT_F_MBUF_DATA) {
642 ifp_inc_traffic_class_in(fsw->fsw_ifp, spkt->pkt_mbuf);
643 mbuf_free(spkt->pkt_mbuf);
644 KPKT_CLEAR_MBUF_DATA(spkt);
645 } else {
646 fsw_ifp_inc_traffic_class_in_pkt(fsw->fsw_ifp, dph);
647 }
648
649 if (__probable(do_cksum_rx != 0)) {
650 FSW_STATS_INC(FSW_STATS_RX_COPY_SUM);
651 }
652
653 return 0;
654 }
655
656 SK_NO_INLINE_ATTRIBUTE
657 static struct __kern_packet *
rx_process_ip_frag(struct nx_flowswitch * fsw,struct __kern_packet * pkt)658 rx_process_ip_frag(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
659 {
660 char *pkt_buf;
661 void *l3_hdr;
662 uint16_t nfrags, tlen;
663 int err = 0;
664
665 switch (fsw_ip_reass) {
666 case FSW_IP_REASS_FORCE_OFF:
667 return pkt;
668 case FSW_IP_REASS_FORCE_ON:
669 break;
670 default:
671 if (!FSW_NETAGENT_ENABLED(fsw) ||
672 flow_mgr_get_num_flows(fsw->fsw_flow_mgr) == 0) {
673 return pkt;
674 }
675 break;
676 }
677
678 MD_BUFLET_ADDR_ABS(pkt, pkt_buf);
679 l3_hdr = pkt_buf + pkt->pkt_headroom + pkt->pkt_l2_len;
680
681 ASSERT(fsw->fsw_ipfm != NULL);
682 ASSERT((pkt->pkt_qum_qflags & QUM_F_FLOW_CLASSIFIED) != 0);
683
684 if (pkt->pkt_flow_ip_ver == IPVERSION) {
685 struct ip *ip = l3_hdr;
686 err = fsw_ip_frag_reass_v4(fsw->fsw_ipfm, &pkt, ip, &nfrags, &tlen);
687 } else {
688 struct ip6_hdr *ip6_hdr = l3_hdr;
689 struct ip6_frag *__single ip6_frag =
690 (struct ip6_frag *)((uint8_t *)l3_hdr + sizeof(struct ip6_hdr));
691
692 ASSERT(pkt->pkt_flow_ip_ver == IPV6_VERSION);
693 /* we only handle frag header immediately after v6 header */
694 err = fsw_ip_frag_reass_v6(fsw->fsw_ipfm, &pkt, ip6_hdr, ip6_frag,
695 &nfrags, &tlen);
696 }
697 if (__improbable(err != 0)) {
698 /* if we get a bad fragment, free it */
699 pp_free_packet_single(pkt);
700 pkt = NULL;
701 } else {
702 ASSERT(!((pkt != NULL) ^ (nfrags > 0)));
703 }
704
705 return pkt;
706 }
707
708 SK_NO_INLINE_ATTRIBUTE
709 static void
rx_prepare_packet_mbuf(struct nx_flowswitch * fsw,struct __kern_packet * pkt)710 rx_prepare_packet_mbuf(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
711 {
712 ASSERT(pkt->pkt_pflags & PKT_F_MBUF_DATA);
713 uint32_t mlen = (uint32_t)m_pktlen(pkt->pkt_mbuf);
714 kern_packet_t ph = SK_PTR_ENCODE(pkt,
715 METADATA_TYPE(pkt), METADATA_SUBTYPE(pkt));
716 /*
717 * This is the case when the packet is coming in from
718 * compat-netif. This packet only has valid metadata
719 * and an attached mbuf. We need to copy enough data
720 * from the mbuf to the packet buffer for the
721 * classifier. Compat netif packet pool is configured
722 * with buffer size of NETIF_COMPAT_MAX_MBUF_DATA_COPY
723 * which is just enough to hold the protocol headers
724 * for the flowswitch classifier.
725 */
726
727 pkt->pkt_headroom = 0;
728 METADATA_ADJUST_LEN(pkt, 0, 0);
729 /*
730 * Copy the initial 128 bytes of the packet for
731 * classification.
732 * Ethernet(14) + IPv6 header(40) +
733 * + IPv6 fragment header(8) +
734 * TCP header with options(60).
735 */
736 fsw->fsw_pkt_copy_from_mbuf(NR_RX, ph,
737 pkt->pkt_headroom, pkt->pkt_mbuf, 0,
738 MIN(mlen, NETIF_COMPAT_MAX_MBUF_DATA_COPY),
739 FALSE, 0);
740
741 int err = __packet_finalize_with_mbuf(pkt);
742 VERIFY(err == 0);
743 }
744
745 static struct __kern_packet *
rx_prepare_packet(struct nx_flowswitch * fsw,struct __kern_packet * pkt)746 rx_prepare_packet(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
747 {
748 pkt->pkt_qum_qflags &= ~QUM_F_FLOW_CLASSIFIED;
749
750 if (__improbable(pkt->pkt_pflags & PKT_F_MBUF_DATA)) {
751 rx_prepare_packet_mbuf(fsw, pkt);
752 }
753
754 return pkt;
755 }
756
757 static struct flow_entry *
lookup_flow_with_pkt(struct nx_flowswitch * fsw,struct __kern_packet * pkt,bool input,struct flow_entry * prev_fe)758 lookup_flow_with_pkt(struct nx_flowswitch *fsw, struct __kern_packet *pkt,
759 bool input, struct flow_entry *prev_fe)
760 {
761 struct flow_key key __sk_aligned(16);
762 struct flow_entry *__single fe = NULL;
763
764 ASSERT(pkt->pkt_qum_qflags & QUM_F_FLOW_CLASSIFIED);
765 flow_pkt2key(pkt, input, &key);
766
767 if (__probable(prev_fe != NULL &&
768 prev_fe->fe_key.fk_mask == FKMASK_5TUPLE)) {
769 uint16_t saved_mask = key.fk_mask;
770 key.fk_mask = FKMASK_5TUPLE;
771 if (flow_key_cmp_mask(&prev_fe->fe_key, &key, &fk_mask_5tuple) == 0) {
772 flow_entry_retain(prev_fe);
773 fe = prev_fe;
774 } else {
775 key.fk_mask = saved_mask;
776 }
777 }
778
779 top:
780 if (__improbable(fe == NULL)) {
781 fe = flow_mgr_find_fe_by_key(fsw->fsw_flow_mgr, &key);
782 }
783
784 if (__improbable(fe != NULL &&
785 (fe->fe_flags & (FLOWENTF_PARENT | FLOWENTF_CHILD)) != 0)) {
786 /* Rx */
787 if (input) {
788 if (fe->fe_flags & FLOWENTF_PARENT) {
789 struct flow_entry *child_fe = rx_lookup_child_flow(fsw, fe, pkt);
790 if (child_fe != NULL) {
791 flow_entry_release(&fe);
792 fe = child_fe;
793 }
794 } else {
795 if (!rx_flow_demux_match(fsw, fe, pkt)) {
796 flow_entry_release(&fe);
797 fe = NULL;
798 goto top;
799 }
800 }
801 } else {
802 /* Tx */
803 if (__improbable(!_UUID_MATCH(pkt->pkt_flow_id, fe->fe_uuid))) {
804 if (__probable(fe->fe_flags & FLOWENTF_PARENT)) {
805 struct flow_entry *__single parent_fe = fe;
806 fe = tx_lookup_child_flow(parent_fe, pkt->pkt_flow_id);
807 flow_entry_release(&parent_fe);
808 } else {
809 flow_entry_release(&fe);
810 fe = NULL;
811 goto top;
812 }
813 }
814 }
815 }
816
817 SK_LOG_VAR(char fkbuf[FLOWKEY_DBGBUF_SIZE]);
818 SK_DF(SK_VERB_FSW_DP | SK_VERB_LOOKUP,
819 "%s %s %s \"%s\" fe 0x%llx",
820 input ? "Rx" : "Tx", if_name(fsw->fsw_ifp),
821 sk_proc_name_address(current_proc()),
822 fk_as_string(&key, fkbuf, sizeof(fkbuf)),
823 SK_KVA(fe));
824
825 return fe;
826 }
827
828 SK_NO_INLINE_ATTRIBUTE
829 static bool
pkt_is_for_listener(struct flow_entry * fe,struct __kern_packet * pkt)830 pkt_is_for_listener(struct flow_entry *fe, struct __kern_packet *pkt)
831 {
832 struct nx_flowswitch *fsw = fe->fe_fsw;
833 struct ifnet *ifp = fsw->fsw_ifp;
834 struct in_ifaddr *ia = NULL;
835 struct in_ifaddr *best_ia = NULL;
836 struct in6_ifaddr *ia6 = NULL;
837 struct in6_ifaddr *best_ia6 = NULL;
838 struct ifnet *match_ifp = NULL;
839 struct __flow *flow = pkt->pkt_flow;
840 bool result = false;
841
842 ASSERT(pkt->pkt_qum_qflags & QUM_F_FLOW_CLASSIFIED);
843
844 if (flow->flow_ip_ver == IPVERSION) {
845 if (IN_ZERONET(ntohl(flow->flow_ipv4_dst.s_addr)) ||
846 IN_LOOPBACK(ntohl(flow->flow_ipv4_dst.s_addr)) ||
847 IN_LINKLOCAL(ntohl(flow->flow_ipv4_dst.s_addr)) ||
848 IN_DS_LITE(ntohl(flow->flow_ipv4_dst.s_addr)) ||
849 IN_6TO4_RELAY_ANYCAST(ntohl(flow->flow_ipv4_dst.s_addr)) ||
850 IN_MULTICAST(ntohl(flow->flow_ipv4_dst.s_addr)) ||
851 INADDR_BROADCAST == flow->flow_ipv4_dst.s_addr) {
852 result = true;
853 goto done;
854 }
855
856 /*
857 * Check for a match in the hash bucket.
858 */
859 lck_rw_lock_shared(&in_ifaddr_rwlock);
860 TAILQ_FOREACH(ia, INADDR_HASH(flow->flow_ipv4_dst.s_addr), ia_hash) {
861 if (IA_SIN(ia)->sin_addr.s_addr == flow->flow_ipv4_dst.s_addr) {
862 best_ia = ia;
863 match_ifp = ia->ia_ifp;
864
865 if (match_ifp == ifp) {
866 break;
867 }
868 /*
869 * Continue the loop in case there's a exact match with another
870 * interface
871 */
872 }
873 }
874
875 if (best_ia != NULL) {
876 if (match_ifp != ifp && ipforwarding == 0 &&
877 (match_ifp->if_family == IFNET_FAMILY_IPSEC ||
878 match_ifp->if_family == IFNET_FAMILY_UTUN)) {
879 /*
880 * Drop when interface address check is strict and forwarding
881 * is disabled
882 */
883 } else {
884 lck_rw_done(&in_ifaddr_rwlock);
885 result = true;
886 goto done;
887 }
888 }
889 lck_rw_done(&in_ifaddr_rwlock);
890
891 if (ifp->if_flags & IFF_BROADCAST) {
892 /*
893 * Check for broadcast addresses.
894 *
895 * Only accept broadcast packets that arrive via the matching
896 * interface. Reception of forwarded directed broadcasts would be
897 * handled via ip_forward() and ether_frameout() with the loopback
898 * into the stack for SIMPLEX interfaces handled by ether_frameout().
899 */
900 struct ifaddr *ifa;
901
902 ifnet_lock_shared(ifp);
903 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
904 if (ifa->ifa_addr->sa_family != AF_INET) {
905 continue;
906 }
907 ia = ifatoia(ifa);
908 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == flow->flow_ipv4_dst.s_addr ||
909 ia->ia_netbroadcast.s_addr == flow->flow_ipv4_dst.s_addr) {
910 ifnet_lock_done(ifp);
911 result = true;
912 goto done;
913 }
914 }
915 ifnet_lock_done(ifp);
916 }
917 } else {
918 struct in6_ifaddrhashhead *ia6_hash_head;
919
920 if (IN6_IS_ADDR_LOOPBACK(&flow->flow_ipv6_dst) ||
921 IN6_IS_ADDR_LINKLOCAL(&flow->flow_ipv6_dst) ||
922 IN6_IS_ADDR_MULTICAST(&flow->flow_ipv6_dst)) {
923 result = true;
924 goto done;
925 }
926
927 /*
928 * Check for exact addresses in the hash bucket.
929 */
930 lck_rw_lock_shared(&in6_ifaddr_rwlock);
931 /* XXX -fbounds-safety: external dependency on ip6_input.c */
932 ia6_hash_head = __unsafe_forge_bidi_indexable(struct in6_ifaddrhashhead *,
933 in6_ifaddrhashtbl, in6addr_nhash * sizeof(*in6_ifaddrhashtbl));
934 ia6_hash_head = &ia6_hash_head[in6addr_hashval(&flow->flow_ipv6_dst)];
935
936 TAILQ_FOREACH(ia6, ia6_hash_head, ia6_hash) {
937 if (in6_are_addr_equal_scoped(&ia6->ia_addr.sin6_addr, &flow->flow_ipv6_dst,
938 ia6->ia_ifp->if_index, ifp->if_index)) {
939 if ((ia6->ia6_flags & (IN6_IFF_NOTREADY | IN6_IFF_CLAT46))) {
940 continue;
941 }
942 best_ia6 = ia6;
943 if (ia6->ia_ifp == ifp) {
944 break;
945 }
946 /*
947 * Continue the loop in case there's a exact match with another
948 * interface
949 */
950 }
951 }
952 if (best_ia6 != NULL) {
953 if (best_ia6->ia_ifp != ifp && ip6_forwarding == 0 &&
954 (best_ia6->ia_ifp->if_family == IFNET_FAMILY_IPSEC ||
955 best_ia6->ia_ifp->if_family == IFNET_FAMILY_UTUN)) {
956 /*
957 * Drop when interface address check is strict and forwarding
958 * is disabled
959 */
960 } else {
961 lck_rw_done(&in6_ifaddr_rwlock);
962 result = true;
963 goto done;
964 }
965 }
966 lck_rw_done(&in6_ifaddr_rwlock);
967 }
968
969 /*
970 * In forwarding mode, if the destination address
971 * of the packet does not match any interface
972 * address, it maybe destined to the client device
973 */
974 SK_DF(SK_VERB_FSW_DP | SK_VERB_RX | SK_VERB_FLOW,
975 "Rx flow does not match interface address");
976 done:
977 return result;
978 }
979
980 static struct flow_entry *
rx_lookup_flow(struct nx_flowswitch * fsw,struct __kern_packet * pkt,struct flow_entry * prev_fe)981 rx_lookup_flow(struct nx_flowswitch *fsw, struct __kern_packet *pkt,
982 struct flow_entry *prev_fe)
983 {
984 struct flow_entry *__single fe;
985
986 fe = lookup_flow_with_pkt(fsw, pkt, true, prev_fe);
987 _FSW_INJECT_ERROR(2, fe, NULL, flow_entry_release, &fe);
988 if (fe == NULL) {
989 FSW_STATS_INC(FSW_STATS_RX_FLOW_NOT_FOUND);
990 return NULL;
991 }
992
993 if (__improbable(fe->fe_key.fk_mask == FKMASK_2TUPLE &&
994 fe->fe_flags & FLOWENTF_LISTENER) &&
995 !pkt_is_for_listener(fe, pkt)) {
996 FSW_STATS_INC(FSW_STATS_RX_PKT_NOT_LISTENER);
997 flow_entry_release(&fe);
998 return NULL;
999 }
1000
1001 if (__improbable(fe->fe_flags & FLOWENTF_TORN_DOWN)) {
1002 FSW_STATS_INC(FSW_STATS_RX_FLOW_TORNDOWN);
1003 SK_DF(SK_VERB_FSW_DP | SK_VERB_RX | SK_VERB_FLOW,
1004 "Rx flow torn down");
1005 flow_entry_release(&fe);
1006 fe = NULL;
1007 }
1008
1009 return fe;
1010 }
1011
1012 static inline void
rx_flow_batch_packets(struct flow_entry_list * fes,struct flow_entry * fe,struct __kern_packet * pkt,uint64_t tid)1013 rx_flow_batch_packets(struct flow_entry_list *fes, struct flow_entry *fe,
1014 struct __kern_packet *pkt, uint64_t tid)
1015 {
1016 /*
1017 * Among threads working on the same fe, the first thread that reaches here
1018 * will be responsible for processing all the packets until a point when
1019 * it does not see new packets in fe_rx_pktq. Other threads only
1020 * enqueue their packets but do not add the flow entry to their flow entry list.
1021 */
1022 lck_mtx_lock(&fe->fe_rx_pktq_lock);
1023
1024 if (fe->fe_rx_worker_tid == 0) {
1025 fe->fe_rx_worker_tid = tid;
1026 } else if (__improbable(fe->fe_rx_worker_tid != tid)) {
1027 STATS_INC(&fe->fe_fsw->fsw_stats, FSW_STATS_RX_FLOW_IN_USE);
1028 }
1029
1030 if (__improbable(pkt->pkt_flow_ip_is_frag)) {
1031 fe->fe_rx_frag_count++;
1032 }
1033
1034 fe->fe_rx_pktq_bytes += pkt->pkt_flow_ulen;
1035 /* KPKTQ_ENQUEUE_LIST is needed until frags become chained buflet */
1036 if (KPKTQ_EMPTY(&fe->fe_rx_pktq) && tid == fe->fe_rx_worker_tid) {
1037 ASSERT(KPKTQ_LEN(&fe->fe_rx_pktq) == 0);
1038 TAILQ_INSERT_TAIL(fes, fe, fe_rx_link);
1039 KPKTQ_ENQUEUE_LIST(&fe->fe_rx_pktq, pkt);
1040 lck_mtx_unlock(&fe->fe_rx_pktq_lock);
1041 } else {
1042 KPKTQ_ENQUEUE_LIST(&fe->fe_rx_pktq, pkt);
1043 lck_mtx_unlock(&fe->fe_rx_pktq_lock);
1044 flow_entry_release(&fe);
1045 }
1046 }
1047
1048 static void
tx_flow_batch_packet(struct flow_entry_list * fes,struct flow_entry * fe,struct __kern_packet * pkt)1049 tx_flow_batch_packet(struct flow_entry_list *fes, struct flow_entry *fe,
1050 struct __kern_packet *pkt)
1051 {
1052 /* record frag continuation */
1053 if (__improbable(pkt->pkt_flow_ip_is_first_frag)) {
1054 ASSERT(pkt->pkt_flow_ip_is_frag);
1055 fe->fe_tx_is_cont_frag = true;
1056 fe->fe_tx_frag_id = pkt->pkt_flow_ip_frag_id;
1057 } else if (__probable(!pkt->pkt_flow_ip_is_frag)) {
1058 fe->fe_tx_is_cont_frag = false;
1059 fe->fe_tx_frag_id = 0;
1060 }
1061
1062 if (KPKTQ_EMPTY(&fe->fe_tx_pktq)) {
1063 ASSERT(KPKTQ_LEN(&fe->fe_tx_pktq) == 0);
1064 TAILQ_INSERT_TAIL(fes, fe, fe_tx_link);
1065 KPKTQ_ENQUEUE(&fe->fe_tx_pktq, pkt);
1066 } else {
1067 ASSERT(!TAILQ_EMPTY(fes));
1068 KPKTQ_ENQUEUE(&fe->fe_tx_pktq, pkt);
1069 flow_entry_release(&fe);
1070 }
1071 }
1072
1073 static inline void
fsw_rx_ring_dequeue_pktq(struct nx_flowswitch * fsw,struct __kern_channel_ring * r,uint32_t n_pkts_max,struct pktq * pktq,uint32_t * n_bytes)1074 fsw_rx_ring_dequeue_pktq(struct nx_flowswitch *fsw, struct __kern_channel_ring *r,
1075 uint32_t n_pkts_max, struct pktq *pktq, uint32_t *n_bytes)
1076 {
1077 uint32_t n_pkts = 0;
1078 slot_idx_t idx, idx_end;
1079 idx = r->ckr_khead;
1080 idx_end = r->ckr_rhead;
1081
1082 ASSERT(KPKTQ_EMPTY(pktq));
1083 *n_bytes = 0;
1084 for (; n_pkts < n_pkts_max && idx != idx_end;
1085 idx = SLOT_NEXT(idx, r->ckr_lim)) {
1086 struct __kern_slot_desc *ksd = KR_KSD(r, idx);
1087 struct __kern_packet *pkt = ksd->sd_pkt;
1088
1089 ASSERT(pkt->pkt_nextpkt == NULL);
1090 KR_SLOT_DETACH_METADATA(r, ksd);
1091
1092 _FSW_INJECT_ERROR(20, pkt->pkt_qum_qflags,
1093 pkt->pkt_qum_qflags | QUM_F_DROPPED, null_func);
1094 if (__improbable(((pkt->pkt_qum_qflags & QUM_F_DROPPED) != 0))
1095 || (pkt->pkt_length == 0)) {
1096 FSW_STATS_INC(FSW_STATS_DROP);
1097 pp_free_packet_single(pkt);
1098 continue;
1099 }
1100 n_pkts++;
1101 *n_bytes += pkt->pkt_length;
1102
1103 KPKTQ_ENQUEUE(pktq, pkt);
1104 }
1105 r->ckr_khead = idx;
1106 r->ckr_ktail = SLOT_PREV(idx, r->ckr_lim);
1107 }
1108
1109 /*
1110 * This is only for estimating how many packets each GSO packet will need.
1111 * The number does not need to be exact because any leftover packets allocated
1112 * will be freed.
1113 */
1114 static uint32_t
estimate_gso_pkts(struct __kern_packet * pkt)1115 estimate_gso_pkts(struct __kern_packet *pkt)
1116 {
1117 packet_tso_flags_t tso_flags;
1118 uint16_t mss;
1119 uint32_t n_pkts = 0, total_hlen = 0, total_len = 0;
1120
1121 tso_flags = pkt->pkt_csum_flags & PACKET_CSUM_TSO_FLAGS;
1122 mss = pkt->pkt_proto_seg_sz;
1123
1124 if (tso_flags == PACKET_TSO_IPV4) {
1125 total_hlen = sizeof(struct ip) + sizeof(struct tcphdr);
1126 } else if (tso_flags == PACKET_TSO_IPV6) {
1127 total_hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1128 }
1129 if (total_hlen != 0 && mss != 0) {
1130 total_len = pkt->pkt_length;
1131 n_pkts = (uint32_t)
1132 (SK_ROUNDUP((total_len - total_hlen), mss) / mss);
1133 }
1134 DTRACE_SKYWALK5(estimate__gso, packet_tso_flags_t, tso_flags,
1135 uint32_t, total_hlen, uint32_t, total_len, uint16_t, mss,
1136 uint32_t, n_pkts);
1137 return n_pkts;
1138 }
1139
1140 /*
1141 * This function retrieves a chain of packets of the same type only
1142 * (GSO or non-GSO).
1143 */
1144 static inline void
fsw_tx_ring_dequeue_pktq(struct nx_flowswitch * fsw,struct __kern_channel_ring * r,uint32_t n_pkts_max,struct pktq * pktq,uint32_t * n_bytes,uint32_t * gso_pkts_estimate)1145 fsw_tx_ring_dequeue_pktq(struct nx_flowswitch *fsw,
1146 struct __kern_channel_ring *r, uint32_t n_pkts_max,
1147 struct pktq *pktq, uint32_t *n_bytes, uint32_t *gso_pkts_estimate)
1148 {
1149 uint32_t n_pkts = 0;
1150 slot_idx_t idx, idx_end;
1151 idx = r->ckr_khead;
1152 idx_end = r->ckr_rhead;
1153 struct nexus_vp_adapter *vpna = VPNA(KRNA(r));
1154 boolean_t gso_enabled, gso_required;
1155 uint32_t gso_pkts;
1156
1157 gso_enabled = (fsw->fsw_tso_mode == FSW_TSO_MODE_SW);
1158 ASSERT(KPKTQ_EMPTY(pktq));
1159 *n_bytes = 0;
1160 for (; n_pkts < n_pkts_max &&
1161 (!gso_enabled || fsw_gso_batch == 0 ||
1162 *gso_pkts_estimate < fsw_gso_batch) &&
1163 idx != idx_end; idx = SLOT_NEXT(idx, r->ckr_lim)) {
1164 struct __kern_slot_desc *ksd = KR_KSD(r, idx);
1165 struct __kern_packet *pkt = ksd->sd_pkt;
1166
1167 ASSERT(pkt->pkt_nextpkt == NULL);
1168
1169 _FSW_INJECT_ERROR(20, pkt->pkt_qum_qflags,
1170 pkt->pkt_qum_qflags | QUM_F_DROPPED, null_func);
1171 if (__improbable(((pkt->pkt_qum_qflags & QUM_F_DROPPED) != 0))
1172 || (pkt->pkt_length == 0)) {
1173 KR_SLOT_DETACH_METADATA(r, ksd);
1174 FSW_STATS_INC(FSW_STATS_DROP);
1175 pp_free_packet_single(pkt);
1176 continue;
1177 }
1178 if (gso_enabled) {
1179 gso_pkts = estimate_gso_pkts(pkt);
1180
1181 /*
1182 * We use the first packet to determine what
1183 * type the subsequent ones need to be (GSO or
1184 * non-GSO).
1185 */
1186 if (n_pkts == 0) {
1187 gso_required = (gso_pkts != 0);
1188 } else {
1189 if (gso_required != (gso_pkts != 0)) {
1190 break;
1191 }
1192 }
1193 *gso_pkts_estimate += gso_pkts;
1194 }
1195 KR_SLOT_DETACH_METADATA(r, ksd);
1196 if (NA_CHANNEL_EVENT_ATTACHED(&vpna->vpna_up)) {
1197 __packet_set_tx_nx_port(SK_PKT2PH(pkt),
1198 vpna->vpna_nx_port, vpna->vpna_gencnt);
1199 }
1200 n_pkts++;
1201 *n_bytes += pkt->pkt_length;
1202 KPKTQ_ENQUEUE(pktq, pkt);
1203 }
1204 r->ckr_khead = idx;
1205 r->ckr_ktail = SLOT_PREV(idx, r->ckr_lim);
1206 DTRACE_SKYWALK5(tx__ring__dequeue, struct nx_flowswitch *, fsw,
1207 ifnet_t, fsw->fsw_ifp, uint32_t, n_pkts, uint32_t, *n_bytes,
1208 uint32_t, *gso_pkts_estimate);
1209 }
1210
1211 static void
fsw_ring_enqueue_pktq(struct nx_flowswitch * fsw,struct __kern_channel_ring * r,struct pktq * pktq)1212 fsw_ring_enqueue_pktq(struct nx_flowswitch *fsw, struct __kern_channel_ring *r,
1213 struct pktq *pktq)
1214 {
1215 #pragma unused(fsw)
1216 struct __kern_packet *pkt;
1217 struct __kern_quantum *kqum;
1218 uint32_t kr_space_avail = 0;
1219 uint32_t n, n_pkts = 0, n_bytes = 0;
1220 slot_idx_t idx = 0, idx_start = 0, idx_end = 0;
1221
1222 kr_enter(r, TRUE);
1223
1224 idx_start = r->ckr_ktail;
1225 kr_space_avail = kr_available_slots_rxring(r);
1226 _FSW_INJECT_ERROR(40, kr_space_avail, 0, null_func);
1227 n = MIN(kr_space_avail, KPKTQ_LEN(pktq));
1228 _FSW_INJECT_ERROR(41, n, 0, null_func);
1229 idx_end = SLOT_INCREMENT(idx_start, n, r->ckr_lim);
1230
1231 idx = idx_start;
1232 while (idx != idx_end) {
1233 KPKTQ_DEQUEUE(pktq, pkt);
1234 kqum = SK_PTR_ADDR_KQUM(pkt);
1235 kqum->qum_qflags |= QUM_F_FINALIZED;
1236 n_pkts++;
1237 n_bytes += pkt->pkt_length;
1238 KR_SLOT_ATTACH_METADATA(r, KR_KSD(r, idx), kqum);
1239 if (__improbable(pkt->pkt_trace_id != 0)) {
1240 KDBG(SK_KTRACE_PKT_RX_FSW | DBG_FUNC_END, pkt->pkt_trace_id);
1241 KDBG(SK_KTRACE_PKT_RX_CHN | DBG_FUNC_START, pkt->pkt_trace_id);
1242 }
1243 idx = SLOT_NEXT(idx, r->ckr_lim);
1244 }
1245
1246 kr_update_stats(r, n_pkts, n_bytes);
1247
1248 /*
1249 * ensure slot attachments are visible before updating the
1250 * tail pointer
1251 */
1252 os_atomic_thread_fence(seq_cst);
1253
1254 r->ckr_ktail = idx_end;
1255
1256 kr_exit(r);
1257
1258 r->ckr_na_notify(r, kernproc, NA_NOTEF_PUSH);
1259
1260 SK_DF(SK_VERB_FSW_DP | SK_VERB_RING, "%s enqueued %d pkts",
1261 r->ckr_name, n_pkts);
1262 }
1263
1264 static void
pkts_to_pktq(struct __kern_packet ** __counted_by (n_pkts)pkts,uint32_t n_pkts,struct pktq * pktq)1265 pkts_to_pktq(struct __kern_packet **__counted_by(n_pkts)pkts, uint32_t n_pkts, struct pktq *pktq)
1266 {
1267 ASSERT(KPKTQ_EMPTY(pktq));
1268
1269 for (uint32_t i = 0; i < n_pkts; i++) {
1270 struct __kern_packet *__single pkt = pkts[i];
1271 ASSERT(pkt->pkt_nextpkt == NULL);
1272 KPKTQ_ENQUEUE(pktq, pkt);
1273 }
1274 }
1275
1276 /*
1277 * This function is modeled after nx_netif_host_grab_pkts() in nx_netif_host.c.
1278 */
1279 SK_NO_INLINE_ATTRIBUTE
1280 static void
convert_native_pktq_to_mbufs(struct nx_flowswitch * fsw,struct pktq * pktq,struct mbuf ** m_headp,struct mbuf ** m_tailp,uint32_t * cnt,uint32_t * bytes)1281 convert_native_pktq_to_mbufs(struct nx_flowswitch *fsw, struct pktq *pktq,
1282 struct mbuf **m_headp, struct mbuf **m_tailp, uint32_t *cnt, uint32_t *bytes)
1283 {
1284 uint32_t tot_cnt;
1285 unsigned int num_segs = 1;
1286 struct mbuf *__single mhead, *__single head = NULL;
1287 struct mbuf *__single tail = NULL, **__single tailp = &head;
1288 uint32_t mhead_cnt, mhead_bufsize;
1289 uint32_t mhead_waste = 0;
1290 uint32_t mcnt = 0, mbytes = 0;
1291 uint32_t largest, max_pkt_len;
1292 struct __kern_packet *__single pkt;
1293 struct kern_pbufpool *pp;
1294
1295 tot_cnt = KPKTQ_LEN(pktq);
1296 ASSERT(tot_cnt > 0);
1297 mhead_cnt = tot_cnt;
1298
1299 /*
1300 * Opportunistically batch-allocate the mbufs based on the largest
1301 * packet size we've seen in the recent past. Note that we reset
1302 * fe_rx_largest_size below if we notice that we're under-utilizing the
1303 * allocated buffers (thus disabling this batch allocation).
1304 */
1305 largest = *(volatile uint32_t*)&fsw->fsw_rx_largest_size; /* read once */
1306 if (__probable(largest != 0)) {
1307 if (largest <= MCLBYTES) {
1308 mhead = m_allocpacket_internal(&mhead_cnt, MCLBYTES,
1309 &num_segs, M_NOWAIT, 1, 0);
1310 mhead_bufsize = MCLBYTES;
1311 } else if (largest <= MBIGCLBYTES) {
1312 mhead = m_allocpacket_internal(&mhead_cnt, MBIGCLBYTES,
1313 &num_segs, M_NOWAIT, 1, 0);
1314 mhead_bufsize = MBIGCLBYTES;
1315 } else if (largest <= M16KCLBYTES) {
1316 mhead = m_allocpacket_internal(&mhead_cnt, M16KCLBYTES,
1317 &num_segs, M_NOWAIT, 1, 0);
1318 mhead_bufsize = M16KCLBYTES;
1319 } else if (largest <= M16KCLBYTES * 2) {
1320 num_segs = 2;
1321 mhead = m_allocpacket_internal(&mhead_cnt, M16KCLBYTES * 2,
1322 &num_segs, M_NOWAIT, 1, 0);
1323 mhead_bufsize = M16KCLBYTES * 2;
1324 } else {
1325 mhead = NULL;
1326 mhead_bufsize = mhead_cnt = 0;
1327 }
1328 } else {
1329 mhead = NULL;
1330 mhead_bufsize = mhead_cnt = 0;
1331 }
1332 DTRACE_SKYWALK4(bufstats, uint32_t, largest, uint32_t, mhead_bufsize,
1333 uint32_t, mhead_cnt, uint32_t, tot_cnt);
1334
1335 pp = __DECONST(struct kern_pbufpool *, KPKTQ_FIRST(pktq)->pkt_qum.qum_pp);
1336 max_pkt_len = PP_BUF_SIZE_DEF(pp) * pp->pp_max_frags;
1337
1338 KPKTQ_FOREACH(pkt, pktq) {
1339 uint32_t tot_len, len;
1340 uint16_t pad, llhlen, iphlen;
1341 boolean_t do_cksum_rx;
1342 struct mbuf *__single m;
1343 int error;
1344
1345 llhlen = pkt->pkt_l2_len;
1346 len = pkt->pkt_length;
1347 if (__improbable(len > max_pkt_len || len == 0 || llhlen > len)) {
1348 DTRACE_SKYWALK2(bad__len, struct nx_flowswitch *, fsw,
1349 struct __kern_packet *, pkt);
1350 FSW_STATS_INC(FSW_STATS_DROP);
1351 FSW_STATS_INC(FSW_STATS_RX_COPY_BAD_LEN);
1352 continue;
1353 }
1354 /* begin payload on 32-bit boundary; figure out the padding */
1355 pad = (uint16_t)P2ROUNDUP(llhlen, sizeof(uint32_t)) - llhlen;
1356 tot_len = pad + len;
1357
1358 /* remember largest packet size */
1359 if (__improbable(largest < tot_len)) {
1360 largest = MAX(tot_len, MCLBYTES);
1361 }
1362
1363 /*
1364 * If the above batch allocation returned partial
1365 * success, we try a blocking allocation here again.
1366 */
1367 m = mhead;
1368 if (__improbable(m == NULL || tot_len > mhead_bufsize)) {
1369 ASSERT(mhead != NULL || mhead_cnt == 0);
1370 num_segs = 1;
1371 if (tot_len > M16KCLBYTES) {
1372 num_segs = 0;
1373 }
1374 if ((error = mbuf_allocpacket(MBUF_DONTWAIT, tot_len,
1375 &num_segs, &m)) != 0) {
1376 DTRACE_SKYWALK2(bad__len,
1377 struct nx_flowswitch *, fsw,
1378 struct __kern_packet *, pkt);
1379 FSW_STATS_INC(FSW_STATS_DROP_NOMEM_MBUF);
1380 FSW_STATS_INC(FSW_STATS_DROP);
1381 continue;
1382 }
1383 } else {
1384 mhead = m->m_nextpkt;
1385 m->m_nextpkt = NULL;
1386 ASSERT(mhead_cnt != 0);
1387 --mhead_cnt;
1388
1389 /* check if we're underutilizing large buffers */
1390 if (__improbable(mhead_bufsize > MCLBYTES &&
1391 tot_len < (mhead_bufsize >> 1))) {
1392 ++mhead_waste;
1393 }
1394 /*
1395 * Clean up unused mbuf.
1396 * Ony need to do this when we pre-alloc 2x16K mbufs
1397 */
1398 if (__improbable(mhead_bufsize >= tot_len + M16KCLBYTES)) {
1399 ASSERT(mhead_bufsize == 2 * M16KCLBYTES);
1400 struct mbuf *m_extra = m->m_next;
1401 ASSERT(m_extra != NULL);
1402 ASSERT(m_extra->m_len == 0);
1403 ASSERT(M_SIZE(m_extra) == M16KCLBYTES);
1404 m->m_next = NULL;
1405 m_freem(m_extra);
1406 FSW_STATS_INC(FSW_STATS_RX_WASTED_16KMBUF);
1407 }
1408 }
1409 m->m_data += pad;
1410 /*
1411 * XXX -fbounds-safety: external dependency
1412 * mtod does not work because m_len is 0
1413 */
1414 m->m_pkthdr.pkt_hdr = m_mtod_current(m);
1415
1416 /* don't include IP header from partial sum */
1417 if (__probable((pkt->pkt_qum_qflags &
1418 QUM_F_FLOW_CLASSIFIED) != 0)) {
1419 iphlen = pkt->pkt_flow_ip_hlen;
1420 do_cksum_rx = sk_cksum_rx;
1421 } else {
1422 iphlen = 0;
1423 do_cksum_rx = FALSE;
1424 }
1425
1426 fsw->fsw_pkt_copy_to_mbuf(NR_RX, SK_PKT2PH(pkt),
1427 pkt->pkt_headroom, m, 0, len, do_cksum_rx,
1428 llhlen + iphlen);
1429
1430 FSW_STATS_INC(FSW_STATS_RX_COPY_PKT2MBUF);
1431 if (do_cksum_rx) {
1432 FSW_STATS_INC(FSW_STATS_RX_COPY_SUM);
1433 }
1434 #if DEBUG || DEVELOPMENT
1435 if (__improbable(pkt_trailers > 0)) {
1436 (void) pkt_add_trailers_mbuf(m, llhlen + iphlen);
1437 }
1438 #endif /* DEBUG || DEVELOPMENT */
1439 m_adj(m, llhlen);
1440
1441 m->m_pkthdr.rcvif = fsw->fsw_ifp;
1442 if (__improbable((pkt->pkt_link_flags &
1443 PKT_LINKF_ETHFCS) != 0)) {
1444 m->m_flags |= M_HASFCS;
1445 }
1446 if (__improbable(pkt->pkt_pflags & PKT_F_WAKE_PKT)) {
1447 m->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
1448 }
1449 ASSERT(m->m_nextpkt == NULL);
1450 tail = m;
1451 *tailp = m;
1452 tailp = &m->m_nextpkt;
1453 mcnt++;
1454 mbytes += m_pktlen(m);
1455 }
1456 /* free any leftovers */
1457 if (__improbable(mhead != NULL)) {
1458 DTRACE_SKYWALK1(mhead__leftover, uint32_t, mhead_cnt);
1459 ASSERT(mhead_cnt != 0);
1460 (void) m_freem_list(mhead);
1461 mhead = NULL;
1462 mhead_cnt = 0;
1463 }
1464
1465 /* reset if most packets (>50%) are smaller than our batch buffers */
1466 if (__improbable(mhead_waste > ((uint32_t)tot_cnt >> 1))) {
1467 DTRACE_SKYWALK4(mhead__waste, struct nx_flowswitch *, fsw,
1468 struct flow_entry *, NULL, uint32_t, mhead_waste,
1469 uint32_t, tot_cnt);
1470 largest = 0;
1471 }
1472
1473 if (largest != fsw->fsw_rx_largest_size) {
1474 os_atomic_store(&fsw->fsw_rx_largest_size, largest, release);
1475 }
1476
1477 pp_free_pktq(pktq);
1478 *m_headp = head;
1479 *m_tailp = tail;
1480 *cnt = mcnt;
1481 *bytes = mbytes;
1482 }
1483
1484 /*
1485 * This function only extracts the mbuf from the packet. The caller frees
1486 * the packet.
1487 */
1488 static inline struct mbuf *
convert_compat_pkt_to_mbuf(struct nx_flowswitch * fsw,struct __kern_packet * pkt)1489 convert_compat_pkt_to_mbuf(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
1490 {
1491 struct mbuf *m;
1492 struct pkthdr *mhdr;
1493 uint16_t llhlen;
1494
1495 m = pkt->pkt_mbuf;
1496 ASSERT(m != NULL);
1497
1498 llhlen = pkt->pkt_l2_len;
1499 if (llhlen > pkt->pkt_length) {
1500 m_freem(m);
1501 KPKT_CLEAR_MBUF_DATA(pkt);
1502 DTRACE_SKYWALK2(bad__len, struct nx_flowswitch *, fsw,
1503 struct __kern_packet *, pkt);
1504 FSW_STATS_INC(FSW_STATS_DROP);
1505 FSW_STATS_INC(FSW_STATS_RX_COPY_BAD_LEN);
1506 return NULL;
1507 }
1508 mhdr = &m->m_pkthdr;
1509 if ((mhdr->csum_flags & CSUM_DATA_VALID) == 0 &&
1510 PACKET_HAS_PARTIAL_CHECKSUM(pkt)) {
1511 mhdr->csum_flags &= ~CSUM_RX_FLAGS;
1512 mhdr->csum_flags |= (CSUM_DATA_VALID | CSUM_PARTIAL);
1513 mhdr->csum_rx_start = pkt->pkt_csum_rx_start_off;
1514 mhdr->csum_rx_val = pkt->pkt_csum_rx_value;
1515 }
1516 #if DEBUG || DEVELOPMENT
1517 uint32_t extra = 0;
1518 if (__improbable(pkt_trailers > 0)) {
1519 extra = pkt_add_trailers_mbuf(m, llhlen);
1520 }
1521 #endif /* DEBUG || DEVELOPMENT */
1522 m_adj(m, llhlen);
1523 ASSERT((uint32_t)m_pktlen(m) == ((pkt->pkt_length - llhlen) + extra));
1524 KPKT_CLEAR_MBUF_DATA(pkt);
1525 return m;
1526 }
1527
1528 SK_NO_INLINE_ATTRIBUTE
1529 static void
convert_compat_pktq_to_mbufs(struct nx_flowswitch * fsw,struct pktq * pktq,struct mbuf ** m_head,struct mbuf ** m_tail,uint32_t * cnt,uint32_t * bytes)1530 convert_compat_pktq_to_mbufs(struct nx_flowswitch *fsw, struct pktq *pktq,
1531 struct mbuf **m_head, struct mbuf **m_tail, uint32_t *cnt, uint32_t *bytes)
1532 {
1533 struct __kern_packet *pkt;
1534 struct mbuf *__single m, *__single head = NULL;
1535 struct mbuf *__single tail = NULL, **__single tailp = &head;
1536 uint32_t c = 0, b = 0;
1537
1538 KPKTQ_FOREACH(pkt, pktq) {
1539 m = convert_compat_pkt_to_mbuf(fsw, pkt);
1540 if (__improbable(m == NULL)) {
1541 continue;
1542 }
1543 tail = m;
1544 *tailp = m;
1545 tailp = &m->m_nextpkt;
1546 c++;
1547 b += m_pktlen(m);
1548 }
1549 pp_free_pktq(pktq);
1550 *m_head = head;
1551 *m_tail = tail;
1552 *cnt = c;
1553 *bytes = b;
1554 }
1555
1556 void
fsw_host_sendup(ifnet_t ifp,struct mbuf * m_head,struct mbuf * m_tail,uint32_t cnt,uint32_t bytes)1557 fsw_host_sendup(ifnet_t ifp, struct mbuf *m_head, struct mbuf *m_tail,
1558 uint32_t cnt, uint32_t bytes)
1559 {
1560 struct ifnet_stat_increment_param s;
1561
1562 bzero(&s, sizeof(s));
1563 s.packets_in = cnt;
1564 s.bytes_in = bytes;
1565 dlil_input_handler(ifp, m_head, m_tail, &s, FALSE, NULL);
1566 }
1567
1568 void
fsw_host_rx(struct nx_flowswitch * fsw,struct pktq * pktq)1569 fsw_host_rx(struct nx_flowswitch *fsw, struct pktq *pktq)
1570 {
1571 struct mbuf *__single m_head = NULL, *__single m_tail = NULL;
1572 uint32_t cnt = 0, bytes = 0;
1573 ifnet_fsw_rx_cb_t __single cb;
1574 void *__single cb_arg;
1575 boolean_t compat;
1576
1577 ASSERT(!KPKTQ_EMPTY(pktq));
1578 if (ifnet_get_flowswitch_rx_callback(fsw->fsw_ifp, &cb, &cb_arg) == 0) {
1579 ASSERT(cb != NULL);
1580 ASSERT(cb_arg != NULL);
1581 (*cb)(cb_arg, pktq);
1582 ifnet_release_flowswitch_rx_callback(fsw->fsw_ifp);
1583 if (KPKTQ_EMPTY(pktq)) {
1584 return;
1585 } else {
1586 DTRACE_SKYWALK2(leftover__pkts, struct nx_flowswitch *, fsw,
1587 struct pktq *, pktq);
1588 }
1589 }
1590
1591 /* All packets in the pktq must have the same type */
1592 compat = ((KPKTQ_FIRST(pktq)->pkt_pflags & PKT_F_MBUF_DATA) != 0);
1593 if (compat) {
1594 convert_compat_pktq_to_mbufs(fsw, pktq, &m_head, &m_tail, &cnt,
1595 &bytes);
1596 } else {
1597 convert_native_pktq_to_mbufs(fsw, pktq, &m_head, &m_tail, &cnt,
1598 &bytes);
1599 }
1600 if (__improbable(m_head == NULL)) {
1601 DTRACE_SKYWALK1(empty__head, struct nx_flowswitch *, fsw);
1602 return;
1603 }
1604 fsw_host_sendup(fsw->fsw_ifp, m_head, m_tail, cnt, bytes);
1605 }
1606
1607 void
fsw_ring_enqueue_tail_drop(struct nx_flowswitch * fsw,struct __kern_channel_ring * r,struct pktq * pktq)1608 fsw_ring_enqueue_tail_drop(struct nx_flowswitch *fsw,
1609 struct __kern_channel_ring *r, struct pktq *pktq)
1610 {
1611 fsw_ring_enqueue_pktq(fsw, r, pktq);
1612 /*
1613 * Rx stall detection: don't update enqueue ts if dequeue ts < enqueue ts.
1614 * This is to ensure we use the timestamp of the earliest enqueue without
1615 * a dequeue.
1616 */
1617 if (r->ckr_rx_dequeue_ts >= r->ckr_rx_enqueue_ts) {
1618 r->ckr_rx_enqueue_ts = _net_uptime;
1619 }
1620 FSW_STATS_ADD(FSW_STATS_RX_DST_RING_FULL, KPKTQ_LEN(pktq));
1621 dp_drop_pktq(fsw, pktq, 0, DROP_REASON_RX_DST_RING_FULL, __LINE__,
1622 DROPTAP_FLAG_L2_MISSING);
1623 }
1624
1625 static struct nexus_adapter *
flow_get_na(struct nx_flowswitch * fsw,struct flow_entry * fe)1626 flow_get_na(struct nx_flowswitch *fsw, struct flow_entry *fe)
1627 {
1628 struct kern_nexus *nx = fsw->fsw_nx;
1629 struct nexus_adapter *na = NULL;
1630 nexus_port_t port = fe->fe_nx_port;
1631
1632 if (port == FSW_VP_DEV || port == FSW_VP_HOST) {
1633 SK_ERR("dev or host ports have no NA");
1634 return NULL;
1635 }
1636
1637 if (__improbable(!nx_port_is_valid(nx, port))) {
1638 SK_DF(SK_VERB_FSW_DP, "%s[%d] port no longer valid",
1639 if_name(fsw->fsw_ifp), port);
1640 return NULL;
1641 }
1642
1643 na = nx_port_get_na(nx, port);
1644 if (__improbable(na == NULL)) {
1645 FSW_STATS_INC(FSW_STATS_DST_NXPORT_INVALID);
1646 SK_DF(SK_VERB_FSW_DP, "%s[%d] NA no longer valid",
1647 if_name(fsw->fsw_ifp), port);
1648 return NULL;
1649 }
1650
1651 if (__improbable(!NA_IS_ACTIVE(na))) {
1652 FSW_STATS_INC(FSW_STATS_DST_NXPORT_INACTIVE);
1653 SK_DF(SK_VERB_FSW_DP, "%s[%d] NA no longer active",
1654 if_name(fsw->fsw_ifp), port);
1655 return NULL;
1656 }
1657
1658 if (__improbable(nx_port_is_defunct(nx, port))) {
1659 FSW_STATS_INC(FSW_STATS_DST_NXPORT_DEFUNCT);
1660 SK_DF(SK_VERB_FSW_DP, "%s[%d] NA defuncted",
1661 if_name(fsw->fsw_ifp), port);
1662 return NULL;
1663 }
1664
1665 return na;
1666 }
1667
1668 static inline struct __kern_channel_ring *
flow_get_ring(struct nx_flowswitch * fsw,struct flow_entry * fe,enum txrx txrx)1669 flow_get_ring(struct nx_flowswitch *fsw, struct flow_entry *fe, enum txrx txrx)
1670 {
1671 struct nexus_vp_adapter *na = NULL;
1672 struct __kern_channel_ring *__single r = NULL;
1673
1674 na = VPNA(flow_get_na(fsw, fe));
1675 if (__improbable(na == NULL)) {
1676 return NULL;
1677 }
1678
1679 switch (txrx) {
1680 case NR_RX:
1681 r = KR_SINGLE(&na->vpna_up.na_rx_rings[0]);
1682 break;
1683 case NR_TX:
1684 r = KR_SINGLE(&na->vpna_up.na_tx_rings[0]);
1685 break;
1686 default:
1687 __builtin_unreachable();
1688 VERIFY(0);
1689 }
1690
1691 if (__improbable(KR_DROP(r))) {
1692 FSW_STATS_INC(FSW_STATS_DST_RING_DROPMODE);
1693 SK_DF(SK_VERB_FSW_DP | SK_VERB_RING, "r %0xllx %s drop mode",
1694 r->ckr_name, SK_KVA(r));
1695 return NULL;
1696 }
1697
1698 ASSERT(KRNA(r)->na_md_type == NEXUS_META_TYPE_PACKET);
1699
1700 #if (DEVELOPMENT || DEBUG)
1701 if (r != NULL) {
1702 _FSW_INJECT_ERROR(4, r, NULL, null_func);
1703 }
1704 #endif /* DEVELOPMENT || DEBUG */
1705
1706 return r;
1707 }
1708
1709 struct __kern_channel_ring *
fsw_flow_get_rx_ring(struct nx_flowswitch * fsw,struct flow_entry * fe)1710 fsw_flow_get_rx_ring(struct nx_flowswitch *fsw, struct flow_entry *fe)
1711 {
1712 return flow_get_ring(fsw, fe, NR_RX);
1713 }
1714
1715 static bool
dp_flow_route_process(struct nx_flowswitch * fsw,struct flow_entry * fe)1716 dp_flow_route_process(struct nx_flowswitch *fsw, struct flow_entry *fe)
1717 {
1718 struct flow_route *fr = fe->fe_route;
1719 struct ifnet *ifp = fsw->fsw_ifp;
1720
1721 if (__improbable(!(fe->fe_flags & FLOWENTF_NONVIABLE) &&
1722 !fe->fe_want_nonviable && (fe->fe_key.fk_mask & FKMASK_SRC) &&
1723 fe->fe_laddr_gencnt != ifp->if_nx_flowswitch.if_fsw_ipaddr_gencnt &&
1724 !flow_route_key_validate(&fe->fe_key, ifp, &fe->fe_laddr_gencnt))) {
1725 /*
1726 * The source address is no longer around; we want this
1727 * flow to be nonviable, but that requires holding the lock
1728 * as writer (which isn't the case now.) Indicate that
1729 * we need to finalize the nonviable later down below.
1730 *
1731 * We also request that the flow route be re-configured,
1732 * if this is a connected mode flow.
1733 *
1734 */
1735 if (!(fe->fe_flags & FLOWENTF_NONVIABLE)) {
1736 /*
1737 * fsw_pending_nonviable is a hint for reaper thread;
1738 * due to the fact that setting fe_want_nonviable and
1739 * incrementing fsw_pending_nonviable counter is not
1740 * atomic, let the increment happen first, and the
1741 * thread losing the CAS does decrement.
1742 */
1743 os_atomic_inc(&fsw->fsw_pending_nonviable, relaxed);
1744 if (os_atomic_cmpxchg(&fe->fe_want_nonviable, 0, 1, acq_rel)) {
1745 fsw_reap_sched(fsw);
1746 } else {
1747 os_atomic_dec(&fsw->fsw_pending_nonviable, relaxed);
1748 }
1749 }
1750 if (fr != NULL) {
1751 os_atomic_inc(&fr->fr_want_configure, relaxed);
1752 }
1753 }
1754
1755 /* if flow was (or is going to be) marked as nonviable, drop it */
1756 if (__improbable(fe->fe_want_nonviable ||
1757 (fe->fe_flags & FLOWENTF_NONVIABLE) != 0)) {
1758 SK_DF(SK_VERB_FSW_DP | SK_VERB_FLOW, "flow 0x%llx non-viable",
1759 SK_KVA(fe));
1760 return false;
1761 }
1762 return true;
1763 }
1764
1765 bool
dp_flow_rx_route_process(struct nx_flowswitch * fsw,struct flow_entry * fe)1766 dp_flow_rx_route_process(struct nx_flowswitch *fsw, struct flow_entry *fe)
1767 {
1768 bool okay;
1769 okay = dp_flow_route_process(fsw, fe);
1770 #if (DEVELOPMENT || DEBUG)
1771 if (okay) {
1772 _FSW_INJECT_ERROR(5, okay, false, null_func);
1773 }
1774 #endif /* DEVELOPMENT || DEBUG */
1775
1776 return okay;
1777 }
1778
1779 void
dp_flow_rx_process(struct nx_flowswitch * fsw,struct flow_entry * fe,struct pktq * rx_pkts,uint32_t rx_bytes,uint32_t flags)1780 dp_flow_rx_process(struct nx_flowswitch *fsw, struct flow_entry *fe,
1781 struct pktq *rx_pkts, uint32_t rx_bytes, uint32_t flags)
1782 {
1783 #pragma unused(flags)
1784 struct pktq dpkts; /* dst pool alloc'ed packets */
1785 struct pktq disposed_pkts; /* done src packets */
1786 struct pktq dropped_pkts; /* dropped src packets */
1787 struct pktq transferred_pkts; /* dst packet ready for ring */
1788 struct __kern_packet *pkt, *tpkt;
1789 struct kern_pbufpool *dpp;
1790 uint32_t n_pkts = KPKTQ_LEN(rx_pkts);
1791 uint64_t buf_array[RX_BUFLET_BATCH_COUNT];
1792 uint16_t buf_array_iter = 0;
1793 uint32_t cnt, buf_cnt = 0;
1794 int err;
1795 drop_reason_t reason = DROP_REASON_UNSPECIFIED;
1796 uint16_t line = 0;
1797
1798 KPKTQ_INIT(&dpkts);
1799 KPKTQ_INIT(&dropped_pkts);
1800 KPKTQ_INIT(&disposed_pkts);
1801 KPKTQ_INIT(&transferred_pkts);
1802
1803 if (__improbable(!dp_flow_rx_route_process(fsw, fe))) {
1804 SK_ERR("Rx route bad");
1805 fsw_snoop_and_dequeue(fe, &dropped_pkts, rx_pkts, true);
1806 FSW_STATS_ADD(FSW_STATS_RX_FLOW_NONVIABLE, n_pkts);
1807 reason = DROP_REASON_FSW_FLOW_NONVIABLE;
1808 line = __LINE__;
1809 goto done;
1810 }
1811
1812 if (fe->fe_nx_port == FSW_VP_HOST) {
1813 /*
1814 * The host ring does not exist anymore so we can't take
1815 * the enqueue path below. This path should only be hit
1816 * for the rare tcp fragmentation case.
1817 */
1818 fsw_host_rx(fsw, rx_pkts);
1819 return;
1820 }
1821
1822 /* find the ring */
1823 struct __kern_channel_ring *r;
1824 r = fsw_flow_get_rx_ring(fsw, fe);
1825 if (__improbable(r == NULL)) {
1826 fsw_snoop_and_dequeue(fe, &dropped_pkts, rx_pkts, true);
1827 reason = DROP_REASON_FSW_RX_RING_NOT_FOUND;
1828 line = __LINE__;
1829 goto done;
1830 }
1831
1832 /* snoop before L2 is stripped */
1833 if (__improbable(pktap_total_tap_count != 0)) {
1834 fsw_snoop(fsw, fe, rx_pkts, true);
1835 }
1836
1837 dpp = r->ckr_pp;
1838 /* batch allocate enough packets */
1839 err = pp_alloc_pktq(dpp, 1, &dpkts, n_pkts, NULL, NULL,
1840 SKMEM_NOSLEEP);
1841 if (__improbable(err == ENOMEM)) {
1842 ASSERT(KPKTQ_EMPTY(&dpkts));
1843 KPKTQ_CONCAT(&dropped_pkts, rx_pkts);
1844 FSW_STATS_ADD(FSW_STATS_DROP_NOMEM_PKT, n_pkts);
1845 SK_ERR("failed to alloc %u pkts for kr %s, 0x%llu", n_pkts,
1846 r->ckr_name, SK_KVA(r));
1847 reason = DROP_REASON_FSW_PP_ALLOC_FAILED;
1848 line = __LINE__;
1849 goto done;
1850 }
1851
1852 /*
1853 * estimate total number of buflets for the packet chain.
1854 */
1855 cnt = howmany(rx_bytes, PP_BUF_SIZE_DEF(dpp));
1856 if (cnt > n_pkts) {
1857 ASSERT(dpp->pp_max_frags > 1);
1858 cnt -= n_pkts;
1859 buf_cnt = MIN(RX_BUFLET_BATCH_COUNT, cnt);
1860 err = pp_alloc_buflet_batch(dpp, buf_array, &buf_cnt,
1861 SKMEM_NOSLEEP, false);
1862 if (__improbable(buf_cnt == 0)) {
1863 KPKTQ_CONCAT(&dropped_pkts, rx_pkts);
1864 FSW_STATS_ADD(FSW_STATS_DROP_NOMEM_PKT, n_pkts);
1865 SK_ERR("failed to alloc %d buflets (err %d) for kr %s, "
1866 "0x%llu", cnt, err, r->ckr_name, SK_KVA(r));
1867 reason = DROP_REASON_FSW_PP_ALLOC_FAILED;
1868 line = __LINE__;
1869 goto done;
1870 }
1871 err = 0;
1872 }
1873
1874 /* extra processing for user flow */
1875 KPKTQ_FOREACH_SAFE(pkt, rx_pkts, tpkt) {
1876 err = 0;
1877 KPKTQ_REMOVE(rx_pkts, pkt);
1878 if (rx_bytes > pkt->pkt_flow_ulen) {
1879 rx_bytes -= pkt->pkt_flow_ulen;
1880 } else {
1881 rx_bytes = 0;
1882 }
1883 err = flow_pkt_track(fe, pkt, true);
1884 _FSW_INJECT_ERROR(33, err, EPROTO, null_func);
1885 if (__improbable(err != 0)) {
1886 SK_DF(SK_VERB_FLOW_TRACK, "flow_pkt_track failed (err %d)", err);
1887 FSW_STATS_INC(FSW_STATS_RX_FLOW_TRACK_ERR);
1888 /* if need to trigger RST */
1889 if (err == ENETRESET) {
1890 flow_track_abort_tcp(fe, pkt, NULL);
1891 }
1892 dp_drop_pkt_single(fsw, pkt, 0, DROP_REASON_FSW_FLOW_TRACK_ERR,
1893 DROPTAP_FLAG_L2_MISSING);
1894 continue;
1895 }
1896
1897 /* transfer to dpkt */
1898 if (pkt->pkt_qum.qum_pp != dpp) {
1899 struct __kern_buflet *bprev, *bnew;
1900 struct __kern_packet *dpkt = NULL;
1901 uint32_t n_bufs, i;
1902
1903 KPKTQ_DEQUEUE(&dpkts, dpkt);
1904 /* XXX Why would dpkt be NULL at this point? */
1905 if (__improbable(dpkt == NULL)) {
1906 FSW_STATS_INC(FSW_STATS_DROP_NOMEM_PKT);
1907 dp_drop_pkt_single(fsw, pkt, 0,
1908 DROP_REASON_FSW_PP_ALLOC_FAILED, DROPTAP_FLAG_L2_MISSING);
1909 continue;
1910 }
1911 n_bufs = howmany(pkt->pkt_length, PP_BUF_SIZE_DEF(dpp));
1912 n_bufs--;
1913 for (i = 0; i < n_bufs; i++) {
1914 if (__improbable(buf_cnt == 0)) {
1915 ASSERT(dpp->pp_max_frags > 1);
1916 buf_array_iter = 0;
1917 cnt = howmany(rx_bytes, PP_BUF_SIZE_DEF(dpp));
1918 n_pkts = KPKTQ_LEN(rx_pkts);
1919 if (cnt >= n_pkts) {
1920 cnt -= n_pkts;
1921 } else {
1922 cnt = 0;
1923 }
1924 cnt += (n_bufs - i);
1925 buf_cnt = MIN(RX_BUFLET_BATCH_COUNT,
1926 cnt);
1927 cnt = buf_cnt;
1928 err = pp_alloc_buflet_batch(dpp,
1929 buf_array, &buf_cnt,
1930 SKMEM_NOSLEEP, false);
1931 if (__improbable(buf_cnt == 0)) {
1932 FSW_STATS_INC(FSW_STATS_DROP_NOMEM_PKT);
1933 dp_drop_pkt_single(fsw, pkt, 0,
1934 DROP_REASON_FSW_PP_ALLOC_FAILED,
1935 DROPTAP_FLAG_L2_MISSING);
1936 pkt = NULL;
1937 pp_free_packet_single(dpkt);
1938 dpkt = NULL;
1939 SK_ERR("failed to alloc %d "
1940 "buflets (err %d) for "
1941 "kr %s, 0x%llu", cnt, err,
1942 r->ckr_name, SK_KVA(r));
1943 break;
1944 }
1945 err = 0;
1946 }
1947 ASSERT(buf_cnt != 0);
1948 if (i == 0) {
1949 PKT_GET_FIRST_BUFLET(dpkt, 1, bprev);
1950 }
1951 /*
1952 * XXX -fbounds-safety: can't avoid using forge
1953 * unless we change the signature of
1954 * pp_alloc_buflet_batch().
1955 */
1956 bnew = __unsafe_forge_single(kern_buflet_t,
1957 buf_array[buf_array_iter]);
1958 buf_array[buf_array_iter] = 0;
1959 buf_array_iter++;
1960 buf_cnt--;
1961 VERIFY(kern_packet_add_buflet(SK_PKT2PH(dpkt),
1962 bprev, bnew) == 0);
1963 bprev = bnew;
1964 }
1965 if (__improbable(err != 0)) {
1966 continue;
1967 }
1968 err = copy_packet_from_dev(fsw, pkt, dpkt);
1969 _FSW_INJECT_ERROR(43, err, EINVAL, null_func);
1970 if (__improbable(err != 0)) {
1971 SK_ERR("copy packet failed (err %d)", err);
1972 dp_drop_pkt_single(fsw, pkt, 0,
1973 DROP_REASON_FSW_PKT_COPY_FAILED,
1974 DROPTAP_FLAG_L2_MISSING);
1975 pp_free_packet_single(dpkt);
1976 dpkt = NULL;
1977 continue;
1978 }
1979 KPKTQ_ENQUEUE(&disposed_pkts, pkt);
1980 pkt = dpkt;
1981 }
1982 _UUID_COPY(pkt->pkt_flow_id, fe->fe_uuid);
1983 _UUID_COPY(pkt->pkt_policy_euuid, fe->fe_eproc_uuid);
1984 pkt->pkt_policy_id = fe->fe_policy_id;
1985 pkt->pkt_skip_policy_id = fe->fe_skip_policy_id;
1986 pkt->pkt_transport_protocol = fe->fe_transport_protocol;
1987 if (pkt->pkt_bufs_cnt > 1) {
1988 pkt->pkt_aggr_type = PKT_AGGR_SINGLE_IP;
1989 pkt->pkt_seg_cnt = 1;
1990 }
1991 KPKTQ_ENQUEUE(&transferred_pkts, pkt);
1992 }
1993 KPKTQ_FINI(rx_pkts);
1994
1995 if (KPKTQ_LEN(&transferred_pkts) > 0) {
1996 fsw_ring_enqueue_tail_drop(fsw, r, &transferred_pkts);
1997 }
1998 KPKTQ_FINI(&transferred_pkts);
1999
2000 done:
2001 /* Free unused buflets */
2002 while (buf_cnt > 0) {
2003 /*
2004 * XXX -fbounds-safety: can't avoid using forge unless we change
2005 * the signature of pp_alloc_buflet_batch().
2006 */
2007 pp_free_buflet(dpp, __unsafe_forge_single(kern_buflet_t,
2008 (kern_buflet_t)(buf_array[buf_array_iter])));
2009 buf_array[buf_array_iter] = 0;
2010 buf_array_iter++;
2011 buf_cnt--;
2012 }
2013 dp_free_pktq(fsw, &dpkts);
2014 dp_free_pktq(fsw, &disposed_pkts);
2015 dp_drop_pktq(fsw, &dropped_pkts, 0, reason, line, DROPTAP_FLAG_L2_MISSING);
2016 }
2017
2018 static inline void
rx_flow_process(struct nx_flowswitch * fsw,struct flow_entry * fe,struct flow_entry_list * fes)2019 rx_flow_process(struct nx_flowswitch *fsw, struct flow_entry *fe,
2020 struct flow_entry_list *fes)
2021 {
2022 struct pktq rx_pkts;
2023 uint32_t rx_bytes;
2024 uint32_t rx_proc_flags;
2025
2026 ASSERT(!KPKTQ_EMPTY(&fe->fe_rx_pktq));
2027 ASSERT(KPKTQ_LEN(&fe->fe_rx_pktq) != 0);
2028
2029 KPKTQ_INIT(&rx_pkts);
2030 for (;;) {
2031 lck_mtx_lock(&fe->fe_rx_pktq_lock);
2032 if (KPKTQ_EMPTY(&fe->fe_rx_pktq)) {
2033 fe->fe_rx_worker_tid = 0;
2034 TAILQ_REMOVE(fes, fe, fe_rx_link);
2035 lck_mtx_unlock(&fe->fe_rx_pktq_lock);
2036 break;
2037 }
2038 KPKTQ_CONCAT(&rx_pkts, &fe->fe_rx_pktq);
2039 KPKTQ_DISPOSE(&fe->fe_rx_pktq);
2040 rx_bytes = fe->fe_rx_pktq_bytes;
2041 rx_proc_flags = fe->fe_rx_frag_count ? FLOW_PROC_FLAG_FRAGMENTS : 0;
2042 fe->fe_rx_pktq_bytes = 0;
2043 fe->fe_rx_frag_count = 0;
2044 lck_mtx_unlock(&fe->fe_rx_pktq_lock);
2045 SK_DF(SK_VERB_FSW_DP | SK_VERB_RX, "Rx %d pkts for fe %p port %d",
2046 KPKTQ_LEN(&rx_pkts), fe, fe->fe_nx_port);
2047 /* flow related processing (default, agg, fpd, etc.) */
2048 fe->fe_rx_process(fsw, fe, &rx_pkts, rx_bytes, rx_proc_flags);
2049 }
2050 ASSERT(KPKTQ_EMPTY(&rx_pkts));
2051
2052 if (__improbable(fe->fe_want_withdraw)) {
2053 fsw_reap_sched(fsw);
2054 }
2055 }
2056
2057 static inline void
dp_rx_process_wake_packet(struct nx_flowswitch * fsw,struct __kern_packet * pkt)2058 dp_rx_process_wake_packet(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
2059 {
2060 /*
2061 * We only care about wake packets of flows that belong the flow switch
2062 * as wake packets for the host stack are handled by the host input
2063 * function
2064 */
2065 #if (DEBUG || DEVELOPMENT)
2066 if (__improbable(fsw->fsw_ifp->if_xflags & IFXF_MARK_WAKE_PKT)) {
2067 /*
2068 * This is a one shot command
2069 */
2070 fsw->fsw_ifp->if_xflags &= ~IFXF_MARK_WAKE_PKT;
2071
2072 pkt->pkt_pflags |= PKT_F_WAKE_PKT;
2073 }
2074 #endif /* (DEBUG || DEVELOPMENT) */
2075 if (__improbable(pkt->pkt_pflags & PKT_F_WAKE_PKT)) {
2076 if_ports_used_match_pkt(fsw->fsw_ifp, pkt);
2077 }
2078 }
2079
2080 static void
_fsw_receive_locked(struct nx_flowswitch * fsw,struct pktq * pktq)2081 _fsw_receive_locked(struct nx_flowswitch *fsw, struct pktq *pktq)
2082 {
2083 struct __kern_packet *__single pkt, *__single tpkt;
2084 struct flow_entry_list fes = TAILQ_HEAD_INITIALIZER(fes);
2085 struct flow_entry *__single fe, *__single prev_fe;
2086 sa_family_t af;
2087 struct pktq host_pkts, dropped_pkts;
2088 drop_reason_t reason = DROP_REASON_UNSPECIFIED;
2089 uint16_t line = 0;
2090 int err;
2091 uint64_t thread_id;
2092
2093 KPKTQ_INIT(&host_pkts);
2094 KPKTQ_INIT(&dropped_pkts);
2095
2096 if (__improbable(FSW_QUIESCED(fsw))) {
2097 DTRACE_SKYWALK1(rx__quiesced, struct nx_flowswitch *, fsw);
2098 KPKTQ_CONCAT(&dropped_pkts, pktq);
2099 reason = DROP_REASON_FSW_QUIESCED;
2100 line = __LINE__;
2101 goto done;
2102 }
2103 if (__improbable(fsw->fsw_demux == NULL)) {
2104 KPKTQ_CONCAT(&dropped_pkts, pktq);
2105 reason = DROP_REASON_FSW_DEMUX_FAILED;
2106 line = __LINE__;
2107 goto done;
2108 }
2109
2110 thread_id = thread_tid(current_thread());
2111 prev_fe = NULL;
2112 KPKTQ_FOREACH_SAFE(pkt, pktq, tpkt) {
2113 if (__probable(tpkt)) {
2114 void *baddr;
2115 MD_BUFLET_ADDR_ABS_PKT(tpkt, baddr);
2116 SK_PREFETCH(baddr, 0);
2117 /* prefetch L3 and L4 flow structs */
2118 SK_PREFETCHW(tpkt->pkt_flow, 0);
2119 SK_PREFETCHW(tpkt->pkt_flow, 128);
2120 }
2121
2122 KPKTQ_REMOVE(pktq, pkt);
2123
2124 pkt = rx_prepare_packet(fsw, pkt);
2125
2126 af = fsw->fsw_demux(fsw, pkt);
2127 if (__improbable(af == AF_UNSPEC)) {
2128 KPKTQ_ENQUEUE(&host_pkts, pkt);
2129 continue;
2130 }
2131
2132 err = flow_pkt_classify(pkt, fsw->fsw_ifp, af, TRUE);
2133 _FSW_INJECT_ERROR(1, err, ENXIO, null_func);
2134 if (__improbable(err != 0)) {
2135 FSW_STATS_INC(FSW_STATS_RX_FLOW_EXTRACT_ERR);
2136 KPKTQ_ENQUEUE(&host_pkts, pkt);
2137 continue;
2138 }
2139
2140 if (__improbable(pkt->pkt_flow_ip_is_frag)) {
2141 pkt = rx_process_ip_frag(fsw, pkt);
2142 if (pkt == NULL) {
2143 continue;
2144 }
2145 }
2146
2147 prev_fe = fe = rx_lookup_flow(fsw, pkt, prev_fe);
2148 if (__improbable(fe == NULL)) {
2149 KPKTQ_ENQUEUE_LIST(&host_pkts, pkt);
2150 continue;
2151 }
2152
2153 dp_rx_process_wake_packet(fsw, pkt);
2154
2155 rx_flow_batch_packets(&fes, fe, pkt, thread_id);
2156 prev_fe = fe;
2157 }
2158
2159 struct flow_entry *tfe = NULL;
2160 TAILQ_FOREACH_SAFE(fe, &fes, fe_rx_link, tfe) {
2161 rx_flow_process(fsw, fe, &fes);
2162 flow_entry_release(&fe);
2163 }
2164
2165 if (!KPKTQ_EMPTY(&host_pkts)) {
2166 fsw_host_rx(fsw, &host_pkts);
2167 }
2168
2169 done:
2170 dp_drop_pktq(fsw, &dropped_pkts, 0, reason, line, 0);
2171 }
2172
2173 #if (DEVELOPMENT || DEBUG)
2174 static void
fsw_rps_rx(struct nx_flowswitch * fsw,uint32_t id,struct __kern_packet * pkt)2175 fsw_rps_rx(struct nx_flowswitch *fsw, uint32_t id,
2176 struct __kern_packet *pkt)
2177 {
2178 struct fsw_rps_thread *frt = &fsw->fsw_rps_threads[id];
2179
2180 lck_mtx_lock_spin(&frt->frt_lock);
2181 KPKTQ_ENQUEUE(&frt->frt_pktq, pkt);
2182 lck_mtx_unlock(&frt->frt_lock);
2183 }
2184
2185 static void
fsw_rps_thread_schedule(struct nx_flowswitch * fsw,uint32_t id)2186 fsw_rps_thread_schedule(struct nx_flowswitch *fsw, uint32_t id)
2187 {
2188 struct fsw_rps_thread *frt = &fsw->fsw_rps_threads[id];
2189
2190 ASSERT(frt->frt_thread != THREAD_NULL);
2191 lck_mtx_lock_spin(&frt->frt_lock);
2192 ASSERT(!(frt->frt_flags & (FRT_TERMINATING | FRT_TERMINATED)));
2193
2194 frt->frt_requests++;
2195 if (!(frt->frt_flags & FRT_RUNNING)) {
2196 thread_wakeup((caddr_t)frt);
2197 }
2198 lck_mtx_unlock(&frt->frt_lock);
2199 }
2200
2201 __attribute__((noreturn))
2202 static void
fsw_rps_thread_cont(void * v,wait_result_t w)2203 fsw_rps_thread_cont(void *v, wait_result_t w)
2204 {
2205 struct fsw_rps_thread *__single frt = v;
2206 struct nx_flowswitch *fsw = frt->frt_fsw;
2207
2208 lck_mtx_lock(&frt->frt_lock);
2209 if (__improbable(w == THREAD_INTERRUPTIBLE ||
2210 (frt->frt_flags & FRT_TERMINATING) != 0)) {
2211 goto terminate;
2212 }
2213 if (KPKTQ_EMPTY(&frt->frt_pktq)) {
2214 goto done;
2215 }
2216 frt->frt_flags |= FRT_RUNNING;
2217
2218 for (;;) {
2219 uint32_t requests = frt->frt_requests;
2220 struct pktq pkts;
2221
2222 KPKTQ_INIT(&pkts);
2223 KPKTQ_CONCAT(&pkts, &frt->frt_pktq);
2224 lck_mtx_unlock(&frt->frt_lock);
2225
2226 sk_protect_t protect;
2227 protect = sk_sync_protect();
2228 FSW_RLOCK(fsw);
2229 _fsw_receive_locked(fsw, &pkts);
2230 FSW_RUNLOCK(fsw);
2231 sk_sync_unprotect(protect);
2232
2233 lck_mtx_lock(&frt->frt_lock);
2234 if ((frt->frt_flags & FRT_TERMINATING) != 0 ||
2235 requests == frt->frt_requests) {
2236 frt->frt_requests = 0;
2237 break;
2238 }
2239 }
2240
2241 done:
2242 lck_mtx_unlock(&frt->frt_lock);
2243 if (!(frt->frt_flags & FRT_TERMINATING)) {
2244 frt->frt_flags &= ~FRT_RUNNING;
2245 assert_wait(frt, THREAD_UNINT);
2246 thread_block_parameter(fsw_rps_thread_cont, frt);
2247 __builtin_unreachable();
2248 } else {
2249 terminate:
2250 LCK_MTX_ASSERT(&frt->frt_lock, LCK_MTX_ASSERT_OWNED);
2251 frt->frt_flags &= ~(FRT_RUNNING | FRT_TERMINATING);
2252 frt->frt_flags |= FRT_TERMINATED;
2253
2254 if (frt->frt_flags & FRT_TERMINATEBLOCK) {
2255 thread_wakeup((caddr_t)&frt);
2256 }
2257 lck_mtx_unlock(&frt->frt_lock);
2258
2259 SK_D("fsw_rx_%s_%d terminated", if_name(fsw->fsw_ifp),
2260 frt->frt_idx);
2261
2262 /* for the extra refcnt from kernel_thread_start() */
2263 thread_deallocate(current_thread());
2264 /* this is the end */
2265 thread_terminate(current_thread());
2266 /* NOTREACHED */
2267 __builtin_unreachable();
2268 }
2269
2270 /* must never get here */
2271 VERIFY(0);
2272 /* NOTREACHED */
2273 __builtin_unreachable();
2274 }
2275
2276 __attribute__((noreturn))
2277 static void
fsw_rps_thread_func(void * v,wait_result_t w)2278 fsw_rps_thread_func(void *v, wait_result_t w)
2279 {
2280 #pragma unused(w)
2281 struct fsw_rps_thread *__single frt = v;
2282 struct nx_flowswitch *fsw = frt->frt_fsw;
2283 const char *__null_terminated tname = NULL;
2284
2285 char thread_name[MAXTHREADNAMESIZE];
2286 bzero(thread_name, sizeof(thread_name));
2287 tname = tsnprintf(thread_name, sizeof(thread_name), "fsw_rx_%s_%d",
2288 if_name(fsw->fsw_ifp), frt->frt_idx);
2289
2290 thread_set_thread_name(frt->frt_thread, tname);
2291 SK_D("%s spawned", tname);
2292
2293 net_thread_marks_push(NET_THREAD_SYNC_RX);
2294 assert_wait(frt, THREAD_UNINT);
2295 (void) thread_block_parameter(fsw_rps_thread_cont, frt);
2296
2297 __builtin_unreachable();
2298 }
2299
2300 static void
fsw_rps_thread_join(struct nx_flowswitch * fsw,uint32_t i)2301 fsw_rps_thread_join(struct nx_flowswitch *fsw, uint32_t i)
2302 {
2303 struct fsw_rps_thread *frt = &fsw->fsw_rps_threads[i];
2304 uint64_t f = (1 * NSEC_PER_MSEC);
2305 uint64_t s = (1000 * NSEC_PER_SEC);
2306 uint32_t c = 0;
2307
2308 lck_mtx_lock(&frt->frt_lock);
2309 frt->frt_flags |= FRT_TERMINATING;
2310
2311 while (!(frt->frt_flags & FRT_TERMINATED)) {
2312 uint64_t t = 0;
2313 nanoseconds_to_absolutetime((c++ == 0) ? f : s, &t);
2314 clock_absolutetime_interval_to_deadline(t, &t);
2315 ASSERT(t != 0);
2316
2317 frt->frt_flags |= FRT_TERMINATEBLOCK;
2318 if (!(frt->frt_flags & FRT_RUNNING)) {
2319 thread_wakeup_one((caddr_t)frt);
2320 }
2321 (void) assert_wait_deadline(&frt->frt_thread, THREAD_UNINT, t);
2322 lck_mtx_unlock(&frt->frt_lock);
2323 thread_block(THREAD_CONTINUE_NULL);
2324 lck_mtx_lock(&frt->frt_lock);
2325 frt->frt_flags &= ~FRT_TERMINATEBLOCK;
2326 }
2327 ASSERT(frt->frt_flags & FRT_TERMINATED);
2328 lck_mtx_unlock(&frt->frt_lock);
2329 frt->frt_thread = THREAD_NULL;
2330 }
2331
2332 static void
fsw_rps_thread_spawn(struct nx_flowswitch * fsw,uint32_t i)2333 fsw_rps_thread_spawn(struct nx_flowswitch *fsw, uint32_t i)
2334 {
2335 kern_return_t error;
2336 struct fsw_rps_thread *frt = &fsw->fsw_rps_threads[i];
2337
2338 lck_mtx_init(&frt->frt_lock, &nexus_lock_group, &nexus_lock_attr);
2339 frt->frt_idx = i;
2340 frt->frt_fsw = fsw;
2341 error = kernel_thread_start(fsw_rps_thread_func, frt, &frt->frt_thread);
2342 ASSERT(!error);
2343 KPKTQ_INIT(&frt->frt_pktq);
2344 }
2345
2346 int
fsw_rps_set_nthreads(struct nx_flowswitch * fsw,uint32_t n)2347 fsw_rps_set_nthreads(struct nx_flowswitch* fsw, uint32_t n)
2348 {
2349 if (n > FSW_RPS_MAX_NTHREADS) {
2350 SK_ERR("rps nthreads %d, max %d", n, FSW_RPS_MAX_NTHREADS);
2351 return EINVAL;
2352 }
2353
2354 FSW_WLOCK(fsw);
2355 if (n < fsw->fsw_rps_nthreads) {
2356 for (uint32_t i = n; i < fsw->fsw_rps_nthreads; i++) {
2357 fsw_rps_thread_join(fsw, i);
2358 }
2359 fsw->fsw_rps_threads = krealloc_type(struct fsw_rps_thread,
2360 fsw->fsw_rps_nthreads, n, fsw->fsw_rps_threads, Z_WAITOK | Z_ZERO | Z_NOFAIL);
2361 fsw->fsw_rps_nthreads = n;
2362 } else if (n > fsw->fsw_rps_nthreads) {
2363 uint32_t nthreads_old = fsw->fsw_rps_nthreads;
2364
2365 fsw->fsw_rps_threads = krealloc_type(struct fsw_rps_thread,
2366 fsw->fsw_rps_nthreads, n, fsw->fsw_rps_threads, Z_WAITOK | Z_ZERO | Z_NOFAIL);
2367 fsw->fsw_rps_nthreads = n;
2368 for (uint32_t i = nthreads_old; i < n; i++) {
2369 fsw_rps_thread_spawn(fsw, i);
2370 }
2371 }
2372 FSW_WUNLOCK(fsw);
2373 return 0;
2374 }
2375
2376 static uint32_t
get_rps_id(struct nx_flowswitch * fsw,struct __kern_packet * pkt)2377 get_rps_id(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
2378 {
2379 sa_family_t af = fsw->fsw_demux(fsw, pkt);
2380 if (__improbable(af == AF_UNSPEC)) {
2381 return 0;
2382 }
2383
2384 flow_pkt_classify(pkt, fsw->fsw_ifp, af, true);
2385
2386 if (__improbable((pkt->pkt_qum_qflags &
2387 QUM_F_FLOW_CLASSIFIED) == 0)) {
2388 return 0;
2389 }
2390
2391 struct flow_key key;
2392 flow_pkt2key(pkt, true, &key);
2393 key.fk_mask = FKMASK_5TUPLE;
2394
2395 uint32_t id = flow_key_hash(&key) % fsw->fsw_rps_nthreads;
2396
2397 return id;
2398 }
2399
2400 #endif /* !DEVELOPMENT && !DEBUG */
2401
2402 void
fsw_receive(struct nx_flowswitch * fsw,struct pktq * pktq)2403 fsw_receive(struct nx_flowswitch *fsw, struct pktq *pktq)
2404 {
2405 FSW_RLOCK(fsw);
2406 #if (DEVELOPMENT || DEBUG)
2407 if (fsw->fsw_rps_nthreads != 0) {
2408 struct __kern_packet *pkt, *tpkt;
2409 bitmap_t map = 0;
2410
2411 _CASSERT(BITMAP_LEN(FSW_RPS_MAX_NTHREADS) == 1);
2412 KPKTQ_FOREACH_SAFE(pkt, pktq, tpkt) {
2413 uint32_t id = get_rps_id(fsw, pkt);
2414 KPKTQ_REMOVE(pktq, pkt);
2415 fsw_rps_rx(fsw, id, pkt);
2416 bitmap_set(&map, id);
2417 }
2418 for (int i = bitmap_first(&map, 64); i >= 0;
2419 i = bitmap_next(&map, i)) {
2420 fsw_rps_thread_schedule(fsw, i);
2421 }
2422 } else
2423 #endif /* !DEVELOPMENT && !DEBUG */
2424 {
2425 _fsw_receive_locked(fsw, pktq);
2426 }
2427 FSW_RUNLOCK(fsw);
2428 }
2429
2430 int
fsw_dev_input_netem_dequeue(void * handle,pktsched_pkt_t * __counted_by (n_pkts)pkts,uint32_t n_pkts)2431 fsw_dev_input_netem_dequeue(void *handle,
2432 pktsched_pkt_t *__counted_by(n_pkts)pkts, uint32_t n_pkts)
2433 {
2434 #pragma unused(handle)
2435 struct nx_flowswitch *__single fsw = handle;
2436 struct __kern_packet *kpkts[FSW_VP_DEV_BATCH_MAX];
2437 struct pktq pktq;
2438 sk_protect_t protect;
2439 uint32_t i;
2440
2441 ASSERT(n_pkts <= FSW_VP_DEV_BATCH_MAX);
2442
2443 for (i = 0; i < n_pkts; i++) {
2444 ASSERT(pkts[i].pktsched_ptype == QP_PACKET);
2445 ASSERT(pkts[i].pktsched_pkt_kpkt != NULL);
2446 kpkts[i] = pkts[i].pktsched_pkt_kpkt;
2447 }
2448
2449 protect = sk_sync_protect();
2450 KPKTQ_INIT(&pktq);
2451 pkts_to_pktq(kpkts, n_pkts, &pktq);
2452
2453 fsw_receive(fsw, &pktq);
2454 KPKTQ_FINI(&pktq);
2455 sk_sync_unprotect(protect);
2456
2457 return 0;
2458 }
2459
2460 static void
fsw_dev_input_netem_enqueue(struct nx_flowswitch * fsw,struct pktq * q)2461 fsw_dev_input_netem_enqueue(struct nx_flowswitch *fsw, struct pktq *q)
2462 {
2463 classq_pkt_t p;
2464 struct netem *__single ne;
2465 struct __kern_packet *pkt, *tpkt;
2466
2467 ASSERT(fsw->fsw_ifp != NULL);
2468 ne = fsw->fsw_ifp->if_input_netem;
2469 ASSERT(ne != NULL);
2470 KPKTQ_FOREACH_SAFE(pkt, q, tpkt) {
2471 bool pdrop;
2472 KPKTQ_REMOVE(q, pkt);
2473 CLASSQ_PKT_INIT_PACKET(&p, pkt);
2474 netem_enqueue(ne, &p, &pdrop);
2475 }
2476 }
2477
2478 void
fsw_devna_rx(struct nexus_adapter * devna,struct __kern_packet * pkt_head,struct nexus_pkt_stats * out_stats)2479 fsw_devna_rx(struct nexus_adapter *devna, struct __kern_packet *pkt_head,
2480 struct nexus_pkt_stats *out_stats)
2481 {
2482 struct __kern_packet *pkt = pkt_head, *next;
2483 struct nx_flowswitch *fsw;
2484 uint32_t n_bytes = 0, n_pkts = 0;
2485 uint64_t total_pkts = 0, total_bytes = 0;
2486 struct pktq q;
2487
2488 KPKTQ_INIT(&q);
2489 if (__improbable(devna->na_ifp == NULL ||
2490 (fsw = fsw_ifp_to_fsw(devna->na_ifp)) == NULL)) {
2491 SK_ERR("fsw not attached, dropping %d pkts", KPKTQ_LEN(&q));
2492 dp_drop_pkt_chain(pkt_head, 0, DROP_REASON_FSW_QUIESCED, DROPTAP_FLAG_L2_MISSING);
2493 return;
2494 }
2495 while (pkt != NULL) {
2496 if (__improbable(pkt->pkt_trace_id != 0)) {
2497 KDBG(SK_KTRACE_PKT_RX_DRV | DBG_FUNC_END, pkt->pkt_trace_id);
2498 KDBG(SK_KTRACE_PKT_RX_FSW | DBG_FUNC_START, pkt->pkt_trace_id);
2499 }
2500 next = pkt->pkt_nextpkt;
2501 pkt->pkt_nextpkt = NULL;
2502
2503 if (__probable((pkt->pkt_qum_qflags & QUM_F_DROPPED) == 0)) {
2504 KPKTQ_ENQUEUE(&q, pkt);
2505 n_bytes += pkt->pkt_length;
2506 } else {
2507 DTRACE_SKYWALK1(non__finalized__drop,
2508 struct __kern_packet *, pkt);
2509 FSW_STATS_INC(FSW_STATS_RX_PKT_NOT_FINALIZED);
2510 dp_drop_pkt_single(fsw, pkt, 0,
2511 DROP_REASON_FSW_RX_PKT_NOT_FINALIZED,
2512 DROPTAP_FLAG_L2_MISSING);
2513 pkt = NULL;
2514 }
2515 n_pkts = KPKTQ_LEN(&q);
2516 if (n_pkts == fsw_rx_batch || (next == NULL && n_pkts > 0)) {
2517 if (__improbable(fsw->fsw_ifp->if_input_netem != NULL)) {
2518 fsw_dev_input_netem_enqueue(fsw, &q);
2519 } else {
2520 fsw_receive(fsw, &q);
2521 }
2522 total_pkts += n_pkts;
2523 total_bytes += n_bytes;
2524 n_pkts = 0;
2525 n_bytes = 0;
2526 KPKTQ_FINI(&q);
2527 }
2528 pkt = next;
2529 }
2530 ASSERT(KPKTQ_LEN(&q) == 0);
2531 FSW_STATS_ADD(FSW_STATS_RX_PACKETS, total_pkts);
2532 if (out_stats != NULL) {
2533 out_stats->nps_pkts += total_pkts;
2534 out_stats->nps_bytes += total_bytes;
2535 }
2536 KDBG(SK_KTRACE_FSW_DEV_RING_FLUSH, SK_KVA(devna), total_pkts, total_bytes);
2537 }
2538
2539 static int
dp_copy_to_dev_mbuf(struct nx_flowswitch * fsw,struct __kern_packet * spkt,struct __kern_packet * dpkt)2540 dp_copy_to_dev_mbuf(struct nx_flowswitch *fsw, struct __kern_packet *spkt,
2541 struct __kern_packet *dpkt)
2542 {
2543 struct mbuf *__single m = NULL;
2544 uint32_t bdlen, bdlim, bdoff;
2545 uint8_t *bdaddr;
2546 unsigned int one = 1;
2547 int err = 0;
2548
2549 err = mbuf_allocpacket(MBUF_DONTWAIT,
2550 (fsw->fsw_frame_headroom + spkt->pkt_length), &one, &m);
2551 #if (DEVELOPMENT || DEBUG)
2552 if (m != NULL) {
2553 _FSW_INJECT_ERROR(11, m, NULL, m_freem, m);
2554 }
2555 #endif /* DEVELOPMENT || DEBUG */
2556 if (__improbable(m == NULL)) {
2557 FSW_STATS_INC(FSW_STATS_DROP_NOMEM_MBUF);
2558 err = ENOBUFS;
2559 goto done;
2560 }
2561
2562 MD_BUFLET_ADDR_ABS_DLEN(dpkt, bdaddr, bdlen, bdlim, bdoff);
2563 if (fsw->fsw_frame_headroom > bdlim) {
2564 SK_ERR("not enough space in buffer for headroom");
2565 err = EINVAL;
2566 goto done;
2567 }
2568
2569 dpkt->pkt_headroom = fsw->fsw_frame_headroom;
2570 dpkt->pkt_mbuf = m;
2571 dpkt->pkt_pflags |= PKT_F_MBUF_DATA;
2572
2573 /* packet copy into mbuf */
2574 fsw->fsw_pkt_copy_to_mbuf(NR_TX, SK_PTR_ENCODE(spkt,
2575 METADATA_TYPE(spkt), METADATA_SUBTYPE(spkt)), 0, m,
2576 fsw->fsw_frame_headroom, spkt->pkt_length,
2577 PACKET_HAS_PARTIAL_CHECKSUM(spkt),
2578 spkt->pkt_csum_tx_start_off);
2579 FSW_STATS_INC(FSW_STATS_TX_COPY_PKT2MBUF);
2580
2581 /* header copy into dpkt buffer for classification */
2582 kern_packet_t sph = SK_PTR_ENCODE(spkt,
2583 METADATA_TYPE(spkt), METADATA_SUBTYPE(spkt));
2584 kern_packet_t dph = SK_PTR_ENCODE(dpkt,
2585 METADATA_TYPE(dpkt), METADATA_SUBTYPE(dpkt));
2586 uint32_t copy_len = MIN(spkt->pkt_length, bdlim - dpkt->pkt_headroom);
2587 fsw->fsw_pkt_copy_from_pkt(NR_TX, dph, dpkt->pkt_headroom,
2588 sph, spkt->pkt_headroom, copy_len, FALSE, 0, 0, 0);
2589 if (copy_len < spkt->pkt_length) {
2590 dpkt->pkt_pflags |= PKT_F_TRUNCATED;
2591 }
2592
2593 /*
2594 * fsw->fsw_frame_headroom is after m_data, thus we treat m_data same as
2595 * buflet baddr m_data always points to the beginning of packet and
2596 * should represents the same as baddr + headroom
2597 */
2598 ASSERT((uintptr_t)m->m_data ==
2599 ((uintptr_t)mbuf_datastart(m) + fsw->fsw_frame_headroom));
2600
2601 done:
2602 return err;
2603 }
2604
2605 static int
dp_copy_to_dev_pkt(struct nx_flowswitch * fsw,struct __kern_packet * spkt,struct __kern_packet * dpkt)2606 dp_copy_to_dev_pkt(struct nx_flowswitch *fsw, struct __kern_packet *spkt,
2607 struct __kern_packet *dpkt)
2608 {
2609 struct ifnet *ifp = fsw->fsw_ifp;
2610 uint16_t headroom = fsw->fsw_frame_headroom + ifp->if_tx_headroom;
2611
2612 if (headroom > UINT8_MAX) {
2613 SK_ERR("headroom too large %d", headroom);
2614 return ERANGE;
2615 }
2616 dpkt->pkt_headroom = (uint8_t)headroom;
2617 ASSERT((dpkt->pkt_headroom & 0x7) == 0);
2618 dpkt->pkt_l2_len = 0;
2619 dpkt->pkt_link_flags = spkt->pkt_link_flags;
2620
2621 kern_packet_t sph = SK_PTR_ENCODE(spkt,
2622 METADATA_TYPE(spkt), METADATA_SUBTYPE(spkt));
2623 kern_packet_t dph = SK_PTR_ENCODE(dpkt,
2624 METADATA_TYPE(dpkt), METADATA_SUBTYPE(dpkt));
2625 fsw->fsw_pkt_copy_from_pkt(NR_TX, dph,
2626 dpkt->pkt_headroom, sph, spkt->pkt_headroom,
2627 spkt->pkt_length, PACKET_HAS_PARTIAL_CHECKSUM(spkt),
2628 (spkt->pkt_csum_tx_start_off - spkt->pkt_headroom),
2629 (spkt->pkt_csum_tx_stuff_off - spkt->pkt_headroom),
2630 (spkt->pkt_csum_flags & PACKET_CSUM_ZERO_INVERT));
2631
2632 FSW_STATS_INC(FSW_STATS_TX_COPY_PKT2PKT);
2633
2634 return 0;
2635 }
2636
2637 #if SK_LOG
2638 /* Hoisted out of line to reduce kernel stack footprint */
2639 SK_LOG_ATTRIBUTE
2640 static void
dp_copy_to_dev_log(struct nx_flowswitch * fsw,const struct kern_pbufpool * pp,struct __kern_packet * spkt,struct __kern_packet * dpkt,int error)2641 dp_copy_to_dev_log(struct nx_flowswitch *fsw, const struct kern_pbufpool *pp,
2642 struct __kern_packet *spkt, struct __kern_packet *dpkt, int error)
2643 {
2644 struct proc *p = current_proc();
2645 struct ifnet *ifp = fsw->fsw_ifp;
2646 uint64_t logflags = (SK_VERB_FSW_DP | SK_VERB_TX);
2647
2648 if (error == ERANGE) {
2649 SK_ERR("packet too long, hr(fr+tx)+slen (%u+%u)+%u > "
2650 "dev_pp_max %u", (uint32_t)fsw->fsw_frame_headroom,
2651 (uint32_t)ifp->if_tx_headroom, spkt->pkt_length,
2652 (uint32_t)pp->pp_max_frags * PP_BUF_SIZE_DEF(pp));
2653 } else if (error == ENOBUFS) {
2654 SK_DF(logflags, "%s(%d) packet allocation failure",
2655 sk_proc_name_address(p), sk_proc_pid(p));
2656 } else if (error == 0) {
2657 ASSERT(dpkt != NULL);
2658 char *daddr;
2659 uint32_t pkt_len;
2660
2661 MD_BUFLET_ADDR_ABS(dpkt, daddr);
2662 pkt_len = __packet_get_real_data_length(dpkt);
2663 SK_DF(logflags, "%s(%d) splen %u dplen %u hr %u (fr/tx %u/%u)",
2664 sk_proc_name_address(p), sk_proc_pid(p), spkt->pkt_length,
2665 dpkt->pkt_length, (uint32_t)dpkt->pkt_headroom,
2666 (uint32_t)fsw->fsw_frame_headroom,
2667 (uint32_t)ifp->if_tx_headroom);
2668 SK_DF(logflags | SK_VERB_DUMP, "%s",
2669 sk_dump("buf", daddr, pkt_len, 128, NULL, 0));
2670 } else {
2671 SK_DF(logflags, "%s(%d) error %d", error);
2672 }
2673 }
2674 #else
2675 #define dp_copy_to_dev_log(...)
2676 #endif /* SK_LOG */
2677
2678 static void
fsw_pkt_copy_metadata(struct __kern_packet * spkt,struct __kern_packet * dpkt)2679 fsw_pkt_copy_metadata(struct __kern_packet *spkt, struct __kern_packet *dpkt)
2680 {
2681 ASSERT(!(spkt->pkt_pflags & PKT_F_MBUF_MASK));
2682 ASSERT(!(spkt->pkt_pflags & PKT_F_PKT_MASK));
2683
2684 SK_PREFETCHW(dpkt->pkt_qum_buf.buf_addr, 0);
2685 /* Copy packet metadata */
2686 _QUM_COPY(&(spkt)->pkt_qum, &(dpkt)->pkt_qum);
2687 _PKT_COPY(spkt, dpkt);
2688 _PKT_COPY_TX_PORT_DATA(spkt, dpkt);
2689 ASSERT((dpkt->pkt_qum.qum_qflags & QUM_F_KERNEL_ONLY) ||
2690 !PP_KERNEL_ONLY(dpkt->pkt_qum.qum_pp));
2691 ASSERT(dpkt->pkt_mbuf == NULL);
2692
2693 /* Copy AQM metadata */
2694 dpkt->pkt_flowsrc_type = spkt->pkt_flowsrc_type;
2695 dpkt->pkt_flowsrc_fidx = spkt->pkt_flowsrc_fidx;
2696 _CASSERT((offsetof(struct __flow, flow_src_id) % 8) == 0);
2697 _UUID_COPY(dpkt->pkt_flowsrc_id, spkt->pkt_flowsrc_id);
2698 _UUID_COPY(dpkt->pkt_policy_euuid, spkt->pkt_policy_euuid);
2699 dpkt->pkt_policy_id = spkt->pkt_policy_id;
2700 dpkt->pkt_skip_policy_id = spkt->pkt_skip_policy_id;
2701 }
2702
2703 static int
dp_copy_to_dev(struct nx_flowswitch * fsw,struct __kern_packet * spkt,struct __kern_packet * dpkt)2704 dp_copy_to_dev(struct nx_flowswitch *fsw, struct __kern_packet *spkt,
2705 struct __kern_packet *dpkt)
2706 {
2707 const struct kern_pbufpool *pp = dpkt->pkt_qum.qum_pp;
2708 struct ifnet *ifp = fsw->fsw_ifp;
2709 uint32_t dev_pkt_len;
2710 int err = 0;
2711
2712 fsw_pkt_copy_metadata(spkt, dpkt);
2713 switch (fsw->fsw_classq_enq_ptype) {
2714 case QP_MBUF:
2715 err = dp_copy_to_dev_mbuf(fsw, spkt, dpkt);
2716 break;
2717
2718 case QP_PACKET:
2719 dev_pkt_len = fsw->fsw_frame_headroom + ifp->if_tx_headroom +
2720 spkt->pkt_length;
2721 if (dev_pkt_len > pp->pp_max_frags * PP_BUF_SIZE_DEF(pp)) {
2722 FSW_STATS_INC(FSW_STATS_TX_COPY_BAD_LEN);
2723 err = ERANGE;
2724 goto done;
2725 }
2726 err = dp_copy_to_dev_pkt(fsw, spkt, dpkt);
2727 break;
2728
2729 default:
2730 VERIFY(0);
2731 __builtin_unreachable();
2732 }
2733 done:
2734 dp_copy_to_dev_log(fsw, pp, spkt, dpkt, err);
2735 return err;
2736 }
2737
2738 static int
dp_copy_headers_to_dev(struct nx_flowswitch * fsw,struct __kern_packet * spkt,struct __kern_packet * dpkt)2739 dp_copy_headers_to_dev(struct nx_flowswitch *fsw, struct __kern_packet *spkt,
2740 struct __kern_packet *dpkt)
2741 {
2742 uint8_t *sbaddr, *dbaddr;
2743 uint16_t headroom = fsw->fsw_frame_headroom + fsw->fsw_ifp->if_tx_headroom;
2744 uint16_t hdrs_len_estimate = (uint16_t)MIN(spkt->pkt_length, 128);
2745
2746 fsw_pkt_copy_metadata(spkt, dpkt);
2747
2748 MD_BUFLET_ADDR_ABS(spkt, sbaddr);
2749 ASSERT(sbaddr != NULL);
2750 sbaddr += spkt->pkt_headroom;
2751
2752 MD_BUFLET_ADDR_ABS(dpkt, dbaddr);
2753 ASSERT(dbaddr != NULL);
2754 dpkt->pkt_headroom = (uint8_t)headroom;
2755 dbaddr += headroom;
2756
2757 pkt_copy(sbaddr, dbaddr, hdrs_len_estimate);
2758 METADATA_SET_LEN(dpkt, hdrs_len_estimate, headroom);
2759
2760 /* packet length is set to the full length */
2761 dpkt->pkt_length = spkt->pkt_length;
2762 dpkt->pkt_pflags |= PKT_F_TRUNCATED;
2763 return 0;
2764 }
2765
2766 static struct mbuf *
convert_pkt_to_mbuf(struct __kern_packet * pkt)2767 convert_pkt_to_mbuf(struct __kern_packet *pkt)
2768 {
2769 ASSERT(pkt->pkt_pflags & PKT_F_MBUF_DATA);
2770 ASSERT(pkt->pkt_mbuf != NULL);
2771 struct mbuf *m = pkt->pkt_mbuf;
2772
2773 /* pass additional metadata generated from flow parse/lookup */
2774 _CASSERT(sizeof(m->m_pkthdr.pkt_flowid) ==
2775 sizeof(pkt->pkt_flow_token));
2776 _CASSERT(sizeof(m->m_pkthdr.pkt_mpriv_srcid) ==
2777 sizeof(pkt->pkt_flowsrc_token));
2778 _CASSERT(sizeof(m->m_pkthdr.pkt_mpriv_fidx) ==
2779 sizeof(pkt->pkt_flowsrc_fidx));
2780 m->m_pkthdr.pkt_svc = pkt->pkt_svc_class;
2781 m->m_pkthdr.pkt_proto = pkt->pkt_flow->flow_ip_proto;
2782 m->m_pkthdr.pkt_flowid = pkt->pkt_flow_token;
2783 m->m_pkthdr.comp_gencnt = pkt->pkt_comp_gencnt;
2784 m->m_pkthdr.pkt_flowsrc = pkt->pkt_flowsrc_type;
2785 m->m_pkthdr.pkt_mpriv_srcid = pkt->pkt_flowsrc_token;
2786 m->m_pkthdr.pkt_mpriv_fidx = pkt->pkt_flowsrc_fidx;
2787
2788 if (pkt->pkt_transport_protocol == IPPROTO_QUIC) {
2789 m->m_pkthdr.pkt_ext_flags |= PKTF_EXT_QUIC;
2790 }
2791
2792 /* The packet should have a timestamp by the time we get here. */
2793 m->m_pkthdr.pkt_timestamp = pkt->pkt_timestamp;
2794 m->m_pkthdr.pkt_flags &= ~PKTF_TS_VALID;
2795
2796 m->m_pkthdr.pkt_flags &= ~PKT_F_COMMON_MASK;
2797 m->m_pkthdr.pkt_flags |= (pkt->pkt_pflags & PKT_F_COMMON_MASK);
2798 /* set pkt_hdr so that AQM can find IP header and mark ECN bits */
2799 m->m_pkthdr.pkt_hdr = m_mtod_current(m) + pkt->pkt_l2_len;
2800
2801 if ((pkt->pkt_pflags & PKT_F_START_SEQ) != 0) {
2802 m->m_pkthdr.tx_start_seq = ntohl(pkt->pkt_flow_tcp_seq);
2803 }
2804 KPKT_CLEAR_MBUF_DATA(pkt);
2805
2806 /* mbuf has been consumed, release packet as well */
2807 ASSERT(pkt->pkt_qum.qum_ksd == NULL);
2808 pp_free_packet_single(pkt);
2809 return m;
2810 }
2811
2812 static void
convert_pkt_to_mbuf_list(struct __kern_packet * pkt_list,struct mbuf ** head,struct mbuf ** tail,uint32_t * cnt,uint32_t * bytes)2813 convert_pkt_to_mbuf_list(struct __kern_packet *pkt_list,
2814 struct mbuf **head, struct mbuf **tail,
2815 uint32_t *cnt, uint32_t *bytes)
2816 {
2817 struct __kern_packet *pkt = pkt_list, *next;
2818 struct mbuf *__single m_head = NULL, **__single m_tailp = &m_head;
2819 struct mbuf *__single m = NULL;
2820 uint32_t c = 0, b = 0;
2821
2822 while (pkt != NULL) {
2823 next = pkt->pkt_nextpkt;
2824 pkt->pkt_nextpkt = NULL;
2825 m = convert_pkt_to_mbuf(pkt);
2826 ASSERT(m != NULL);
2827
2828 *m_tailp = m;
2829 m_tailp = &m->m_nextpkt;
2830 c++;
2831 b += m_pktlen(m);
2832 pkt = next;
2833 }
2834 if (head != NULL) {
2835 *head = m_head;
2836 }
2837 if (tail != NULL) {
2838 *tail = m;
2839 }
2840 if (cnt != NULL) {
2841 *cnt = c;
2842 }
2843 if (bytes != NULL) {
2844 *bytes = b;
2845 }
2846 }
2847
2848 SK_NO_INLINE_ATTRIBUTE
2849 static int
classq_enqueue_flow_single(struct nx_flowswitch * fsw,struct __kern_packet * pkt)2850 classq_enqueue_flow_single(struct nx_flowswitch *fsw,
2851 struct __kern_packet *pkt)
2852 {
2853 struct ifnet *ifp = fsw->fsw_ifp;
2854 boolean_t pkt_drop = FALSE;
2855 int err;
2856
2857 FSW_LOCK_ASSERT_HELD(fsw);
2858 ASSERT(fsw->fsw_classq_enabled);
2859 ASSERT(pkt->pkt_flow_token != 0);
2860 fsw_ifp_inc_traffic_class_out_pkt(ifp, pkt->pkt_svc_class,
2861 1, pkt->pkt_length);
2862
2863 if (__improbable(pkt->pkt_trace_id != 0)) {
2864 KDBG(SK_KTRACE_PKT_TX_FSW | DBG_FUNC_END, pkt->pkt_trace_id);
2865 KDBG(SK_KTRACE_PKT_TX_AQM | DBG_FUNC_START, pkt->pkt_trace_id);
2866 }
2867
2868 switch (fsw->fsw_classq_enq_ptype) {
2869 case QP_MBUF: { /* compat interface */
2870 struct mbuf *m;
2871
2872 m = convert_pkt_to_mbuf(pkt);
2873 ASSERT(m != NULL);
2874 pkt = NULL;
2875
2876 /* ifnet_enqueue consumes mbuf */
2877 err = ifnet_enqueue_mbuf(ifp, m, false, &pkt_drop);
2878 m = NULL;
2879 #if (DEVELOPMENT || DEBUG)
2880 if (__improbable(!pkt_drop)) {
2881 _FSW_INJECT_ERROR(14, pkt_drop, TRUE, null_func);
2882 }
2883 #endif /* DEVELOPMENT || DEBUG */
2884 if (pkt_drop) {
2885 FSW_STATS_INC(FSW_STATS_DROP);
2886 FSW_STATS_INC(FSW_STATS_TX_AQM_DROP);
2887 }
2888 break;
2889 }
2890 case QP_PACKET: { /* native interface */
2891 /* ifnet_enqueue consumes packet */
2892 err = ifnet_enqueue_pkt(ifp, pkt, false, &pkt_drop);
2893 pkt = NULL;
2894 #if (DEVELOPMENT || DEBUG)
2895 if (__improbable(!pkt_drop)) {
2896 _FSW_INJECT_ERROR(14, pkt_drop, TRUE, null_func);
2897 }
2898 #endif /* DEVELOPMENT || DEBUG */
2899 if (pkt_drop) {
2900 FSW_STATS_INC(FSW_STATS_DROP);
2901 FSW_STATS_INC(FSW_STATS_TX_AQM_DROP);
2902 }
2903 break;
2904 }
2905 default:
2906 err = EINVAL;
2907 VERIFY(0);
2908 /* NOTREACHED */
2909 __builtin_unreachable();
2910 }
2911
2912 return err;
2913 }
2914
2915 static int
classq_enqueue_flow_chain(struct nx_flowswitch * fsw,struct __kern_packet * pkt_head,struct __kern_packet * pkt_tail,uint32_t cnt,uint32_t bytes)2916 classq_enqueue_flow_chain(struct nx_flowswitch *fsw,
2917 struct __kern_packet *pkt_head, struct __kern_packet *pkt_tail,
2918 uint32_t cnt, uint32_t bytes)
2919 {
2920 struct ifnet *ifp = fsw->fsw_ifp;
2921 boolean_t pkt_drop = FALSE;
2922 uint32_t svc;
2923 int err;
2924
2925 FSW_LOCK_ASSERT_HELD(fsw);
2926 ASSERT(fsw->fsw_classq_enabled);
2927 ASSERT(pkt_head->pkt_flow_token != 0);
2928
2929 /*
2930 * All packets in the flow should have the same svc.
2931 */
2932 svc = pkt_head->pkt_svc_class;
2933 fsw_ifp_inc_traffic_class_out_pkt(ifp, svc, cnt, bytes);
2934
2935 switch (fsw->fsw_classq_enq_ptype) {
2936 case QP_MBUF: { /* compat interface */
2937 struct mbuf *__single m_head = NULL, *__single m_tail = NULL;
2938 uint32_t c = 0, b = 0;
2939
2940 convert_pkt_to_mbuf_list(pkt_head, &m_head, &m_tail, &c, &b);
2941 ASSERT(m_head != NULL && m_tail != NULL);
2942 ASSERT(c == cnt);
2943 ASSERT(b == bytes);
2944 pkt_head = NULL;
2945
2946 /* ifnet_enqueue consumes mbuf */
2947 err = ifnet_enqueue_mbuf_chain(ifp, m_head, m_tail, cnt,
2948 bytes, FALSE, &pkt_drop);
2949 m_head = NULL;
2950 m_tail = NULL;
2951 #if (DEVELOPMENT || DEBUG)
2952 if (__improbable(!pkt_drop)) {
2953 _FSW_INJECT_ERROR(14, pkt_drop, TRUE, null_func);
2954 }
2955 #endif /* DEVELOPMENT || DEBUG */
2956 if (pkt_drop) {
2957 STATS_ADD(&fsw->fsw_stats, FSW_STATS_DROP, cnt);
2958 STATS_ADD(&fsw->fsw_stats, FSW_STATS_TX_AQM_DROP,
2959 cnt);
2960 }
2961 break;
2962 }
2963 case QP_PACKET: { /* native interface */
2964 /* ifnet_enqueue consumes packet */
2965 err = ifnet_enqueue_pkt_chain(ifp, pkt_head, pkt_tail, cnt,
2966 bytes, FALSE, &pkt_drop);
2967 pkt_head = NULL;
2968 #if (DEVELOPMENT || DEBUG)
2969 if (__improbable(!pkt_drop)) {
2970 _FSW_INJECT_ERROR(14, pkt_drop, TRUE, null_func);
2971 }
2972 #endif /* DEVELOPMENT || DEBUG */
2973 if (pkt_drop) {
2974 STATS_ADD(&fsw->fsw_stats, FSW_STATS_DROP, cnt);
2975 STATS_ADD(&fsw->fsw_stats, FSW_STATS_TX_AQM_DROP,
2976 cnt);
2977 }
2978 break;
2979 }
2980 default:
2981 err = EINVAL;
2982 VERIFY(0);
2983 /* NOTREACHED */
2984 __builtin_unreachable();
2985 }
2986
2987 return err;
2988 }
2989
2990 /*
2991 * This code path needs to be kept for interfaces without logical link support.
2992 */
2993 static void
classq_enqueue_flow(struct nx_flowswitch * fsw,struct flow_entry * fe,bool chain,uint32_t cnt,uint32_t bytes)2994 classq_enqueue_flow(struct nx_flowswitch *fsw, struct flow_entry *fe,
2995 bool chain, uint32_t cnt, uint32_t bytes)
2996 {
2997 struct __kern_packet *pkt, *tail, *tpkt;
2998 flowadv_idx_t flow_adv_idx;
2999 bool flowadv_cap;
3000 flowadv_token_t flow_adv_token;
3001 int err;
3002
3003 SK_DF(SK_VERB_FSW_DP | SK_VERB_AQM, "%s classq enqueued %d pkts",
3004 if_name(fsw->fsw_ifp), KPKTQ_LEN(&fe->fe_tx_pktq));
3005
3006 if (chain) {
3007 pkt = KPKTQ_FIRST(&fe->fe_tx_pktq);
3008 tail = KPKTQ_LAST(&fe->fe_tx_pktq);
3009 KPKTQ_INIT(&fe->fe_tx_pktq);
3010 if (pkt == NULL) {
3011 return;
3012 }
3013 flow_adv_idx = pkt->pkt_flowsrc_fidx;
3014 flowadv_cap = ((pkt->pkt_pflags & PKT_F_FLOW_ADV) != 0);
3015 flow_adv_token = pkt->pkt_flow_token;
3016
3017 err = classq_enqueue_flow_chain(fsw, pkt, tail, cnt, bytes);
3018 DTRACE_SKYWALK3(chain__enqueue, uint32_t, cnt, uint32_t, bytes, int, err);
3019 } else {
3020 uint32_t c = 0, b = 0;
3021
3022 KPKTQ_FOREACH_SAFE(pkt, &fe->fe_tx_pktq, tpkt) {
3023 KPKTQ_REMOVE(&fe->fe_tx_pktq, pkt);
3024
3025 flow_adv_idx = pkt->pkt_flowsrc_fidx;
3026 flowadv_cap = ((pkt->pkt_pflags & PKT_F_FLOW_ADV) != 0);
3027 flow_adv_token = pkt->pkt_flow_token;
3028
3029 c++;
3030 b += pkt->pkt_length;
3031 err = classq_enqueue_flow_single(fsw, pkt);
3032 }
3033 ASSERT(c == cnt);
3034 ASSERT(b == bytes);
3035 DTRACE_SKYWALK3(non__chain__enqueue, uint32_t, cnt, uint32_t, bytes,
3036 int, err);
3037 }
3038 }
3039
3040 /*
3041 * Logical link code path
3042 */
3043 static void
classq_qset_enqueue_flow(struct nx_flowswitch * fsw,struct flow_entry * fe,bool chain,uint32_t cnt,uint32_t bytes)3044 classq_qset_enqueue_flow(struct nx_flowswitch *fsw, struct flow_entry *fe,
3045 bool chain, uint32_t cnt, uint32_t bytes)
3046 {
3047 #pragma unused(chain)
3048 struct __kern_packet *pkt, *tail;
3049 flowadv_idx_t flow_adv_idx;
3050 bool flowadv_cap;
3051 flowadv_token_t flow_adv_token;
3052 uint32_t flowctl = 0, dropped = 0;
3053 int err;
3054
3055 SK_DF(SK_VERB_FSW_DP | SK_VERB_AQM, "%s classq enqueued %d pkts",
3056 if_name(fsw->fsw_ifp), KPKTQ_LEN(&fe->fe_tx_pktq));
3057
3058 pkt = KPKTQ_FIRST(&fe->fe_tx_pktq);
3059 tail = KPKTQ_LAST(&fe->fe_tx_pktq);
3060 KPKTQ_INIT(&fe->fe_tx_pktq);
3061 if (pkt == NULL) {
3062 return;
3063 }
3064 flow_adv_idx = pkt->pkt_flowsrc_fidx;
3065 flowadv_cap = ((pkt->pkt_pflags & PKT_F_FLOW_ADV) != 0);
3066 flow_adv_token = pkt->pkt_flow_token;
3067
3068 err = netif_qset_enqueue(fe->fe_qset, pkt, tail, cnt, bytes,
3069 &flowctl, &dropped);
3070
3071 if (__improbable(err != 0) && dropped > 0) {
3072 STATS_ADD(&fsw->fsw_stats, FSW_STATS_DROP, dropped);
3073 STATS_ADD(&fsw->fsw_stats, FSW_STATS_TX_AQM_DROP, dropped);
3074 }
3075 }
3076
3077 static void
tx_finalize_packet(struct nx_flowswitch * fsw,struct __kern_packet * pkt)3078 tx_finalize_packet(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
3079 {
3080 #pragma unused(fsw)
3081 /* finalize here; no more changes to buflets after classq */
3082 if (__probable(!(pkt->pkt_pflags & PKT_F_MBUF_DATA))) {
3083 kern_packet_t ph = SK_PTR_ENCODE(pkt,
3084 METADATA_TYPE(pkt), METADATA_SUBTYPE(pkt));
3085 int err = __packet_finalize(ph);
3086 VERIFY(err == 0);
3087 }
3088 }
3089
3090 static bool
dp_flow_tx_route_process(struct nx_flowswitch * fsw,struct flow_entry * fe)3091 dp_flow_tx_route_process(struct nx_flowswitch *fsw, struct flow_entry *fe)
3092 {
3093 struct flow_route *fr = fe->fe_route;
3094 int err;
3095
3096 ASSERT(fr != NULL);
3097
3098 if (__improbable(!dp_flow_route_process(fsw, fe))) {
3099 return false;
3100 }
3101 if (fe->fe_qset_select == FE_QSET_SELECT_DYNAMIC) {
3102 flow_qset_select_dynamic(fsw, fe, TRUE);
3103 }
3104
3105 _FSW_INJECT_ERROR(35, fr->fr_flags, fr->fr_flags,
3106 _fsw_error35_handler, 1, fr, NULL, NULL);
3107 _FSW_INJECT_ERROR(36, fr->fr_flags, fr->fr_flags,
3108 _fsw_error36_handler, 1, fr, NULL);
3109
3110 /*
3111 * See if we need to resolve the flow route; note the test against
3112 * fr_flags here is done without any lock for performance. Thus
3113 * it's possible that we race against the thread performing route
3114 * event updates for a packet (which is OK). In any case we should
3115 * not have any assertion on fr_flags value(s) due to the lack of
3116 * serialization.
3117 */
3118 if (fr->fr_flags & FLOWRTF_RESOLVED) {
3119 goto frame;
3120 }
3121
3122 struct __kern_packet *pkt, *tpkt;
3123 KPKTQ_FOREACH_SAFE(pkt, &fe->fe_tx_pktq, tpkt) {
3124 err = fsw->fsw_resolve(fsw, fr, pkt);
3125 _FSW_INJECT_ERROR_SET(35, _fsw_error35_handler, 2, fr, pkt, &err);
3126 _FSW_INJECT_ERROR_SET(36, _fsw_error36_handler, 2, fr, &err);
3127 /*
3128 * If resolver returns EJUSTRETURN then we drop the pkt as the
3129 * resolver should have converted the pkt into mbuf (or
3130 * detached the attached mbuf from pkt) and added it to the
3131 * llinfo queue. If we do have a cached llinfo, then proceed
3132 * to using it even though it may be stale (very unlikely)
3133 * while the resolution is in progress.
3134 * Otherwise, any other error results in dropping pkt.
3135 */
3136 if (err == EJUSTRETURN) {
3137 KPKTQ_REMOVE(&fe->fe_tx_pktq, pkt);
3138 pp_free_packet_single(pkt);
3139 FSW_STATS_INC(FSW_STATS_TX_RESOLV_PENDING);
3140 continue;
3141 } else if (err != 0 && (fr->fr_flags & FLOWRTF_HAS_LLINFO)) {
3142 /* use existing llinfo */
3143 FSW_STATS_INC(FSW_STATS_TX_RESOLV_STALE);
3144 } else if (err != 0) {
3145 KPKTQ_REMOVE(&fe->fe_tx_pktq, pkt);
3146 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_TX_RESOLV_FAILED,
3147 DROPTAP_FLAG_L2_MISSING);
3148 FSW_STATS_INC(FSW_STATS_TX_RESOLV_FAIL);
3149 continue;
3150 }
3151 }
3152
3153 frame:
3154 KPKTQ_FOREACH_SAFE(pkt, &fe->fe_tx_pktq, tpkt) {
3155 if (fsw->fsw_frame != NULL) {
3156 fsw->fsw_frame(fsw, fr, pkt);
3157 }
3158 }
3159
3160 return true;
3161 }
3162
3163 static void
dp_listener_flow_tx_process(struct nx_flowswitch * fsw,struct flow_entry * fe)3164 dp_listener_flow_tx_process(struct nx_flowswitch *fsw, struct flow_entry *fe)
3165 {
3166 #pragma unused(fsw)
3167 struct __kern_packet *pkt, *tpkt;
3168 KPKTQ_FOREACH_SAFE(pkt, &fe->fe_tx_pktq, tpkt) {
3169 KPKTQ_REMOVE(&fe->fe_tx_pktq, pkt);
3170 /* listener is only allowed TCP RST */
3171 if (pkt->pkt_flow_ip_proto == IPPROTO_TCP &&
3172 (pkt->pkt_flow_tcp_flags & TH_RST) != 0) {
3173 flow_track_abort_tcp(fe, NULL, pkt);
3174 } else {
3175 char *addr;
3176
3177 MD_BUFLET_ADDR_ABS(pkt, addr);
3178 SK_ERR("listener flow sends non-RST packet %s",
3179 sk_dump(sk_proc_name_address(current_proc()),
3180 addr, __packet_get_real_data_length(pkt), 128, NULL, 0));
3181 }
3182 pp_free_packet_single(pkt);
3183 }
3184 }
3185
3186 static void
fsw_update_timestamps(struct __kern_packet * pkt,volatile uint64_t * fg_ts,volatile uint64_t * rt_ts,ifnet_t ifp)3187 fsw_update_timestamps(struct __kern_packet *pkt, volatile uint64_t *fg_ts,
3188 volatile uint64_t *rt_ts, ifnet_t ifp)
3189 {
3190 struct timespec now;
3191 uint64_t now_nsec = 0;
3192
3193 if (!(pkt->pkt_pflags & PKT_F_TS_VALID) || pkt->pkt_timestamp == 0) {
3194 nanouptime(&now);
3195 net_timernsec(&now, &now_nsec);
3196 pkt->pkt_timestamp = now_nsec;
3197 }
3198 pkt->pkt_pflags &= ~PKT_F_TS_VALID;
3199
3200 /*
3201 * If the packet service class is not background,
3202 * update the timestamps on the interface, as well as
3203 * the ones in nexus-wide advisory to indicate recent
3204 * activity on a foreground flow.
3205 */
3206 if (!(pkt->pkt_pflags & PKT_F_BACKGROUND)) {
3207 ifp->if_fg_sendts = (uint32_t)_net_uptime;
3208 if (fg_ts != NULL) {
3209 *fg_ts = _net_uptime;
3210 }
3211 }
3212 if (pkt->pkt_pflags & PKT_F_REALTIME) {
3213 ifp->if_rt_sendts = (uint32_t)_net_uptime;
3214 if (rt_ts != NULL) {
3215 *rt_ts = _net_uptime;
3216 }
3217 }
3218 }
3219
3220 static bool
fsw_chain_enqueue_enabled(struct nx_flowswitch * fsw,bool gso_enabled)3221 fsw_chain_enqueue_enabled(struct nx_flowswitch *fsw, bool gso_enabled)
3222 {
3223 return fsw_chain_enqueue != 0 &&
3224 fsw->fsw_ifp->if_output_netem == NULL &&
3225 (fsw->fsw_ifp->if_eflags & IFEF_ENQUEUE_MULTI) == 0 &&
3226 gso_enabled;
3227 }
3228
3229 void
dp_flow_tx_process(struct nx_flowswitch * fsw,struct flow_entry * fe,uint32_t flags)3230 dp_flow_tx_process(struct nx_flowswitch *fsw, struct flow_entry *fe,
3231 uint32_t flags)
3232 {
3233 struct pktq dropped_pkts;
3234 bool chain, gso = ((flags & FLOW_PROC_FLAG_GSO) != 0);
3235 uint32_t cnt = 0, bytes = 0;
3236 volatile struct sk_nexusadv *nxadv = NULL;
3237 volatile uint64_t *fg_ts = NULL;
3238 volatile uint64_t *rt_ts = NULL;
3239 uint8_t qset_idx = (fe->fe_qset != NULL) ? fe->fe_qset->nqs_idx : 0;
3240 drop_reason_t reason = DROP_REASON_UNSPECIFIED;
3241 uint16_t line = 0;
3242
3243 KPKTQ_INIT(&dropped_pkts);
3244 ASSERT(!KPKTQ_EMPTY(&fe->fe_tx_pktq));
3245 if (__improbable(fe->fe_flags & FLOWENTF_LISTENER)) {
3246 dp_listener_flow_tx_process(fsw, fe);
3247 return;
3248 }
3249 if (__improbable(!dp_flow_tx_route_process(fsw, fe))) {
3250 SK_RDERR(5, "Tx route bad");
3251 FSW_STATS_ADD(FSW_STATS_TX_FLOW_NONVIABLE,
3252 KPKTQ_LEN(&fe->fe_tx_pktq));
3253 KPKTQ_CONCAT(&dropped_pkts, &fe->fe_tx_pktq);
3254 reason = DROP_REASON_FSW_FLOW_NONVIABLE;
3255 line = __LINE__;
3256 goto done;
3257 }
3258 chain = fsw_chain_enqueue_enabled(fsw, gso);
3259 if (chain) {
3260 nxadv = fsw->fsw_nx->nx_adv.flowswitch_nxv_adv;
3261 if (nxadv != NULL) {
3262 fg_ts = &nxadv->nxadv_fg_sendts;
3263 rt_ts = &nxadv->nxadv_rt_sendts;
3264 }
3265 }
3266 struct __kern_packet *pkt, *tpkt;
3267 KPKTQ_FOREACH_SAFE(pkt, &fe->fe_tx_pktq, tpkt) {
3268 int err = 0;
3269
3270 err = flow_pkt_track(fe, pkt, false);
3271 if (__improbable(err != 0)) {
3272 SK_RDERR(5, "flow_pkt_track failed (err %d)", err);
3273 FSW_STATS_INC(FSW_STATS_TX_FLOW_TRACK_ERR);
3274 KPKTQ_REMOVE(&fe->fe_tx_pktq, pkt);
3275 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_FLOW_TRACK_ERR,
3276 DROPTAP_FLAG_L2_MISSING);
3277 continue;
3278 }
3279 _UUID_COPY(pkt->pkt_policy_euuid, fe->fe_eproc_uuid);
3280 pkt->pkt_transport_protocol = fe->fe_transport_protocol;
3281
3282 /* set AQM related values for outgoing packet */
3283 if (fe->fe_adv_idx != FLOWADV_IDX_NONE) {
3284 pkt->pkt_pflags |= PKT_F_FLOW_ADV;
3285 pkt->pkt_flowsrc_type = FLOWSRC_CHANNEL;
3286 pkt->pkt_flowsrc_fidx = fe->fe_adv_idx;
3287 } else {
3288 pkt->pkt_pflags &= ~PKT_F_FLOW_ADV;
3289 }
3290 _UUID_CLEAR(pkt->pkt_flow_id);
3291 pkt->pkt_flow_token = fe->fe_flowid;
3292 pkt->pkt_pflags |= PKT_F_FLOW_ID;
3293 pkt->pkt_qset_idx = qset_idx;
3294 pkt->pkt_policy_id = fe->fe_policy_id;
3295 pkt->pkt_skip_policy_id = fe->fe_skip_policy_id;
3296
3297 /*
3298 * The same code is exercised per packet for the non-chain case
3299 * (see ifnet_enqueue_ifclassq()). It's replicated here to avoid
3300 * re-walking the chain later.
3301 */
3302 if (chain) {
3303 fsw_update_timestamps(pkt, fg_ts, rt_ts, fsw->fsw_ifp);
3304 }
3305 /* mark packet tos/svc_class */
3306 fsw_qos_mark(fsw, fe, pkt);
3307
3308 tx_finalize_packet(fsw, pkt);
3309 bytes += pkt->pkt_length;
3310 cnt++;
3311 }
3312
3313 /* snoop after it's finalized */
3314 if (__improbable(pktap_total_tap_count != 0)) {
3315 fsw_snoop(fsw, fe, &fe->fe_tx_pktq, false);
3316 }
3317 if (fe->fe_qset != NULL) {
3318 classq_qset_enqueue_flow(fsw, fe, chain, cnt, bytes);
3319 } else {
3320 classq_enqueue_flow(fsw, fe, chain, cnt, bytes);
3321 }
3322 done:
3323 dp_drop_pktq(fsw, &dropped_pkts, 1, reason, line, 0);
3324 }
3325
3326 static struct flow_entry *
tx_process_continuous_ip_frag(struct nx_flowswitch * fsw,struct flow_entry * prev_fe,struct __kern_packet * pkt)3327 tx_process_continuous_ip_frag(struct nx_flowswitch *fsw,
3328 struct flow_entry *prev_fe, struct __kern_packet *pkt)
3329 {
3330 ASSERT(!pkt->pkt_flow_ip_is_first_frag);
3331
3332 if (__improbable(pkt->pkt_flow_ip_frag_id == 0)) {
3333 FSW_STATS_INC(FSW_STATS_TX_FRAG_BAD_ID);
3334 SK_ERR("%s(%d) invalid zero fragment id",
3335 sk_proc_name_address(current_proc()),
3336 sk_proc_pid(current_proc()));
3337 return NULL;
3338 }
3339
3340 SK_DF(SK_VERB_FSW_DP | SK_VERB_TX,
3341 "%s(%d) continuation frag, id %u",
3342 sk_proc_name_address(current_proc()),
3343 sk_proc_pid(current_proc()),
3344 pkt->pkt_flow_ip_frag_id);
3345 if (__improbable(prev_fe == NULL ||
3346 !prev_fe->fe_tx_is_cont_frag)) {
3347 SK_ERR("%s(%d) unexpected continuation frag",
3348 sk_proc_name_address(current_proc()),
3349 sk_proc_pid(current_proc()),
3350 pkt->pkt_flow_ip_frag_id);
3351 FSW_STATS_INC(FSW_STATS_TX_FRAG_BAD_CONT);
3352 return NULL;
3353 }
3354 if (__improbable(pkt->pkt_flow_ip_frag_id !=
3355 prev_fe->fe_tx_frag_id)) {
3356 FSW_STATS_INC(FSW_STATS_TX_FRAG_BAD_CONT);
3357 SK_ERR("%s(%d) wrong continuation frag id %u expecting %u",
3358 sk_proc_name_address(current_proc()),
3359 sk_proc_pid(current_proc()),
3360 pkt->pkt_flow_ip_frag_id,
3361 prev_fe->fe_tx_frag_id);
3362 return NULL;
3363 }
3364
3365 return prev_fe;
3366 }
3367
3368 static struct flow_entry *
tx_lookup_flow(struct nx_flowswitch * fsw,struct __kern_packet * pkt,struct flow_entry * prev_fe)3369 tx_lookup_flow(struct nx_flowswitch *fsw, struct __kern_packet *pkt,
3370 struct flow_entry *prev_fe)
3371 {
3372 struct flow_entry *__single fe;
3373
3374 fe = lookup_flow_with_pkt(fsw, pkt, false, prev_fe);
3375 if (__improbable(fe == NULL)) {
3376 goto done;
3377 }
3378
3379 if (__improbable(fe->fe_flags & FLOWENTF_TORN_DOWN)) {
3380 SK_RDERR(5, "Tx flow torn down");
3381 FSW_STATS_INC(FSW_STATS_TX_FLOW_TORNDOWN);
3382 flow_entry_release(&fe);
3383 goto done;
3384 }
3385
3386 _FSW_INJECT_ERROR(34, pkt->pkt_flow_id[0], fe->fe_uuid[0] + 1,
3387 null_func);
3388
3389 if (__improbable(!_UUID_MATCH(pkt->pkt_flow_id, fe->fe_uuid))) {
3390 uuid_string_t flow_id_str, pkt_id_str;
3391 sk_uuid_unparse(fe->fe_uuid, flow_id_str);
3392 sk_uuid_unparse(pkt->pkt_flow_id, pkt_id_str);
3393 SK_ERR("pkt flow id %s != flow id %s", pkt_id_str, flow_id_str);
3394 flow_entry_release(&fe);
3395 FSW_STATS_INC(FSW_STATS_TX_FLOW_BAD_ID);
3396 }
3397
3398 done:
3399 return fe;
3400 }
3401
3402 static inline void
tx_flow_process(struct nx_flowswitch * fsw,struct flow_entry * fe,uint32_t flags)3403 tx_flow_process(struct nx_flowswitch *fsw, struct flow_entry *fe,
3404 uint32_t flags)
3405 {
3406 ASSERT(!KPKTQ_EMPTY(&fe->fe_tx_pktq));
3407 ASSERT(KPKTQ_LEN(&fe->fe_tx_pktq) != 0);
3408
3409 SK_DF(SK_VERB_FSW_DP | SK_VERB_TX, "TX %d pkts from fe %p port %d",
3410 KPKTQ_LEN(&fe->fe_tx_pktq), fe, fe->fe_nx_port);
3411
3412 /* flow related processing (default, agg, etc.) */
3413 fe->fe_tx_process(fsw, fe, flags);
3414
3415 KPKTQ_FINI(&fe->fe_tx_pktq);
3416 }
3417
3418 #if SK_LOG
3419 static void
dp_tx_log_pkt(uint64_t verb,char * desc,struct __kern_packet * pkt)3420 dp_tx_log_pkt(uint64_t verb, char *desc, struct __kern_packet *pkt)
3421 {
3422 char *pkt_buf;
3423 uint32_t pkt_len;
3424
3425 MD_BUFLET_ADDR_ABS(pkt, pkt_buf);
3426 pkt_len = __packet_get_real_data_length(pkt);
3427 SK_DF(verb, "%s(%d) %s %s", sk_proc_name_address(current_proc()),
3428 sk_proc_pid(current_proc()), desc, sk_dump("buf", pkt_buf, pkt_len,
3429 128, NULL, 0));
3430 }
3431 #else /* !SK_LOG */
3432 #define dp_tx_log_pkt(...)
3433 #endif /* !SK_LOG */
3434
3435 static inline struct ifnet *
fsw_datamov_begin(struct nx_flowswitch * fsw)3436 fsw_datamov_begin(struct nx_flowswitch *fsw)
3437 {
3438 struct ifnet *ifp;
3439
3440 ifp = fsw->fsw_ifp;
3441 if (!ifnet_datamov_begin(ifp)) {
3442 DTRACE_SKYWALK1(ifnet__detached, struct ifnet *, ifp);
3443 return NULL;
3444 }
3445 return ifp;
3446 }
3447
3448 static inline void
fsw_datamov_end(struct nx_flowswitch * fsw)3449 fsw_datamov_end(struct nx_flowswitch *fsw)
3450 {
3451 ifnet_datamov_end(fsw->fsw_ifp);
3452 }
3453
3454 static void
dp_tx_pktq(struct nx_flowswitch * fsw,struct pktq * spktq)3455 dp_tx_pktq(struct nx_flowswitch *fsw, struct pktq *spktq)
3456 {
3457 struct __kern_packet *spkt, *pkt;
3458 struct flow_entry_list fes = TAILQ_HEAD_INITIALIZER(fes);
3459 struct flow_entry *__single fe, *__single prev_fe;
3460 struct pktq dropped_pkts, dpktq;
3461 struct nexus_adapter *dev_na;
3462 struct kern_pbufpool *dev_pp;
3463 struct ifnet *ifp = NULL;
3464 sa_family_t af;
3465 uint32_t n_pkts, n_flows = 0;
3466 boolean_t do_pacing = FALSE;
3467 drop_reason_t reason = DROP_REASON_UNSPECIFIED;
3468 uint16_t line = 0;
3469
3470 int err;
3471 KPKTQ_INIT(&dpktq);
3472 KPKTQ_INIT(&dropped_pkts);
3473 n_pkts = KPKTQ_LEN(spktq);
3474
3475 FSW_RLOCK(fsw);
3476 if (__improbable(FSW_QUIESCED(fsw))) {
3477 DTRACE_SKYWALK1(tx__quiesced, struct nx_flowswitch *, fsw);
3478 SK_ERR("flowswitch detached, dropping %d pkts", n_pkts);
3479 KPKTQ_CONCAT(&dropped_pkts, spktq);
3480 reason = DROP_REASON_FSW_QUIESCED;
3481 line = __LINE__;
3482 goto done;
3483 }
3484 dev_na = fsw->fsw_dev_ch->ch_na;
3485 if (__improbable(dev_na == NULL)) {
3486 SK_ERR("dev port not attached, dropping %d pkts", n_pkts);
3487 FSW_STATS_ADD(FSW_STATS_DST_NXPORT_INACTIVE, n_pkts);
3488 KPKTQ_CONCAT(&dropped_pkts, spktq);
3489 reason = DROP_REASON_FSW_TX_DEVPORT_NOT_ATTACHED;
3490 line = __LINE__;
3491 goto done;
3492 }
3493 ifp = fsw_datamov_begin(fsw);
3494 if (ifp == NULL) {
3495 SK_ERR("ifnet not attached, dropping %d pkts", n_pkts);
3496 KPKTQ_CONCAT(&dropped_pkts, spktq);
3497 reason = DROP_REASON_FSW_IFNET_NOT_ATTACHED;
3498 line = __LINE__;
3499 goto done;
3500 }
3501
3502 /* batch allocate enough packets */
3503 dev_pp = na_kr_get_pp(dev_na, NR_TX);
3504
3505 err = pp_alloc_pktq(dev_pp, dev_pp->pp_max_frags, &dpktq, n_pkts, NULL,
3506 NULL, SKMEM_NOSLEEP);
3507 #if DEVELOPMENT || DEBUG
3508 if (__probable(err != ENOMEM)) {
3509 _FSW_INJECT_ERROR(12, err, ENOMEM, pp_free_pktq, &dpktq);
3510 }
3511 #endif /* DEVELOPMENT || DEBUG */
3512 if (__improbable(err == ENOMEM)) {
3513 ASSERT(KPKTQ_EMPTY(&dpktq));
3514 KPKTQ_CONCAT(&dropped_pkts, spktq);
3515 FSW_STATS_ADD(FSW_STATS_DROP_NOMEM_PKT, n_pkts);
3516 SK_ERR("failed to alloc %u pkts from device pool", n_pkts);
3517 reason = DROP_REASON_FSW_PP_ALLOC_FAILED;
3518 line = __LINE__;
3519 goto done;
3520 } else if (__improbable(err == EAGAIN)) {
3521 FSW_STATS_ADD(FSW_STATS_DROP_NOMEM_PKT,
3522 (n_pkts - KPKTQ_LEN(&dpktq)));
3523 FSW_STATS_ADD(FSW_STATS_DROP,
3524 (n_pkts - KPKTQ_LEN(&dpktq)));
3525 }
3526
3527 n_pkts = KPKTQ_LEN(&dpktq);
3528 prev_fe = NULL;
3529 KPKTQ_FOREACH(spkt, spktq) {
3530 if (n_pkts == 0) {
3531 break;
3532 }
3533 --n_pkts;
3534
3535 KPKTQ_DEQUEUE(&dpktq, pkt);
3536 ASSERT(pkt != NULL);
3537 err = dp_copy_to_dev(fsw, spkt, pkt);
3538 if (__improbable(err != 0)) {
3539 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_PKT_COPY_FAILED,
3540 DROPTAP_FLAG_L2_MISSING);
3541 continue;
3542 }
3543
3544 do_pacing |= __packet_get_tx_timestamp(SK_PKT2PH(pkt)) != 0;
3545 af = fsw_ip_demux(fsw, pkt);
3546 if (__improbable(af == AF_UNSPEC)) {
3547 dp_tx_log_pkt(SK_VERB_ERROR, "demux err", pkt);
3548 FSW_STATS_INC(FSW_STATS_TX_DEMUX_ERR);
3549 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_DEMUX_FAILED,
3550 DROPTAP_FLAG_L2_MISSING);
3551 continue;
3552 }
3553
3554 err = flow_pkt_classify(pkt, ifp, af, false);
3555 if (__improbable(err != 0)) {
3556 dp_tx_log_pkt(SK_VERB_ERROR, "flow extract err", pkt);
3557 FSW_STATS_INC(FSW_STATS_TX_FLOW_EXTRACT_ERR);
3558 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_TX_FLOW_EXTRACT_FAILED,
3559 DROPTAP_FLAG_L2_MISSING);
3560 continue;
3561 }
3562
3563 if (__improbable(pkt->pkt_flow_ip_is_frag &&
3564 !pkt->pkt_flow_ip_is_first_frag)) {
3565 fe = tx_process_continuous_ip_frag(fsw, prev_fe, pkt);
3566 if (__probable(fe != NULL)) {
3567 flow_entry_retain(fe);
3568 goto flow_batch;
3569 } else {
3570 FSW_STATS_INC(FSW_STATS_TX_FRAG_BAD_CONT);
3571 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_TX_FRAG_BAD_CONT,
3572 DROPTAP_FLAG_L2_MISSING);
3573 continue;
3574 }
3575 }
3576
3577 fe = tx_lookup_flow(fsw, pkt, prev_fe);
3578 if (__improbable(fe == NULL)) {
3579 FSW_STATS_INC(FSW_STATS_TX_FLOW_NOT_FOUND);
3580 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_TX_FLOW_NOT_FOUND,
3581 DROPTAP_FLAG_L2_MISSING);
3582 prev_fe = NULL;
3583 continue;
3584 }
3585 flow_batch:
3586 tx_flow_batch_packet(&fes, fe, pkt);
3587 prev_fe = fe;
3588 }
3589
3590 struct flow_entry *tfe = NULL;
3591 TAILQ_FOREACH_SAFE(fe, &fes, fe_tx_link, tfe) {
3592 tx_flow_process(fsw, fe, 0);
3593 TAILQ_REMOVE(&fes, fe, fe_tx_link);
3594 fe->fe_tx_is_cont_frag = false;
3595 fe->fe_tx_frag_id = 0;
3596 flow_entry_release(&fe);
3597 n_flows++;
3598 }
3599
3600 done:
3601 FSW_RUNLOCK(fsw);
3602 if (n_flows > 0) {
3603 netif_transmit(ifp, NETIF_XMIT_FLAG_CHANNEL | (do_pacing ? NETIF_XMIT_FLAG_PACING : 0));
3604 }
3605 if (ifp != NULL) {
3606 fsw_datamov_end(fsw);
3607 }
3608 dp_drop_pktq(fsw, &dropped_pkts, 1, reason, line, DROPTAP_FLAG_L2_MISSING);
3609 KPKTQ_FINI(&dropped_pkts);
3610 KPKTQ_FINI(&dpktq);
3611 }
3612
3613 static sa_family_t
get_tso_af(struct __kern_packet * pkt)3614 get_tso_af(struct __kern_packet *pkt)
3615 {
3616 packet_tso_flags_t tso_flags;
3617
3618 tso_flags = pkt->pkt_csum_flags & PACKET_CSUM_TSO_FLAGS;
3619 if (tso_flags == PACKET_TSO_IPV4) {
3620 return AF_INET;
3621 } else if (tso_flags == PACKET_TSO_IPV6) {
3622 return AF_INET6;
3623 } else {
3624 panic("invalid tso flags: 0x%x\n", tso_flags);
3625 /* NOTREACHED */
3626 __builtin_unreachable();
3627 }
3628 }
3629
3630 static inline void
update_flow_info(struct __kern_packet * pkt,void * iphdr,void * tcphdr,uint16_t payload_sz)3631 update_flow_info(struct __kern_packet *pkt, void *iphdr, void *tcphdr, uint16_t payload_sz)
3632 {
3633 struct tcphdr *__single tcp = tcphdr;
3634
3635 DTRACE_SKYWALK4(update__flow__info, struct __kern_packet *, pkt,
3636 void *, iphdr, void *, tcphdr, uint16_t, payload_sz);
3637 pkt->pkt_flow_ip_hdr = (mach_vm_address_t)iphdr;
3638 pkt->pkt_flow_tcp_hdr = (mach_vm_address_t)tcphdr;
3639 pkt->pkt_flow_tcp_flags = tcp->th_flags;
3640 pkt->pkt_flow_tcp_seq = tcp->th_seq;
3641 pkt->pkt_flow_ulen = payload_sz;
3642 }
3643
3644 static int
do_gso(struct nx_flowswitch * fsw,int af,struct __kern_packet * orig_pkt,struct __kern_packet * first_pkt,struct pktq * dev_pktq,struct pktq * gso_pktq)3645 do_gso(struct nx_flowswitch *fsw, int af, struct __kern_packet *orig_pkt,
3646 struct __kern_packet *first_pkt, struct pktq *dev_pktq,
3647 struct pktq *gso_pktq)
3648 {
3649 ifnet_t ifp = fsw->fsw_ifp;
3650 struct __kern_packet *pkt = first_pkt;
3651 uint8_t proto = pkt->pkt_flow_ip_proto;
3652 uint16_t ip_hlen = pkt->pkt_flow_ip_hlen;
3653 uint16_t tcp_hlen = pkt->pkt_flow_tcp_hlen;
3654 uint16_t total_hlen = ip_hlen + tcp_hlen;
3655 uint16_t mtu = (uint16_t)ifp->if_mtu;
3656 uint16_t mss = pkt->pkt_proto_seg_sz, payload_sz;
3657 uint32_t n, n_pkts, off = 0, total_len = orig_pkt->pkt_length;
3658 uint16_t headroom = fsw->fsw_frame_headroom + ifp->if_tx_headroom;
3659 kern_packet_t orig_ph = SK_PKT2PH(orig_pkt);
3660 uint8_t *orig_pkt_baddr;
3661 struct tcphdr *tcp;
3662 struct ip *ip;
3663 struct ip6_hdr *ip6;
3664 uint32_t tcp_seq;
3665 uint16_t ipid;
3666 uint32_t pseudo_hdr_csum, bufsz;
3667 uint64_t pkt_tx_timestamp = 0;
3668
3669 ASSERT(headroom <= UINT8_MAX);
3670 if (proto != IPPROTO_TCP) {
3671 SK_ERR("invalid proto: %d", proto);
3672 DTRACE_SKYWALK3(invalid__proto, struct nx_flowswitch *,
3673 fsw, ifnet_t, ifp, uint8_t, proto);
3674 return EINVAL;
3675 }
3676 if (mss == 0 || mss > (mtu - total_hlen)) {
3677 SK_ERR("invalid args: mss %d, mtu %d, total_hlen %d",
3678 mss, mtu, total_hlen);
3679 DTRACE_SKYWALK5(invalid__args1, struct nx_flowswitch *,
3680 fsw, ifnet_t, ifp, uint16_t, mss, uint16_t, mtu,
3681 uint32_t, total_hlen);
3682 return EINVAL;
3683 }
3684 bufsz = PP_BUF_SIZE_DEF(pkt->pkt_qum.qum_pp);
3685 if ((headroom + total_hlen + mss) > bufsz) {
3686 SK_ERR("invalid args: headroom %d, total_hlen %d, "
3687 "mss %d, bufsz %d", headroom, total_hlen, mss, bufsz);
3688 DTRACE_SKYWALK6(invalid__args2, struct nx_flowswitch *,
3689 fsw, ifnet_t, ifp, uint16_t, headroom, uint16_t,
3690 total_hlen, uint16_t, mss, uint32_t, bufsz);
3691 return EINVAL;
3692 }
3693 n_pkts = (uint32_t)(SK_ROUNDUP((total_len - total_hlen), mss) / mss);
3694
3695 ASSERT(pkt->pkt_headroom == headroom);
3696 ASSERT(pkt->pkt_length == total_len);
3697 ASSERT(pkt->pkt_l2_len == 0);
3698 ASSERT((pkt->pkt_qum.qum_qflags & QUM_F_FINALIZED) == 0);
3699 ASSERT((pkt->pkt_pflags & PKT_F_TRUNCATED) != 0);
3700 pkt->pkt_pflags &= ~PKT_F_TRUNCATED;
3701 pkt->pkt_proto_seg_sz = 0;
3702 pkt->pkt_csum_flags = 0;
3703 MD_BUFLET_ADDR_ABS(orig_pkt, orig_pkt_baddr);
3704 orig_pkt_baddr += orig_pkt->pkt_headroom;
3705
3706 if (af == AF_INET) {
3707 /*
3708 * XXX -fbounds-safety: can't avoid using forge unless we change
3709 * the flow metadata definition.
3710 */
3711 ip = __unsafe_forge_bidi_indexable(struct ip *,
3712 pkt->pkt_flow_ip_hdr, pkt->pkt_length);
3713 tcp = __unsafe_forge_bidi_indexable(struct tcphdr *,
3714 pkt->pkt_flow_tcp_hdr, pkt->pkt_length - ip_hlen);
3715 ipid = ip->ip_id;
3716 pseudo_hdr_csum = in_pseudo(pkt->pkt_flow_ipv4_src.s_addr,
3717 pkt->pkt_flow_ipv4_dst.s_addr, 0);
3718 } else {
3719 ASSERT(af == AF_INET6);
3720 tcp = __unsafe_forge_bidi_indexable(struct tcphdr *,
3721 pkt->pkt_flow_tcp_hdr, pkt->pkt_length - ip_hlen);
3722 pseudo_hdr_csum = in6_pseudo(&pkt->pkt_flow_ipv6_src,
3723 &pkt->pkt_flow_ipv6_dst, 0);
3724 }
3725 tcp_seq = ntohl(tcp->th_seq);
3726
3727 pkt_tx_timestamp = __packet_get_tx_timestamp(orig_ph);
3728
3729 for (n = 1, payload_sz = mss, off = total_hlen; off < total_len;
3730 off += payload_sz) {
3731 uint8_t *baddr, *baddr0;
3732 uint32_t partial;
3733
3734 if (pkt == NULL) {
3735 n++;
3736 KPKTQ_DEQUEUE(dev_pktq, pkt);
3737 ASSERT(pkt != NULL);
3738 }
3739 MD_BUFLET_ADDR_ABS(pkt, baddr0);
3740 baddr = baddr0;
3741 baddr += headroom;
3742
3743 /* Copy headers from the original packet */
3744 if (n != 1) {
3745 ASSERT(pkt != first_pkt);
3746 pkt_copy(orig_pkt_baddr, baddr, total_hlen);
3747 fsw_pkt_copy_metadata(first_pkt, pkt);
3748
3749 ASSERT((pkt->pkt_qum_qflags & QUM_F_FLOW_CLASSIFIED) != 0);
3750 /* flow info still needs to be updated below */
3751 bcopy(first_pkt->pkt_flow, pkt->pkt_flow,
3752 sizeof(*pkt->pkt_flow));
3753 pkt->pkt_trace_id = 0;
3754 ASSERT(pkt->pkt_headroom == headroom);
3755 } else {
3756 METADATA_SET_LEN(pkt, 0, 0);
3757 }
3758 baddr += total_hlen;
3759
3760 /* copy tx timestamp from the orignal packet */
3761 __packet_set_tx_timestamp(SK_PKT2PH(pkt), pkt_tx_timestamp);
3762
3763 /* Copy/checksum the payload from the original packet */
3764 if (off + payload_sz > total_len) {
3765 payload_sz = (uint16_t)(total_len - off);
3766 }
3767 pkt_copypkt_sum(orig_ph,
3768 (uint16_t)(orig_pkt->pkt_headroom + off),
3769 SK_PKT2PH(pkt), headroom + total_hlen, payload_sz,
3770 &partial, TRUE);
3771
3772 DTRACE_SKYWALK6(copy__csum, struct nx_flowswitch *, fsw,
3773 ifnet_t, ifp, uint8_t *, baddr, uint16_t, payload_sz,
3774 uint16_t, mss, uint32_t, partial);
3775 FSW_STATS_INC(FSW_STATS_TX_COPY_PKT2PKT);
3776
3777 /*
3778 * Adjust header information and fill in the missing fields.
3779 */
3780 if (af == AF_INET) {
3781 ip = (struct ip *)(void *)(baddr0 + pkt->pkt_headroom);
3782 tcp = (struct tcphdr *)(void *)((caddr_t)ip + ip_hlen);
3783
3784 if (n != n_pkts) {
3785 tcp->th_flags &= ~(TH_FIN | TH_PUSH);
3786 }
3787 if (n != 1) {
3788 tcp->th_flags &= ~TH_CWR;
3789 tcp->th_seq = htonl(tcp_seq);
3790 }
3791 update_flow_info(pkt, ip, tcp, payload_sz);
3792
3793 ip->ip_id = htons((ipid)++);
3794 ip->ip_len = htons(ip_hlen + tcp_hlen + payload_sz);
3795 ip->ip_sum = 0;
3796 ip->ip_sum = inet_cksum_buffer(ip, 0, 0, ip_hlen);
3797 tcp->th_sum = 0;
3798
3799 partial = __packet_cksum(tcp, tcp_hlen, partial);
3800 partial += htons(tcp_hlen + IPPROTO_TCP + payload_sz);
3801 partial += pseudo_hdr_csum;
3802 ADDCARRY(partial);
3803 tcp->th_sum = ~(uint16_t)partial;
3804 } else {
3805 ASSERT(af == AF_INET6);
3806 ip6 = (struct ip6_hdr *)(baddr0 + pkt->pkt_headroom);
3807 tcp = (struct tcphdr *)(void *)((caddr_t)ip6 + ip_hlen);
3808
3809 if (n != n_pkts) {
3810 tcp->th_flags &= ~(TH_FIN | TH_PUSH);
3811 }
3812 if (n != 1) {
3813 tcp->th_flags &= ~TH_CWR;
3814 tcp->th_seq = htonl(tcp_seq);
3815 }
3816 update_flow_info(pkt, ip6, tcp, payload_sz);
3817
3818 ip6->ip6_plen = htons(tcp_hlen + payload_sz);
3819 tcp->th_sum = 0;
3820 partial = __packet_cksum(tcp, tcp_hlen, partial);
3821 partial += htonl(tcp_hlen + IPPROTO_TCP + payload_sz);
3822 partial += pseudo_hdr_csum;
3823 ADDCARRY(partial);
3824 tcp->th_sum = ~(uint16_t)partial;
3825 }
3826 tcp_seq += payload_sz;
3827 METADATA_ADJUST_LEN(pkt, total_hlen, headroom);
3828 #if (DEVELOPMENT || DEBUG)
3829 struct __kern_buflet *bft;
3830 uint32_t blen;
3831 PKT_GET_FIRST_BUFLET(pkt, 1, bft);
3832 blen = __buflet_get_data_length(bft);
3833 if (blen != total_hlen + payload_sz) {
3834 panic("blen (%d) != total_len + payload_sz (%d)\n",
3835 blen, total_hlen + payload_sz);
3836 }
3837 #endif /* DEVELOPMENT || DEBUG */
3838
3839 pkt->pkt_length = total_hlen + payload_sz;
3840 KPKTQ_ENQUEUE(gso_pktq, pkt);
3841 pkt = NULL;
3842
3843 /*
3844 * Note that at this point the packet is not yet finalized.
3845 * The finalization happens in dp_flow_tx_process() after
3846 * the framing is done.
3847 */
3848 }
3849 ASSERT(n == n_pkts);
3850 ASSERT(off == total_len);
3851 DTRACE_SKYWALK7(gso__done, struct nx_flowswitch *, fsw, ifnet_t, ifp,
3852 uint32_t, n_pkts, uint32_t, total_len, uint16_t, ip_hlen,
3853 uint16_t, tcp_hlen, uint8_t *, orig_pkt_baddr);
3854 return 0;
3855 }
3856
3857 static void
tx_flow_enqueue_gso_pktq(struct flow_entry_list * fes,struct flow_entry * fe,struct pktq * gso_pktq)3858 tx_flow_enqueue_gso_pktq(struct flow_entry_list *fes, struct flow_entry *fe,
3859 struct pktq *gso_pktq)
3860 {
3861 if (KPKTQ_EMPTY(&fe->fe_tx_pktq)) {
3862 ASSERT(KPKTQ_LEN(&fe->fe_tx_pktq) == 0);
3863 TAILQ_INSERT_TAIL(fes, fe, fe_tx_link);
3864 KPKTQ_ENQUEUE_MULTI(&fe->fe_tx_pktq, KPKTQ_FIRST(gso_pktq),
3865 KPKTQ_LAST(gso_pktq), KPKTQ_LEN(gso_pktq));
3866 KPKTQ_INIT(gso_pktq);
3867 } else {
3868 ASSERT(!TAILQ_EMPTY(fes));
3869 KPKTQ_ENQUEUE_MULTI(&fe->fe_tx_pktq, KPKTQ_FIRST(gso_pktq),
3870 KPKTQ_LAST(gso_pktq), KPKTQ_LEN(gso_pktq));
3871 KPKTQ_INIT(gso_pktq);
3872 flow_entry_release(&fe);
3873 }
3874 }
3875
3876 static void
dp_gso_pktq(struct nx_flowswitch * fsw,struct pktq * spktq,uint32_t gso_pkts_estimate)3877 dp_gso_pktq(struct nx_flowswitch *fsw, struct pktq *spktq,
3878 uint32_t gso_pkts_estimate)
3879 {
3880 struct __kern_packet *spkt, *pkt;
3881 struct flow_entry_list fes = TAILQ_HEAD_INITIALIZER(fes);
3882 struct flow_entry *__single fe, *__single prev_fe;
3883 struct pktq dpktq;
3884 struct nexus_adapter *dev_na;
3885 struct kern_pbufpool *dev_pp;
3886 struct ifnet *ifp = NULL;
3887 sa_family_t af;
3888 uint32_t n_pkts, n_flows = 0;
3889 int err;
3890
3891 KPKTQ_INIT(&dpktq);
3892 n_pkts = KPKTQ_LEN(spktq);
3893
3894 FSW_RLOCK(fsw);
3895 if (__improbable(FSW_QUIESCED(fsw))) {
3896 DTRACE_SKYWALK1(tx__quiesced, struct nx_flowswitch *, fsw);
3897 SK_ERR("flowswitch detached, dropping %d pkts", n_pkts);
3898 dp_drop_pktq(fsw, spktq, 1, DROP_REASON_FSW_QUIESCED, __LINE__,
3899 DROPTAP_FLAG_L2_MISSING);
3900 goto done;
3901 }
3902 dev_na = fsw->fsw_dev_ch->ch_na;
3903 if (__improbable(dev_na == NULL)) {
3904 SK_ERR("dev port not attached, dropping %d pkts", n_pkts);
3905 FSW_STATS_ADD(FSW_STATS_DST_NXPORT_INACTIVE, n_pkts);
3906 dp_drop_pktq(fsw, spktq, 1, DROP_REASON_FSW_TX_DEVPORT_NOT_ATTACHED,
3907 __LINE__, DROPTAP_FLAG_L2_MISSING);
3908 goto done;
3909 }
3910 ifp = fsw_datamov_begin(fsw);
3911 if (ifp == NULL) {
3912 SK_ERR("ifnet not attached, dropping %d pkts", n_pkts);
3913 dp_drop_pktq(fsw, spktq, 1, DROP_REASON_FSW_IFNET_NOT_ATTACHED,
3914 __LINE__, DROPTAP_FLAG_L2_MISSING);
3915 goto done;
3916 }
3917
3918 dev_pp = na_kr_get_pp(dev_na, NR_TX);
3919
3920 /*
3921 * Batch allocate enough packets to perform GSO on all
3922 * packets in spktq.
3923 */
3924 err = pp_alloc_pktq(dev_pp, dev_pp->pp_max_frags, &dpktq,
3925 gso_pkts_estimate, NULL, NULL, SKMEM_NOSLEEP);
3926 #if DEVELOPMENT || DEBUG
3927 if (__probable(err != ENOMEM)) {
3928 _FSW_INJECT_ERROR(12, err, ENOMEM, pp_free_pktq, &dpktq);
3929 }
3930 #endif /* DEVELOPMENT || DEBUG */
3931 /*
3932 * We either get all packets or none. No partial allocations.
3933 */
3934 if (__improbable(err != 0)) {
3935 if (err == ENOMEM) {
3936 ASSERT(KPKTQ_EMPTY(&dpktq));
3937 } else {
3938 dp_free_pktq(fsw, &dpktq);
3939 }
3940 DTRACE_SKYWALK1(gso__no__mem, int, err);
3941 dp_drop_pktq(fsw, spktq, 1, DROP_REASON_FSW_PP_ALLOC_FAILED,
3942 __LINE__, DROPTAP_FLAG_L2_MISSING);
3943 FSW_STATS_ADD(FSW_STATS_DROP_NOMEM_PKT, n_pkts);
3944 SK_ERR("failed to alloc %u pkts from device pool",
3945 gso_pkts_estimate);
3946 goto done;
3947 }
3948 prev_fe = NULL;
3949 KPKTQ_FOREACH(spkt, spktq) {
3950 KPKTQ_DEQUEUE(&dpktq, pkt);
3951 ASSERT(pkt != NULL);
3952 /*
3953 * Copy only headers to the first packet of the GSO chain.
3954 * The headers will be used for classification below.
3955 */
3956 err = dp_copy_headers_to_dev(fsw, spkt, pkt);
3957 if (__improbable(err != 0)) {
3958 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_PKT_COPY_FAILED,
3959 DROPTAP_FLAG_L2_MISSING);
3960 DTRACE_SKYWALK2(copy__headers__failed,
3961 struct nx_flowswitch *, fsw,
3962 struct __kern_packet *, spkt);
3963 continue;
3964 }
3965 af = get_tso_af(pkt);
3966 ASSERT(af == AF_INET || af == AF_INET6);
3967
3968 err = flow_pkt_classify(pkt, ifp, af, false);
3969 if (__improbable(err != 0)) {
3970 dp_tx_log_pkt(SK_VERB_ERROR, "flow extract err", pkt);
3971 FSW_STATS_INC(FSW_STATS_TX_FLOW_EXTRACT_ERR);
3972 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_TX_FLOW_EXTRACT_FAILED,
3973 DROPTAP_FLAG_L2_MISSING);
3974 DTRACE_SKYWALK4(classify__failed,
3975 struct nx_flowswitch *, fsw,
3976 struct __kern_packet *, spkt,
3977 struct __kern_packet *, pkt,
3978 int, err);
3979 continue;
3980 }
3981 /*
3982 * GSO cannot be done on a fragment and it's a bug in user
3983 * space to mark a fragment as needing GSO.
3984 */
3985 if (__improbable(pkt->pkt_flow_ip_is_frag)) {
3986 FSW_STATS_INC(FSW_STATS_TX_FRAG_BAD_CONT);
3987 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_TX_FRAG_BAD_CONT,
3988 DROPTAP_FLAG_L2_MISSING);
3989 DTRACE_SKYWALK3(is__frag,
3990 struct nx_flowswitch *, fsw,
3991 struct __kern_packet *, spkt,
3992 struct __kern_packet *, pkt);
3993 continue;
3994 }
3995 fe = tx_lookup_flow(fsw, pkt, prev_fe);
3996 if (__improbable(fe == NULL)) {
3997 FSW_STATS_INC(FSW_STATS_TX_FLOW_NOT_FOUND);
3998 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_TX_FRAG_BAD_CONT,
3999 DROPTAP_FLAG_L2_MISSING);
4000 DTRACE_SKYWALK3(lookup__failed,
4001 struct nx_flowswitch *, fsw,
4002 struct __kern_packet *, spkt,
4003 struct __kern_packet *, pkt);
4004 prev_fe = NULL;
4005 continue;
4006 }
4007 /*
4008 * Perform GSO on spkt using the flow information
4009 * obtained above.
4010 */
4011 struct pktq gso_pktq;
4012 KPKTQ_INIT(&gso_pktq);
4013 err = do_gso(fsw, af, spkt, pkt, &dpktq, &gso_pktq);
4014 if (__probable(err == 0)) {
4015 tx_flow_enqueue_gso_pktq(&fes, fe, &gso_pktq);
4016 prev_fe = fe;
4017 } else {
4018 DTRACE_SKYWALK1(gso__error, int, err);
4019 /* TODO: increment error stat */
4020 dp_drop_pkt_single(fsw, pkt, 1, DROP_REASON_FSW_GSO_FAILED,
4021 DROPTAP_FLAG_L2_MISSING);
4022 flow_entry_release(&fe);
4023 prev_fe = NULL;
4024 }
4025 KPKTQ_FINI(&gso_pktq);
4026 }
4027 struct flow_entry *tfe = NULL;
4028 TAILQ_FOREACH_SAFE(fe, &fes, fe_tx_link, tfe) {
4029 /* Chain-enqueue can be used for GSO chains */
4030 tx_flow_process(fsw, fe, FLOW_PROC_FLAG_GSO);
4031 TAILQ_REMOVE(&fes, fe, fe_tx_link);
4032 flow_entry_release(&fe);
4033 n_flows++;
4034 }
4035 done:
4036 FSW_RUNLOCK(fsw);
4037 if (n_flows > 0) {
4038 netif_transmit(ifp, NETIF_XMIT_FLAG_CHANNEL);
4039 }
4040 if (ifp != NULL) {
4041 fsw_datamov_end(fsw);
4042 }
4043
4044 /*
4045 * It's possible for packets to be left in dpktq because
4046 * gso_pkts_estimate is only an estimate. The actual number
4047 * of packets needed could be less.
4048 */
4049 uint32_t dpktq_len;
4050 if ((dpktq_len = KPKTQ_LEN(&dpktq)) > 0) {
4051 DTRACE_SKYWALK2(leftover__dev__pkts,
4052 struct nx_flowswitch *, fsw, uint32_t, dpktq_len);
4053 dp_free_pktq(fsw, &dpktq);
4054 }
4055 KPKTQ_FINI(&dpktq);
4056 }
4057
4058 static inline void
fsw_dev_ring_flush(struct nx_flowswitch * fsw,struct __kern_channel_ring * r,struct proc * p)4059 fsw_dev_ring_flush(struct nx_flowswitch *fsw, struct __kern_channel_ring *r,
4060 struct proc *p)
4061 {
4062 #pragma unused(p)
4063 uint32_t total_pkts = 0, total_bytes = 0;
4064
4065 for (;;) {
4066 struct pktq pktq;
4067 KPKTQ_INIT(&pktq);
4068 uint32_t n_bytes;
4069 fsw_rx_ring_dequeue_pktq(fsw, r, fsw_rx_batch, &pktq, &n_bytes);
4070 if (n_bytes == 0) {
4071 break;
4072 }
4073 total_pkts += KPKTQ_LEN(&pktq);
4074 total_bytes += n_bytes;
4075
4076 if (__probable(fsw->fsw_ifp->if_input_netem == NULL)) {
4077 fsw_receive(fsw, &pktq);
4078 } else {
4079 fsw_dev_input_netem_enqueue(fsw, &pktq);
4080 }
4081 KPKTQ_FINI(&pktq);
4082 }
4083
4084 KDBG(SK_KTRACE_FSW_DEV_RING_FLUSH, SK_KVA(r), total_pkts, total_bytes);
4085 DTRACE_SKYWALK2(fsw__dp__dev__ring__flush, uint32_t, total_pkts,
4086 uint32_t, total_bytes);
4087
4088 /* compute mitigation rate for delivered traffic */
4089 if (__probable(r->ckr_netif_mit_stats != NULL)) {
4090 r->ckr_netif_mit_stats(r, total_pkts, total_bytes);
4091 }
4092 }
4093
4094 static inline void
fsw_user_ring_flush(struct nx_flowswitch * fsw,struct __kern_channel_ring * r,struct proc * p)4095 fsw_user_ring_flush(struct nx_flowswitch *fsw, struct __kern_channel_ring *r,
4096 struct proc *p)
4097 {
4098 #pragma unused(p)
4099 static packet_trace_id_t trace_id = 0;
4100 uint32_t total_pkts = 0, total_bytes = 0;
4101
4102 for (;;) {
4103 struct pktq pktq;
4104 KPKTQ_INIT(&pktq);
4105 uint32_t n_bytes;
4106 uint32_t gso_pkts_estimate = 0;
4107
4108 fsw_tx_ring_dequeue_pktq(fsw, r, fsw_tx_batch, &pktq, &n_bytes,
4109 &gso_pkts_estimate);
4110 if (n_bytes == 0) {
4111 break;
4112 }
4113 total_pkts += KPKTQ_LEN(&pktq);
4114 total_bytes += n_bytes;
4115
4116 KPKTQ_FIRST(&pktq)->pkt_trace_id = ++trace_id;
4117 KDBG(SK_KTRACE_PKT_TX_FSW | DBG_FUNC_START,
4118 KPKTQ_FIRST(&pktq)->pkt_trace_id);
4119
4120 if (gso_pkts_estimate > 0) {
4121 dp_gso_pktq(fsw, &pktq, gso_pkts_estimate);
4122 } else {
4123 dp_tx_pktq(fsw, &pktq);
4124 }
4125 dp_free_pktq(fsw, &pktq);
4126 KPKTQ_FINI(&pktq);
4127 }
4128 kr_update_stats(r, total_pkts, total_bytes);
4129
4130 KDBG(SK_KTRACE_FSW_USER_RING_FLUSH, SK_KVA(r), total_pkts, total_bytes);
4131 DTRACE_SKYWALK2(fsw__dp__user__ring__flush, uint32_t, total_pkts,
4132 uint32_t, total_bytes);
4133 }
4134
4135 void
fsw_ring_flush(struct nx_flowswitch * fsw,struct __kern_channel_ring * r,struct proc * p)4136 fsw_ring_flush(struct nx_flowswitch *fsw, struct __kern_channel_ring *r,
4137 struct proc *p)
4138 {
4139 struct nexus_vp_adapter *vpna = VPNA(KRNA(r));
4140
4141 ASSERT(sk_is_sync_protected());
4142 ASSERT(vpna->vpna_nx_port != FSW_VP_HOST);
4143 ASSERT(vpna->vpna_up.na_md_type == NEXUS_META_TYPE_PACKET);
4144
4145 if (vpna->vpna_nx_port == FSW_VP_DEV) {
4146 fsw_dev_ring_flush(fsw, r, p);
4147 } else {
4148 fsw_user_ring_flush(fsw, r, p);
4149 }
4150 }
4151
4152 int
fsw_dp_ctor(struct nx_flowswitch * fsw)4153 fsw_dp_ctor(struct nx_flowswitch *fsw)
4154 {
4155 uint32_t fe_cnt = fsw_fe_table_size;
4156 uint32_t fob_cnt = fsw_flow_owner_buckets;
4157 uint32_t frb_cnt = fsw_flow_route_buckets;
4158 uint32_t frib_cnt = fsw_flow_route_id_buckets;
4159 struct kern_nexus *nx = fsw->fsw_nx;
4160 char name[64];
4161 const char *__null_terminated fsw_name = NULL;
4162 int error = 0;
4163
4164 /* just in case */
4165 if (fe_cnt == 0) {
4166 fe_cnt = NX_FSW_FE_TABLESZ;
4167 ASSERT(fe_cnt != 0);
4168 }
4169 if (fob_cnt == 0) {
4170 fob_cnt = NX_FSW_FOB_HASHSZ;
4171 ASSERT(fob_cnt != 0);
4172 }
4173 if (frb_cnt == 0) {
4174 frb_cnt = NX_FSW_FRB_HASHSZ;
4175 ASSERT(frb_cnt != 0);
4176 }
4177 if (frib_cnt == 0) {
4178 frib_cnt = NX_FSW_FRIB_HASHSZ;
4179 ASSERT(frib_cnt != 0);
4180 }
4181
4182 /* make sure fe_cnt is a power of two, else round up */
4183 if ((fe_cnt & (fe_cnt - 1)) != 0) {
4184 fe_cnt--;
4185 fe_cnt |= (fe_cnt >> 1);
4186 fe_cnt |= (fe_cnt >> 2);
4187 fe_cnt |= (fe_cnt >> 4);
4188 fe_cnt |= (fe_cnt >> 8);
4189 fe_cnt |= (fe_cnt >> 16);
4190 fe_cnt++;
4191 }
4192
4193 /* make sure frb_cnt is a power of two, else round up */
4194 if ((frb_cnt & (frb_cnt - 1)) != 0) {
4195 frb_cnt--;
4196 frb_cnt |= (frb_cnt >> 1);
4197 frb_cnt |= (frb_cnt >> 2);
4198 frb_cnt |= (frb_cnt >> 4);
4199 frb_cnt |= (frb_cnt >> 8);
4200 frb_cnt |= (frb_cnt >> 16);
4201 frb_cnt++;
4202 }
4203
4204 lck_mtx_init(&fsw->fsw_detach_barrier_lock, &nexus_lock_group,
4205 &nexus_lock_attr);
4206 lck_mtx_init(&fsw->fsw_reap_lock, &nexus_lock_group, &nexus_lock_attr);
4207 lck_mtx_init(&fsw->fsw_linger_lock, &nexus_lock_group, &nexus_lock_attr);
4208 TAILQ_INIT(&fsw->fsw_linger_head);
4209
4210 fsw_name = tsnprintf(name, sizeof(name), "%s_%llu", NX_FSW_NAME, nx->nx_id);
4211 error = nx_advisory_alloc(nx, fsw_name,
4212 &NX_PROV(nx)->nxprov_region_params[SKMEM_REGION_NEXUSADV],
4213 NEXUS_ADVISORY_TYPE_FLOWSWITCH);
4214 if (error != 0) {
4215 fsw_dp_dtor(fsw);
4216 return error;
4217 }
4218
4219 fsw->fsw_flow_mgr = flow_mgr_create(fe_cnt, fob_cnt, frb_cnt, frib_cnt);
4220 if (fsw->fsw_flow_mgr == NULL) {
4221 fsw_dp_dtor(fsw);
4222 return error;
4223 }
4224
4225 /* generic name; will be customized upon ifattach */
4226 (void) snprintf(fsw->fsw_reap_name, sizeof(fsw->fsw_reap_name),
4227 FSW_REAP_THREADNAME, name, "");
4228
4229 if (kernel_thread_start(fsw_reap_thread_func, fsw,
4230 &fsw->fsw_reap_thread) != KERN_SUCCESS) {
4231 panic_plain("%s: can't create thread", __func__);
4232 /* NOTREACHED */
4233 __builtin_unreachable();
4234 }
4235 /* this must not fail */
4236 VERIFY(fsw->fsw_reap_thread != NULL);
4237
4238 SK_DF(SK_VERB_MEM, "fsw 0x%llx ALLOC", SK_KVA(fsw));
4239
4240
4241 return error;
4242 }
4243
4244 void
fsw_dp_dtor(struct nx_flowswitch * fsw)4245 fsw_dp_dtor(struct nx_flowswitch *fsw)
4246 {
4247 uint64_t f = (1 * NSEC_PER_MSEC); /* 1 ms */
4248 uint64_t s = (1000 * NSEC_PER_SEC); /* 1 sec */
4249 uint32_t i = 0;
4250
4251 #if (DEVELOPMENT || DEBUG)
4252 if (fsw->fsw_rps_threads != NULL) {
4253 for (i = 0; i < fsw->fsw_rps_nthreads; i++) {
4254 fsw_rps_thread_join(fsw, i);
4255 }
4256 kfree_type_counted_by(struct fsw_rps_thread, fsw->fsw_rps_nthreads,
4257 fsw->fsw_rps_threads);
4258 }
4259 #endif /* !DEVELOPMENT && !DEBUG */
4260
4261 nx_advisory_free(fsw->fsw_nx);
4262
4263 if (fsw->fsw_reap_thread != THREAD_NULL) {
4264 /* signal thread to begin self-termination */
4265 lck_mtx_lock(&fsw->fsw_reap_lock);
4266 fsw->fsw_reap_flags |= FSW_REAPF_TERMINATING;
4267
4268 /*
4269 * And wait for thread to terminate; use another
4270 * wait channel here other than fsw_reap_flags to
4271 * make it more explicit. In the event the reaper
4272 * thread misses a wakeup, we'll try again once
4273 * every second (except for the first time).
4274 */
4275 while (!(fsw->fsw_reap_flags & FSW_REAPF_TERMINATED)) {
4276 uint64_t t = 0;
4277
4278 nanoseconds_to_absolutetime((i++ == 0) ? f : s, &t);
4279 clock_absolutetime_interval_to_deadline(t, &t);
4280 ASSERT(t != 0);
4281
4282 fsw->fsw_reap_flags |= FSW_REAPF_TERMINATEBLOCK;
4283 if (!(fsw->fsw_reap_flags & FSW_REAPF_RUNNING)) {
4284 thread_wakeup((caddr_t)&fsw->fsw_reap_flags);
4285 }
4286 (void) assert_wait_deadline(&fsw->fsw_reap_thread,
4287 THREAD_UNINT, t);
4288 lck_mtx_unlock(&fsw->fsw_reap_lock);
4289 thread_block(THREAD_CONTINUE_NULL);
4290 lck_mtx_lock(&fsw->fsw_reap_lock);
4291 fsw->fsw_reap_flags &= ~FSW_REAPF_TERMINATEBLOCK;
4292 }
4293 ASSERT(fsw->fsw_reap_flags & FSW_REAPF_TERMINATED);
4294 lck_mtx_unlock(&fsw->fsw_reap_lock);
4295 fsw->fsw_reap_thread = THREAD_NULL;
4296 }
4297
4298 /* free any remaining flow entries in the linger list */
4299 fsw_linger_purge(fsw);
4300
4301 if (fsw->fsw_flow_mgr != NULL) {
4302 flow_mgr_destroy(fsw->fsw_flow_mgr);
4303 fsw->fsw_flow_mgr = NULL;
4304 }
4305
4306
4307 lck_mtx_destroy(&fsw->fsw_detach_barrier_lock, &nexus_lock_group);
4308 lck_mtx_destroy(&fsw->fsw_reap_lock, &nexus_lock_group);
4309 lck_mtx_destroy(&fsw->fsw_linger_lock, &nexus_lock_group);
4310 }
4311
4312 void
fsw_linger_insert(struct flow_entry * fe)4313 fsw_linger_insert(struct flow_entry *fe)
4314 {
4315 struct nx_flowswitch *fsw = fe->fe_fsw;
4316 SK_LOG_VAR(char dbgbuf[FLOWENTRY_DBGBUF_SIZE]);
4317 SK_DF(SK_VERB_FLOW, "entry \"%s\" fe 0x%llx flags 0x%b",
4318 fe_as_string(fe, dbgbuf, sizeof(dbgbuf)), SK_KVA(fe),
4319 fe->fe_flags, FLOWENTF_BITS);
4320
4321 net_update_uptime();
4322
4323 ASSERT(flow_entry_refcnt(fe) >= 1);
4324 ASSERT(fe->fe_flags & FLOWENTF_TORN_DOWN);
4325 ASSERT(fe->fe_flags & FLOWENTF_DESTROYED);
4326 ASSERT(!(fe->fe_flags & FLOWENTF_LINGERING));
4327 ASSERT(fe->fe_flags & FLOWENTF_WAIT_CLOSE);
4328 ASSERT(fe->fe_linger_wait != 0);
4329 fe->fe_linger_expire = (_net_uptime + fe->fe_linger_wait);
4330 os_atomic_or(&fe->fe_flags, FLOWENTF_LINGERING, relaxed);
4331
4332 lck_mtx_lock_spin(&fsw->fsw_linger_lock);
4333 TAILQ_INSERT_TAIL(&fsw->fsw_linger_head, fe, fe_linger_link);
4334 fsw->fsw_linger_cnt++;
4335 VERIFY(fsw->fsw_linger_cnt != 0);
4336 lck_mtx_unlock(&fsw->fsw_linger_lock);
4337
4338 fsw_reap_sched(fsw);
4339 }
4340
4341 static void
fsw_linger_remove_internal(struct flow_entry_linger_head * linger_head,struct flow_entry * fe)4342 fsw_linger_remove_internal(struct flow_entry_linger_head *linger_head,
4343 struct flow_entry *fe)
4344 {
4345 SK_LOG_VAR(char dbgbuf[FLOWENTRY_DBGBUF_SIZE]);
4346 SK_DF(SK_VERB_FLOW, "entry \"%s\" fe 0x%llx flags 0x%b",
4347 fe_as_string(fe, dbgbuf, sizeof(dbgbuf)), SK_KVA(fe),
4348 fe->fe_flags, FLOWENTF_BITS);
4349
4350 ASSERT(fe->fe_flags & FLOWENTF_TORN_DOWN);
4351 ASSERT(fe->fe_flags & FLOWENTF_DESTROYED);
4352 ASSERT(fe->fe_flags & FLOWENTF_LINGERING);
4353 os_atomic_andnot(&fe->fe_flags, FLOWENTF_LINGERING, relaxed);
4354
4355 TAILQ_REMOVE(linger_head, fe, fe_linger_link);
4356 flow_entry_release(&fe);
4357 }
4358
4359 static void
fsw_linger_remove(struct flow_entry * fe)4360 fsw_linger_remove(struct flow_entry *fe)
4361 {
4362 struct nx_flowswitch *fsw = fe->fe_fsw;
4363
4364 LCK_MTX_ASSERT(&fsw->fsw_linger_lock, LCK_MTX_ASSERT_OWNED);
4365
4366 fsw_linger_remove_internal(&fsw->fsw_linger_head, fe);
4367 VERIFY(fsw->fsw_linger_cnt != 0);
4368 fsw->fsw_linger_cnt--;
4369 }
4370
4371 void
fsw_linger_purge(struct nx_flowswitch * fsw)4372 fsw_linger_purge(struct nx_flowswitch *fsw)
4373 {
4374 struct flow_entry *fe, *tfe;
4375
4376 lck_mtx_lock(&fsw->fsw_linger_lock);
4377 TAILQ_FOREACH_SAFE(fe, &fsw->fsw_linger_head, fe_linger_link, tfe) {
4378 fsw_linger_remove(fe);
4379 }
4380 ASSERT(fsw->fsw_linger_cnt == 0);
4381 ASSERT(TAILQ_EMPTY(&fsw->fsw_linger_head));
4382 lck_mtx_unlock(&fsw->fsw_linger_lock);
4383 }
4384
4385 static void
fsw_defunct_rx_stall_channel(struct nx_flowswitch * fsw)4386 fsw_defunct_rx_stall_channel(struct nx_flowswitch *fsw)
4387 {
4388 struct kern_nexus *nx;
4389 uint64_t now = _net_uptime;
4390
4391 nx = fsw->fsw_nx;
4392
4393 /* Walk through all channels and check for Rx stall condition */
4394 /* uncrustify doesn't handle C blocks properly */
4395 /* BEGIN IGNORE CODESTYLE */
4396 nx_port_foreach(nx, ^(nexus_port_t nxport) {
4397 struct nexus_adapter *na = nx_port_get_na(nx, nxport);
4398 uint64_t elapsed, enqueue_ts, dequeue_ts;
4399 struct __kern_channel_ring *ring;
4400 struct kern_channel *ch;
4401 struct proc *p;
4402
4403 if (na == NULL || na->na_work_ts == 0 || na->na_rx_rings == NULL) {
4404 return;
4405 }
4406 ch = (struct kern_channel *)na->na_private;
4407 if (ch == NULL) {
4408 return;
4409 }
4410 ring = KR_SINGLE(na->na_rx_rings);
4411 enqueue_ts = ring->ckr_rx_enqueue_ts;
4412 dequeue_ts = ring->ckr_rx_dequeue_ts;
4413 /* Elapsed time since last Rx enqueue */
4414 elapsed = now - enqueue_ts;
4415 if ((dequeue_ts < enqueue_ts) && (elapsed > fsw_rx_stall_thresh)) {
4416 p = proc_find(ch->ch_pid);
4417 if (p == NULL) {
4418 return;
4419 }
4420 if (fsw_rx_stall_defunct) {
4421 kern_channel_defunct(p, ch);
4422 }
4423 proc_rele(p);
4424 DTRACE_SKYWALK3(rx__stall, struct nx_flowswitch *, fsw,
4425 struct nexus_adapter *, na, struct __kern_channel_ring *, ring);
4426 FSW_STATS_INC(FSW_STATS_RX_STALL);
4427 SK_ERR("Rx stall detected in proc %s(%llu) (%s): "
4428 "elapsed %llu (s), now: %llu, enqueue: %llu, dequeue: %llu, "
4429 "defunct: %s",
4430 ch->ch_name, ch->ch_pid, fsw->fsw_ifp->if_xname,
4431 elapsed, now, enqueue_ts, dequeue_ts,
4432 fsw_rx_stall_defunct ? "yes" : "no");
4433 }
4434 });
4435 /* END IGNORE CODESTYLE */
4436 }
4437
4438 void
fsw_reap_sched(struct nx_flowswitch * fsw)4439 fsw_reap_sched(struct nx_flowswitch *fsw)
4440 {
4441 ASSERT(fsw->fsw_reap_thread != THREAD_NULL);
4442 lck_mtx_lock_spin(&fsw->fsw_reap_lock);
4443 if (!(fsw->fsw_reap_flags & FSW_REAPF_RUNNING) &&
4444 !(fsw->fsw_reap_flags & (FSW_REAPF_TERMINATING | FSW_REAPF_TERMINATED))) {
4445 thread_wakeup((caddr_t)&fsw->fsw_reap_flags);
4446 }
4447 lck_mtx_unlock(&fsw->fsw_reap_lock);
4448 }
4449
4450 __attribute__((noreturn))
4451 static void
fsw_reap_thread_func(void * v,wait_result_t w)4452 fsw_reap_thread_func(void *v, wait_result_t w)
4453 {
4454 #pragma unused(w)
4455 struct nx_flowswitch *__single fsw = v;
4456
4457 ASSERT(fsw->fsw_reap_thread == current_thread());
4458 /*
4459 * -fbounds-safety: __unsafe_null_terminated_from_indexable provides
4460 * checks to ensure source contains the null terminator, by doing a
4461 * linear scan of the string.
4462 */
4463 thread_set_thread_name(current_thread(),
4464 __unsafe_null_terminated_from_indexable(fsw->fsw_reap_name));
4465
4466 net_update_uptime();
4467
4468 lck_mtx_lock(&fsw->fsw_reap_lock);
4469 VERIFY(!(fsw->fsw_reap_flags & FSW_REAPF_RUNNING));
4470 (void) assert_wait(&fsw->fsw_reap_flags, THREAD_UNINT);
4471 lck_mtx_unlock(&fsw->fsw_reap_lock);
4472 thread_block_parameter(fsw_reap_thread_cont, fsw);
4473 /* NOTREACHED */
4474 __builtin_unreachable();
4475 }
4476
4477 __attribute__((noreturn))
4478 static void
fsw_reap_thread_cont(void * v,wait_result_t wres)4479 fsw_reap_thread_cont(void *v, wait_result_t wres)
4480 {
4481 struct nx_flowswitch *__single fsw = v;
4482 boolean_t low;
4483 uint64_t t = 0;
4484
4485 SK_DF(SK_VERB_FLOW, "%s: running", fsw->fsw_reap_name);
4486
4487 lck_mtx_lock(&fsw->fsw_reap_lock);
4488 if (__improbable(wres == THREAD_INTERRUPTED ||
4489 (fsw->fsw_reap_flags & FSW_REAPF_TERMINATING) != 0)) {
4490 goto terminate;
4491 }
4492
4493 ASSERT(!(fsw->fsw_reap_flags & FSW_REAPF_TERMINATED));
4494 fsw->fsw_reap_flags |= FSW_REAPF_RUNNING;
4495 lck_mtx_unlock(&fsw->fsw_reap_lock);
4496
4497 net_update_uptime();
4498
4499 /* prevent detach from happening while we're here */
4500 if (!fsw_detach_barrier_add(fsw)) {
4501 SK_ERR("%s: netagent detached", fsw->fsw_reap_name);
4502 t = 0;
4503 } else {
4504 uint32_t fe_nonviable, fe_freed, fe_aborted;
4505 uint32_t fr_freed, fr_resid = 0;
4506 struct ifnet *ifp = fsw->fsw_ifp;
4507 uint64_t i = FSW_REAP_IVAL;
4508 uint64_t now = _net_uptime;
4509 uint64_t last;
4510
4511 ASSERT(fsw->fsw_ifp != NULL);
4512
4513 /*
4514 * Pass 1: process any deferred {withdrawn,nonviable} requests.
4515 */
4516 fe_nonviable = fsw_process_deferred(fsw);
4517
4518 /*
4519 * Pass 2: remove any expired lingering flows.
4520 */
4521 fe_freed = fsw_process_linger(fsw, &fe_aborted);
4522
4523 /*
4524 * Pass 3: prune idle flow routes.
4525 */
4526 fr_freed = flow_route_prune(fsw->fsw_flow_mgr,
4527 ifp, &fr_resid);
4528
4529 /*
4530 * Pass 4: prune flow table
4531 *
4532 */
4533 cuckoo_hashtable_try_shrink(fsw->fsw_flow_mgr->fm_flow_table);
4534
4535 SK_DF(SK_VERB_FLOW, "%s: fe_nonviable %u/%u fe_freed %u/%u "
4536 "fe_aborted %u fr_freed %u/%u",
4537 fsw->fsw_flow_mgr->fm_name, fe_nonviable,
4538 (fe_nonviable + fsw->fsw_pending_nonviable),
4539 fe_freed, fsw->fsw_linger_cnt, fe_aborted, fe_freed,
4540 (fe_freed + fr_resid));
4541
4542 /* see if VM memory level is critical */
4543 low = skmem_lowmem_check();
4544
4545 /*
4546 * If things appear to be idle, we can prune away cached
4547 * object that have fallen out of the working sets (this
4548 * is different than purging). Every once in a while, we
4549 * also purge the caches. Note that this is done across
4550 * all flowswitch instances, and so we limit this to no
4551 * more than once every FSW_REAP_SK_THRES seconds.
4552 */
4553 last = os_atomic_load(&fsw_reap_last, relaxed);
4554 if ((low || (last != 0 && (now - last) >= FSW_REAP_SK_THRES)) &&
4555 os_atomic_cmpxchg(&fsw_reap_last, last, now, acq_rel)) {
4556 fsw_purge_cache(fsw, low);
4557
4558 /* increase sleep interval if idle */
4559 if (kdebug_enable == 0 && fsw->fsw_linger_cnt == 0 &&
4560 fsw->fsw_pending_nonviable == 0 && fr_resid == 0) {
4561 i <<= 3;
4562 }
4563 } else if (last == 0) {
4564 os_atomic_store(&fsw_reap_last, now, release);
4565 }
4566
4567 /*
4568 * Additionally, run thru the list of channels and prune
4569 * or purge away cached objects on "idle" channels. This
4570 * check is rate limited to no more than once every
4571 * FSW_DRAIN_CH_THRES seconds.
4572 */
4573 last = fsw->fsw_drain_channel_chk_last;
4574 if (low || (last != 0 && (now - last) >= FSW_DRAIN_CH_THRES)) {
4575 SK_DF(SK_VERB_FLOW, "%s: pruning channels",
4576 fsw->fsw_flow_mgr->fm_name);
4577
4578 fsw->fsw_drain_channel_chk_last = now;
4579 fsw_drain_channels(fsw, now, low);
4580 } else if (__improbable(last == 0)) {
4581 fsw->fsw_drain_channel_chk_last = now;
4582 }
4583
4584 /*
4585 * Finally, invoke the interface's reap callback to
4586 * tell it to prune or purge away cached objects if
4587 * it is idle. This check is rate limited to no more
4588 * than once every FSW_REAP_IF_THRES seconds.
4589 */
4590 last = fsw->fsw_drain_netif_chk_last;
4591 if (low || (last != 0 && (now - last) >= FSW_REAP_IF_THRES)) {
4592 ASSERT(fsw->fsw_nifna != NULL);
4593
4594 if (ifp->if_na_ops != NULL &&
4595 ifp->if_na_ops->ni_reap != NULL) {
4596 SK_DF(SK_VERB_FLOW, "%s: pruning netif",
4597 fsw->fsw_flow_mgr->fm_name);
4598 ifp->if_na_ops->ni_reap(ifp->if_na, ifp,
4599 FSW_REAP_IF_THRES, low);
4600 }
4601
4602 fsw->fsw_drain_netif_chk_last = now;
4603 } else if (__improbable(last == 0)) {
4604 fsw->fsw_drain_netif_chk_last = now;
4605 }
4606
4607 /* emit periodic interface stats ktrace */
4608 last = fsw->fsw_reap_last;
4609 if (last != 0 && (now - last) >= FSW_IFSTATS_THRES) {
4610 KDBG(SK_KTRACE_AON_IF_STATS, ifp->if_data.ifi_ipackets,
4611 ifp->if_data.ifi_ibytes * 8,
4612 ifp->if_data.ifi_opackets,
4613 ifp->if_data.ifi_obytes * 8);
4614
4615 fsw->fsw_reap_last = now;
4616 } else if (__improbable(last == 0)) {
4617 fsw->fsw_reap_last = now;
4618 }
4619
4620 /* Check for Rx stall condition every NX_FSW_RX_STALL_THRES seconds */
4621 last = fsw->fsw_rx_stall_chk_last;
4622 if (last != 0 && (now - last) >= NX_FSW_RX_STALL_THRES) {
4623 fsw_defunct_rx_stall_channel(fsw);
4624 fsw->fsw_rx_stall_chk_last = now;
4625 } else if (__improbable(last == 0)) {
4626 fsw->fsw_rx_stall_chk_last = now;
4627 }
4628
4629 nanoseconds_to_absolutetime(i * NSEC_PER_SEC, &t);
4630 clock_absolutetime_interval_to_deadline(t, &t);
4631 ASSERT(t != 0);
4632
4633 /* allow any pending detach to proceed */
4634 fsw_detach_barrier_remove(fsw);
4635 }
4636
4637 lck_mtx_lock(&fsw->fsw_reap_lock);
4638 if (!(fsw->fsw_reap_flags & FSW_REAPF_TERMINATING)) {
4639 fsw->fsw_reap_flags &= ~FSW_REAPF_RUNNING;
4640 (void) assert_wait_deadline(&fsw->fsw_reap_flags,
4641 THREAD_UNINT, t);
4642 lck_mtx_unlock(&fsw->fsw_reap_lock);
4643 thread_block_parameter(fsw_reap_thread_cont, fsw);
4644 /* NOTREACHED */
4645 __builtin_unreachable();
4646 } else {
4647 terminate:
4648 LCK_MTX_ASSERT(&fsw->fsw_reap_lock, LCK_MTX_ASSERT_OWNED);
4649 fsw->fsw_reap_flags &= ~(FSW_REAPF_RUNNING | FSW_REAPF_TERMINATING);
4650 fsw->fsw_reap_flags |= FSW_REAPF_TERMINATED;
4651 /*
4652 * And signal any thread waiting for us to terminate;
4653 * wait channel here other than fsw_reap_flags to make
4654 * it more explicit.
4655 */
4656 if (fsw->fsw_reap_flags & FSW_REAPF_TERMINATEBLOCK) {
4657 thread_wakeup((caddr_t)&fsw->fsw_reap_thread);
4658 }
4659 lck_mtx_unlock(&fsw->fsw_reap_lock);
4660
4661 SK_DF(SK_VERB_FLOW, "%s: terminating", fsw->fsw_reap_name);
4662
4663 /* for the extra refcnt from kernel_thread_start() */
4664 thread_deallocate(current_thread());
4665 /* this is the end */
4666 thread_terminate(current_thread());
4667 /* NOTREACHED */
4668 __builtin_unreachable();
4669 }
4670
4671 /* must never get here */
4672 VERIFY(0);
4673 /* NOTREACHED */
4674 __builtin_unreachable();
4675 }
4676
4677 static void
fsw_drain_channels(struct nx_flowswitch * fsw,uint64_t now,boolean_t low)4678 fsw_drain_channels(struct nx_flowswitch *fsw, uint64_t now, boolean_t low)
4679 {
4680 struct kern_nexus *nx = fsw->fsw_nx;
4681
4682 /* flowswitch protects NA via fsw_lock, see fsw_port_alloc/free */
4683 FSW_RLOCK(fsw);
4684
4685 /* uncrustify doesn't handle C blocks properly */
4686 /* BEGIN IGNORE CODESTYLE */
4687 nx_port_foreach(nx, ^(nexus_port_t p) {
4688 struct nexus_adapter *na = nx_port_get_na(nx, p);
4689 if (na == NULL || na->na_work_ts == 0 || na->na_rx_rings == NULL) {
4690 return;
4691 }
4692
4693 boolean_t purge;
4694
4695 /*
4696 * If some activity happened in the last FSW_DRAIN_CH_THRES
4697 * seconds on this channel, we reclaim memory if the channel
4698 * throughput is less than the reap threshold value.
4699 */
4700 if ((now - na->na_work_ts) < FSW_DRAIN_CH_THRES) {
4701 struct __kern_channel_ring *__single ring;
4702 channel_ring_stats *stats;
4703 uint64_t bps;
4704
4705 ring = KR_SINGLE(na->na_rx_rings);
4706 stats = &ring->ckr_stats;
4707 bps = stats->crs_bytes_per_second;
4708
4709 if (bps < fsw_channel_reap_thresh) {
4710 purge = FALSE;
4711 na_drain(na, purge);
4712 }
4713 return;
4714 }
4715
4716 /*
4717 * If NA has been inactive for some time (twice the drain
4718 * threshold), we clear the work timestamp to temporarily skip
4719 * this channel until it's active again. Purging cached objects
4720 * can be expensive since we'd need to allocate and construct
4721 * them again, so we do it only when necessary.
4722 */
4723 if (low || ((now - na->na_work_ts) >= (FSW_DRAIN_CH_THRES << 1))) {
4724 na->na_work_ts = 0;
4725 purge = TRUE;
4726 } else {
4727 purge = FALSE;
4728 }
4729
4730 na_drain(na, purge); /* purge/prune caches */
4731 });
4732 /* END IGNORE CODESTYLE */
4733
4734 FSW_RUNLOCK(fsw);
4735 }
4736
4737 static void
fsw_purge_cache(struct nx_flowswitch * fsw,boolean_t low)4738 fsw_purge_cache(struct nx_flowswitch *fsw, boolean_t low)
4739 {
4740 #pragma unused(fsw)
4741 uint64_t o = os_atomic_inc_orig(&fsw_want_purge, relaxed);
4742 uint32_t p = fsw_flow_purge_thresh;
4743 boolean_t purge = (low || (o != 0 && p != 0 && (o % p) == 0));
4744
4745 SK_DF(SK_VERB_FLOW, "%s: %s caches",
4746 fsw->fsw_flow_mgr->fm_name,
4747 (purge ? "purge" : "prune"));
4748
4749 skmem_cache_reap_now(sk_fo_cache, purge);
4750 skmem_cache_reap_now(sk_fe_cache, purge);
4751 skmem_cache_reap_now(sk_fab_cache, purge);
4752 skmem_cache_reap_now(flow_route_cache, purge);
4753 skmem_cache_reap_now(flow_stats_cache, purge);
4754 netns_reap_caches(purge);
4755 skmem_reap_caches(purge);
4756
4757 #if CONFIG_MBUF_MCACHE
4758 if (if_is_fsw_transport_netagent_enabled() && purge) {
4759 mbuf_drain(FALSE);
4760 }
4761 #endif /* CONFIG_MBUF_MCACHE */
4762 }
4763
4764 static void
fsw_flow_handle_low_power(struct nx_flowswitch * fsw,struct flow_entry * fe)4765 fsw_flow_handle_low_power(struct nx_flowswitch *fsw, struct flow_entry *fe)
4766 {
4767 /* When the interface is in low power mode, the flow is nonviable */
4768 if (!(fe->fe_flags & FLOWENTF_NONVIABLE) &&
4769 os_atomic_cmpxchg(&fe->fe_want_nonviable, 0, 1, acq_rel)) {
4770 os_atomic_inc(&fsw->fsw_pending_nonviable, relaxed);
4771 }
4772 }
4773
4774 static uint32_t
fsw_process_deferred(struct nx_flowswitch * fsw)4775 fsw_process_deferred(struct nx_flowswitch *fsw)
4776 {
4777 struct flow_entry_dead sfed __sk_aligned(8);
4778 struct flow_mgr *fm = fsw->fsw_flow_mgr;
4779 struct flow_entry_dead *fed, *tfed;
4780 LIST_HEAD(, flow_entry_dead) fed_head =
4781 LIST_HEAD_INITIALIZER(fed_head);
4782 uint32_t i, nonviable = 0;
4783 boolean_t lowpowermode = FALSE;
4784
4785 bzero(&sfed, sizeof(sfed));
4786
4787 /*
4788 * The flows become nonviable when the interface
4789 * is in low power mode (edge trigger)
4790 */
4791 if ((fsw->fsw_ifp->if_xflags & IFXF_LOW_POWER) &&
4792 fsw->fsw_ifp->if_low_power_gencnt != fsw->fsw_low_power_gencnt) {
4793 lowpowermode = TRUE;
4794 fsw->fsw_low_power_gencnt = fsw->fsw_ifp->if_low_power_gencnt;
4795 }
4796
4797 /*
4798 * Scan thru the flow entry tree, and commit any pending withdraw or
4799 * nonviable requests. We may need to push stats and/or unassign the
4800 * nexus from NECP, but we cannot do that while holding the locks;
4801 * build a temporary list for those entries.
4802 */
4803 for (i = 0; i < fm->fm_owner_buckets_cnt; i++) {
4804 struct flow_owner_bucket *fob = flow_mgr_get_fob_at_idx(fm, i);
4805 struct flow_owner *fo;
4806
4807 /*
4808 * Grab the lock at all costs when handling low power mode
4809 */
4810 if (__probable(!lowpowermode)) {
4811 if (!FOB_TRY_LOCK(fob)) {
4812 continue;
4813 }
4814 } else {
4815 FOB_LOCK(fob);
4816 }
4817
4818 FOB_LOCK_ASSERT_HELD(fob);
4819 RB_FOREACH(fo, flow_owner_tree, &fob->fob_owner_head) {
4820 struct flow_entry *fe;
4821
4822 RB_FOREACH(fe, flow_entry_id_tree,
4823 &fo->fo_flow_entry_id_head) {
4824 /* try first as reader; skip if we can't */
4825 if (__improbable(lowpowermode)) {
4826 fsw_flow_handle_low_power(fsw, fe);
4827 }
4828 if (__improbable(fe->fe_flags & FLOWENTF_HALF_CLOSED)) {
4829 os_atomic_andnot(&fe->fe_flags, FLOWENTF_HALF_CLOSED, relaxed);
4830 flow_namespace_half_close(&fe->fe_port_reservation);
4831 }
4832
4833 /* if not withdrawn/nonviable, skip */
4834 if (!fe->fe_want_withdraw &&
4835 !fe->fe_want_nonviable) {
4836 continue;
4837 }
4838 /*
4839 * Here we're holding the lock as writer;
4840 * don't spend too much time as we're
4841 * blocking the data path now.
4842 */
4843 ASSERT(!uuid_is_null(fe->fe_uuid));
4844 /* only need flow UUID and booleans */
4845 uuid_copy(sfed.fed_uuid, fe->fe_uuid);
4846 sfed.fed_want_clonotify =
4847 (fe->fe_flags & FLOWENTF_CLOSE_NOTIFY);
4848 sfed.fed_want_nonviable = fe->fe_want_nonviable;
4849 flow_entry_teardown(fo, fe);
4850
4851 /* do this outside the flow bucket lock */
4852 fed = flow_entry_dead_alloc(Z_WAITOK);
4853 ASSERT(fed != NULL);
4854 *fed = sfed;
4855 LIST_INSERT_HEAD(&fed_head, fed, fed_link);
4856 }
4857 }
4858 FOB_UNLOCK(fob);
4859 }
4860
4861 /*
4862 * These nonviable flows are no longer useful since we've lost
4863 * the source IP address; in the event the client monitors the
4864 * viability of the flow, explicitly mark it as nonviable so
4865 * that a new flow can be created.
4866 */
4867 LIST_FOREACH_SAFE(fed, &fed_head, fed_link, tfed) {
4868 LIST_REMOVE(fed, fed_link);
4869 ASSERT(fsw->fsw_agent_session != NULL);
4870
4871 /* if flow is closed early */
4872 if (fed->fed_want_clonotify) {
4873 necp_client_early_close(fed->fed_uuid);
4874 }
4875
4876 /* if nonviable, unassign nexus attributes */
4877 if (fed->fed_want_nonviable) {
4878 (void) netagent_assign_nexus(fsw->fsw_agent_session,
4879 fed->fed_uuid, NULL, 0);
4880 }
4881
4882 flow_entry_dead_free(fed);
4883 ++nonviable;
4884 }
4885 ASSERT(LIST_EMPTY(&fed_head));
4886
4887 return nonviable;
4888 }
4889
4890 static uint32_t
fsw_process_linger(struct nx_flowswitch * fsw,uint32_t * abort)4891 fsw_process_linger(struct nx_flowswitch *fsw, uint32_t *abort)
4892 {
4893 struct flow_entry_linger_head linger_head =
4894 TAILQ_HEAD_INITIALIZER(linger_head);
4895 struct flow_entry *fe, *tfe;
4896 uint64_t now = _net_uptime;
4897 uint32_t i = 0, cnt = 0, freed = 0;
4898
4899 ASSERT(fsw->fsw_ifp != NULL);
4900 ASSERT(abort != NULL);
4901 *abort = 0;
4902
4903 /*
4904 * We don't want to contend with the datapath, so move
4905 * everything that's in the linger list into a local list.
4906 * This allows us to generate RSTs or free the flow entry
4907 * outside the lock. Any remaining flow entry in the local
4908 * list will get re-added back to the head of the linger
4909 * list, in front of any new ones added since then.
4910 */
4911 lck_mtx_lock(&fsw->fsw_linger_lock);
4912 TAILQ_CONCAT(&linger_head, &fsw->fsw_linger_head, fe_linger_link);
4913 ASSERT(TAILQ_EMPTY(&fsw->fsw_linger_head));
4914 cnt = fsw->fsw_linger_cnt;
4915 fsw->fsw_linger_cnt = 0;
4916 lck_mtx_unlock(&fsw->fsw_linger_lock);
4917
4918 TAILQ_FOREACH_SAFE(fe, &linger_head, fe_linger_link, tfe) {
4919 ASSERT(fe->fe_flags & FLOWENTF_TORN_DOWN);
4920 ASSERT(fe->fe_flags & FLOWENTF_DESTROYED);
4921 ASSERT(fe->fe_flags & FLOWENTF_LINGERING);
4922
4923 /*
4924 * See if this is a TCP flow that needs to generate
4925 * a RST to the remote peer (if not already).
4926 */
4927 if (flow_track_tcp_want_abort(fe)) {
4928 VERIFY(fe->fe_flags & FLOWENTF_ABORTED);
4929 ASSERT(!uuid_is_null(fe->fe_uuid));
4930 flow_track_abort_tcp(fe, NULL, NULL);
4931 (*abort)++;
4932 SK_LOG_VAR(char dbgbuf[FLOWENTRY_DBGBUF_SIZE]);
4933 SK_DF(SK_VERB_FLOW, "entry \"%s\" fe 0x%llx "
4934 "flags 0x%b [RST]", fe_as_string(fe, dbgbuf,
4935 sizeof(dbgbuf)), SK_KVA(fe), fe->fe_flags,
4936 FLOWENTF_BITS);
4937 }
4938
4939 /*
4940 * If flow has expired, remove from list and free;
4941 * otherwise leave it around in the linger list.
4942 */
4943 if (fe->fe_linger_expire <= now) {
4944 freed++;
4945 fsw_linger_remove_internal(&linger_head, fe);
4946 fe = NULL;
4947 }
4948 ++i;
4949 }
4950 VERIFY(i == cnt && cnt >= freed);
4951
4952 /*
4953 * Add any remaining ones back into the linger list.
4954 */
4955 lck_mtx_lock(&fsw->fsw_linger_lock);
4956 if (!TAILQ_EMPTY(&linger_head)) {
4957 ASSERT(TAILQ_EMPTY(&fsw->fsw_linger_head) || fsw->fsw_linger_cnt);
4958 TAILQ_CONCAT(&linger_head, &fsw->fsw_linger_head, fe_linger_link);
4959 ASSERT(TAILQ_EMPTY(&fsw->fsw_linger_head));
4960 TAILQ_CONCAT(&fsw->fsw_linger_head, &linger_head, fe_linger_link);
4961 fsw->fsw_linger_cnt += (cnt - freed);
4962 }
4963 ASSERT(TAILQ_EMPTY(&linger_head));
4964 lck_mtx_unlock(&fsw->fsw_linger_lock);
4965
4966 return freed;
4967 }
4968
4969 __attribute__((always_inline))
4970 static inline void
fsw_ifp_inc_traffic_class_in_pkt(struct ifnet * ifp,kern_packet_t ph)4971 fsw_ifp_inc_traffic_class_in_pkt(struct ifnet *ifp, kern_packet_t ph)
4972 {
4973 switch (__packet_get_traffic_class(ph)) {
4974 case PKT_TC_BE:
4975 ifp->if_tc.ifi_ibepackets++;
4976 ifp->if_tc.ifi_ibebytes += SK_PTR_ADDR_KPKT(ph)->pkt_length;
4977 break;
4978 case PKT_TC_BK:
4979 ifp->if_tc.ifi_ibkpackets++;
4980 ifp->if_tc.ifi_ibkbytes += SK_PTR_ADDR_KPKT(ph)->pkt_length;
4981 break;
4982 case PKT_TC_VI:
4983 ifp->if_tc.ifi_ivipackets++;
4984 ifp->if_tc.ifi_ivibytes += SK_PTR_ADDR_KPKT(ph)->pkt_length;
4985 break;
4986 case PKT_TC_VO:
4987 ifp->if_tc.ifi_ivopackets++;
4988 ifp->if_tc.ifi_ivobytes += SK_PTR_ADDR_KPKT(ph)->pkt_length;
4989 break;
4990 default:
4991 break;
4992 }
4993 }
4994
4995 __attribute__((always_inline))
4996 static inline void
fsw_ifp_inc_traffic_class_out_pkt(struct ifnet * ifp,uint32_t svc,uint32_t cnt,uint32_t len)4997 fsw_ifp_inc_traffic_class_out_pkt(struct ifnet *ifp, uint32_t svc,
4998 uint32_t cnt, uint32_t len)
4999 {
5000 switch (svc) {
5001 case PKT_TC_BE:
5002 ifp->if_tc.ifi_obepackets += cnt;
5003 ifp->if_tc.ifi_obebytes += len;
5004 break;
5005 case PKT_TC_BK:
5006 ifp->if_tc.ifi_obkpackets += cnt;
5007 ifp->if_tc.ifi_obkbytes += len;
5008 break;
5009 case PKT_TC_VI:
5010 ifp->if_tc.ifi_ovipackets += cnt;
5011 ifp->if_tc.ifi_ovibytes += len;
5012 break;
5013 case PKT_TC_VO:
5014 ifp->if_tc.ifi_ovopackets += cnt;
5015 ifp->if_tc.ifi_ovobytes += len;
5016 break;
5017 default:
5018 break;
5019 }
5020 }
5021