1 /*
2 * Copyright (c) 2019-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/os_skywalk_private.h>
30 #include <skywalk/nexus/netif/nx_netif.h>
31 #include <skywalk/nexus/flowswitch/fsw_var.h>
32 #include <skywalk/nexus/flowswitch/nx_flowswitch.h>
33
34 /* function to send the packet transmit status event on the channel */
35 static inline errno_t
kern_channel_event_transmit_status_notify(const ifnet_t ifp,os_channel_event_packet_transmit_status_t * pkt_tx_status,uint32_t nx_port_id)36 kern_channel_event_transmit_status_notify(const ifnet_t ifp,
37 os_channel_event_packet_transmit_status_t *pkt_tx_status,
38 uint32_t nx_port_id)
39 {
40 char buf[CHANNEL_EVENT_TX_STATUS_LEN]
41 __attribute((aligned(sizeof(uint64_t))));
42 struct __kern_channel_event *event =
43 (struct __kern_channel_event *)(void *)buf;
44 os_channel_event_packet_transmit_status_t *ts_ev =
45 (os_channel_event_packet_transmit_status_t *)&event->ev_data;
46
47 if (!IF_FULLY_ATTACHED(ifp)) {
48 return ENXIO;
49 }
50
51 event->ev_type = CHANNEL_EVENT_PACKET_TRANSMIT_STATUS;
52 event->ev_flags = 0;
53 event->_reserved = 0;
54 event->ev_dlen = sizeof(os_channel_event_packet_transmit_status_t);
55 *ts_ev = *pkt_tx_status;
56
57 struct nx_flowswitch *fsw = fsw_ifp_to_fsw(ifp);
58 if (fsw == NULL) {
59 return netif_vp_na_channel_event(NA(ifp)->nifna_netif,
60 nx_port_id, event, CHANNEL_EVENT_TX_STATUS_LEN);
61 } else {
62 return fsw_vp_na_channel_event(fsw, nx_port_id, event,
63 CHANNEL_EVENT_TX_STATUS_LEN);
64 }
65 }
66
67 errno_t
kern_channel_event_transmit_status_with_packet(const kern_packet_t ph,const ifnet_t ifp)68 kern_channel_event_transmit_status_with_packet(const kern_packet_t ph,
69 const ifnet_t ifp)
70 {
71 int err;
72 uint32_t nx_port_id;
73 os_channel_event_packet_transmit_status_t pkt_tx_status;
74
75 (void) __packet_get_tx_completion_status(ph,
76 &pkt_tx_status.packet_status);
77 if (pkt_tx_status.packet_status == KERN_SUCCESS) {
78 return 0;
79 }
80 err = __packet_get_packetid(ph, &pkt_tx_status.packet_id);
81 if (__improbable(err != 0)) {
82 return err;
83 }
84 err = __packet_get_tx_nx_port_id(ph, &nx_port_id);
85 if (__improbable(err != 0)) {
86 return err;
87 }
88 return kern_channel_event_transmit_status_notify(ifp, &pkt_tx_status,
89 nx_port_id);
90 }
91
92 errno_t
kern_channel_event_transmit_status(const ifnet_t ifp,os_channel_event_packet_transmit_status_t * pkt_tx_status,uint32_t nx_port_id)93 kern_channel_event_transmit_status(const ifnet_t ifp,
94 os_channel_event_packet_transmit_status_t *pkt_tx_status,
95 uint32_t nx_port_id)
96 {
97 return kern_channel_event_transmit_status_notify(ifp, pkt_tx_status,
98 nx_port_id);
99 }
100
101 /* routine to post kevent notification for the event ring */
102 void
kern_channel_event_notify(struct __kern_channel_ring * kring)103 kern_channel_event_notify(struct __kern_channel_ring *kring)
104 {
105 ASSERT(kring->ckr_tx == NR_TX);
106
107 SK_DF(SK_VERB_EVENTS, "%s(%d) na \"%s\" (0x%llx) kr 0x%llx",
108 sk_proc_name_address(current_proc()), sk_proc_pid(current_proc()),
109 KRNA(kring)->na_name, SK_KVA(KRNA(kring)), SK_KVA(kring));
110
111 na_post_event(kring, TRUE, FALSE, FALSE, CHAN_FILT_HINT_CHANNEL_EVENT);
112 }
113
114 /* sync routine for the event ring */
115 int
kern_channel_event_sync(struct __kern_channel_ring * kring,struct proc * p,uint32_t flags)116 kern_channel_event_sync(struct __kern_channel_ring *kring, struct proc *p,
117 uint32_t flags)
118 {
119 #pragma unused(p, flags)
120 (void) kr_reclaim(kring);
121 return 0;
122 }
123