xref: /xnu-10002.1.13/bsd/skywalk/nexus/flowswitch/fsw_classq.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1 /*
2  * Copyright (c) 2016-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <skywalk/nexus/flowswitch/nx_flowswitch.h>
30 #include <skywalk/nexus/flowswitch/fsw_var.h>
31 #include <skywalk/nexus/netif/nx_netif.h>
32 
33 void
fsw_classq_setup(struct nx_flowswitch * fsw,struct nexus_adapter * hostna)34 fsw_classq_setup(struct nx_flowswitch *fsw, struct nexus_adapter *hostna)
35 {
36 	FSW_WLOCK_ASSERT_HELD(fsw);
37 	ASSERT(hostna->na_ifp->if_snd->ifcq_type != PKTSCHEDT_NONE);
38 	ASSERT(hostna->na_ifp->if_eflags & IFEF_TXSTART);
39 	if (hostna->na_type == NA_NETIF_COMPAT_HOST) {
40 		fsw->fsw_classq_enq_ptype = QP_MBUF;
41 	} else {
42 		ASSERT(hostna->na_type == NA_NETIF_HOST);
43 		fsw->fsw_classq_enq_ptype = QP_PACKET;
44 	}
45 }
46 
47 void
fsw_classq_teardown(struct nx_flowswitch * fsw,struct nexus_adapter * hostna)48 fsw_classq_teardown(struct nx_flowswitch *fsw, struct nexus_adapter *hostna)
49 {
50 #if !(DEVELOPMENT || DEBUG)
51 #pragma unused(fsw)
52 #endif
53 	FSW_WLOCK_ASSERT_HELD(fsw);
54 	ASSERT(hostna->na_ifp->if_snd->ifcq_type != PKTSCHEDT_NONE);
55 	ASSERT(hostna->na_ifp->if_eflags & IFEF_TXSTART);
56 	if (hostna->na_type == NA_NETIF_COMPAT_HOST) {
57 		ASSERT(fsw->fsw_classq_enq_ptype == QP_MBUF);
58 	} else {
59 		ASSERT(hostna->na_type == NA_NETIF_HOST);
60 		ASSERT(fsw->fsw_classq_enq_ptype == QP_PACKET);
61 	}
62 	/* flush the interface queues */
63 	if_qflush_snd(hostna->na_ifp, false);
64 }
65 
66 struct mbuf *
fsw_classq_kpkt_to_mbuf(struct nx_flowswitch * fsw,struct __kern_packet * pkt)67 fsw_classq_kpkt_to_mbuf(struct nx_flowswitch *fsw, struct __kern_packet *pkt)
68 {
69 	struct mbuf *m = NULL;
70 	unsigned int one = 1;
71 	int error;
72 
73 	error = mbuf_allocpacket(MBUF_WAITOK, pkt->pkt_length, &one, &m);
74 	VERIFY(error == 0);
75 
76 	STATS_INC(&fsw->fsw_stats, FSW_STATS_TX_COPY_PKT2MBUF);
77 	if (PACKET_HAS_PARTIAL_CHECKSUM(pkt)) {
78 		STATS_INC(&fsw->fsw_stats, FSW_STATS_TX_COPY_SUM);
79 	}
80 
81 	/* copy packet data */
82 	fsw->fsw_pkt_copy_to_mbuf(NR_TX, SK_PTR_ENCODE(pkt,
83 	    METADATA_TYPE(pkt), METADATA_SUBTYPE(pkt)), pkt->pkt_headroom,
84 	    m, 0, pkt->pkt_length, PACKET_HAS_PARTIAL_CHECKSUM(pkt),
85 	    pkt->pkt_csum_tx_start_off);
86 
87 	_CASSERT(sizeof(m->m_pkthdr.pkt_flowid) ==
88 	    sizeof(pkt->pkt_flow_token));
89 	_CASSERT(sizeof(m->m_pkthdr.pkt_mpriv_srcid) ==
90 	    sizeof(pkt->pkt_flowsrc_token));
91 	_CASSERT(sizeof(m->m_pkthdr.pkt_mpriv_fidx) ==
92 	    sizeof(pkt->pkt_flowsrc_fidx));
93 	_CASSERT(sizeof(m->m_pkthdr.comp_gencnt) ==
94 	    sizeof(pkt->pkt_comp_gencnt));
95 
96 	m->m_pkthdr.pkt_flowid = pkt->pkt_flow_token;
97 	m->m_pkthdr.comp_gencnt = pkt->pkt_comp_gencnt;
98 	m->m_pkthdr.pkt_mpriv_srcid = pkt->pkt_flowsrc_token;
99 	m->m_pkthdr.pkt_mpriv_fidx = pkt->pkt_flowsrc_fidx;
100 
101 	SK_DF(SK_VERB_TX | SK_VERB_DUMP, "%s(%d) %s",
102 	    sk_proc_name_address(current_proc()), sk_proc_pid(current_proc()),
103 	    sk_dump("buf", m->m_data, m->m_pkthdr.len, 128, NULL, 0));
104 
105 	if (__improbable((error != 0))) {
106 		if (m != NULL) {
107 			m_freem(m);
108 			m = NULL;
109 		}
110 	}
111 	return m;
112 }
113