Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
 * Copyright (c) 2019-2021 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
#include <skywalk/os_skywalk_private.h>
#include <skywalk/nexus/netif/nx_netif.h>
#include <net/pktap.h>
#include <sys/sdt.h>

SK_NO_INLINE_ATTRIBUTE
struct __kern_packet *
nx_netif_alloc_packet(struct kern_pbufpool *pp, uint32_t sz, kern_packet_t *php)
{
	kern_packet_t ph;
	ph = pp_alloc_packet_by_size(pp, sz, SKMEM_NOSLEEP);
	if (__improbable(ph == 0)) {
		DTRACE_SKYWALK2(alloc__fail, struct kern_pbufpool *,
		    pp, size_t, sz);
		return NULL;
	}
	if (php != NULL) {
		*php = ph;
	}
	return SK_PTR_ADDR_KPKT(ph);
}

SK_NO_INLINE_ATTRIBUTE
void
nx_netif_free_packet(struct __kern_packet *pkt)
{
	pp_free_packet_single(pkt);
}

SK_NO_INLINE_ATTRIBUTE
void
nx_netif_free_packet_chain(struct __kern_packet *pkt_chain, int *cnt)
{
	pp_free_packet_chain(pkt_chain, cnt);
}

static void
__check_convert_flags(uint32_t flags)
{
	VERIFY((flags & (NETIF_CONVERT_TX | NETIF_CONVERT_RX)) != 0);
	VERIFY((flags & (NETIF_CONVERT_TX | NETIF_CONVERT_RX)) !=
	    (NETIF_CONVERT_TX | NETIF_CONVERT_RX));
}

SK_NO_INLINE_ATTRIBUTE
static void
fill_vlan_info(struct __kern_packet *fpkt)
{
	uint8_t *buf;
	struct ether_vlan_header *evl;
	uint16_t tag;
	boolean_t tag_in_pkt = FALSE;

	if (fpkt->pkt_length < sizeof(*evl)) {
		DTRACE_SKYWALK2(bad__len, struct __kern_packet *, fpkt,
		    uint32_t, fpkt->pkt_length);
		return;
	}
	MD_BUFLET_ADDR_ABS(fpkt, buf);
	buf += fpkt->pkt_headroom;
	evl = (struct ether_vlan_header *)(void *)buf;
	if (ntohs(evl->evl_encap_proto) == ETHERTYPE_VLAN) {
		tag = ntohs(evl->evl_tag);
		tag_in_pkt = TRUE;
		DTRACE_SKYWALK1(tag__in__pkt, uint16_t, tag);
	} else {
		struct mbuf *m;
		struct __kern_packet *pkt;

		/*
		 * A filter packet must always have an mbuf or a packet
		 * attached.
		 */
		VERIFY((fpkt->pkt_pflags & PKT_F_MBUF_DATA) != 0 ||
		    (fpkt->pkt_pflags & PKT_F_PKT_DATA) != 0);

		if ((fpkt->pkt_pflags & PKT_F_MBUF_DATA) != 0) {
			m = fpkt->pkt_mbuf;
			VERIFY(m != NULL);
			if (mbuf_get_vlan_tag(m, &tag) != 0) {
				return;
			}
			DTRACE_SKYWALK1(tag__from__mbuf, uint16_t, tag);
		} else if ((fpkt->pkt_pflags & PKT_F_PKT_DATA) != 0) {
			pkt = fpkt->pkt_pkt;
			VERIFY(pkt != NULL);

			/*
			 * The attached packet could have an mbuf attached
			 * if it came from the compat path.
			 */
			if ((pkt->pkt_pflags & PKT_F_MBUF_DATA) != 0) {
				m = fpkt->pkt_mbuf;
				VERIFY(m != NULL);
				if (mbuf_get_vlan_tag(m, &tag) != 0) {
					return;
				}
				DTRACE_SKYWALK1(tag__from__inner__mbuf,
				    uint16_t, tag);
			} else {
				/*
				 * XXX
				 * No native driver today fills in the vlan tag
				 * metadata. This code will work when the driver
				 * adds support for this.
				 */
				VERIFY((pkt->pkt_pflags & PKT_F_PKT_DATA) == 0);
				if (__packet_get_vlan_tag(SK_PKT2PH(pkt), &tag,
				    NULL) != 0) {
					return;
				}
				DTRACE_SKYWALK1(tag__from__pkt, uint16_t, tag);
			}
		} else {
			panic("filter packet has no mbuf or packet attached: "
			    "pkt_pflags 0x%llx\n", fpkt->pkt_pflags);
			/* NOTREACHED */
			__builtin_unreachable();
		}
	}
	kern_packet_set_vlan_tag(SK_PKT2PH(fpkt), tag, tag_in_pkt);
}

static struct __kern_packet *
nx_netif_mbuf_to_filter_pkt(struct nexus_netif_adapter *nifna,
    struct mbuf *m, uint32_t flags)
{
	struct __kern_packet *fpkt = NULL;
	struct nx_netif *nif = nifna->nifna_netif;
	struct netif_stats *nifs = &nif->nif_stats;
	struct kern_pbufpool *pp = nif->nif_filter_pp;
	ifnet_t ifp = nif->nif_ifp;
	boolean_t is_l3, truncated = FALSE;
	enum txrx type;
	uint8_t off, hlen;
	kern_packet_t fph;
	int err, mlen;

	__check_convert_flags(flags);
	is_l3 = (ifp->if_family == IFNET_FAMILY_UTUN ||
	    ifp->if_family == IFNET_FAMILY_IPSEC);

	off = ((flags & NETIF_CONVERT_TX) != 0) ?
	    (uint8_t)ifp->if_tx_headroom : 0;
	hlen = is_l3 ? 0 : ifnet_hdrlen(ifp);
	mlen = m_pktlen(m);

	ASSERT(pp != NULL);
	if (__improbable((off + mlen) > PP_BUF_SIZE_DEF(pp))) {
		VERIFY(off < PP_BUF_SIZE_DEF(pp));
		mlen = PP_BUF_SIZE_DEF(pp) - off;
		truncated = TRUE;

		DTRACE_SKYWALK5(mbuf__truncated,
		    struct nexus_netif_adapter *, nifna,
		    struct mbuf *, m, uint8_t, off, int, mlen,
		    uint32_t, PP_BUF_SIZE_DEF(pp));
		STATS_INC(nifs, NETIF_STATS_FILTER_PKT_TRUNCATED);
	}
	fpkt = nx_netif_alloc_packet(pp, off + mlen, &fph);
	if (__improbable(fpkt == NULL)) {
		DTRACE_SKYWALK2(alloc__fail, struct nexus_netif_adapter *,
		    nifna, struct mbuf *, m);
		STATS_INC(nifs, NETIF_STATS_FILTER_DROP_PKT_ALLOC_FAIL);
		goto drop;
	}
	type = ((flags & NETIF_CONVERT_TX) != 0) ? NR_TX : NR_RX;

	if (__improbable((m->m_flags & M_HASFCS) != 0)) {
		if (type != NR_RX) {
			/*
			 * There shouldn't be an FCS for TX packets
			 */
			DTRACE_SKYWALK2(bad__flags,
			    struct nexus_netif_adapter *,
			    nifna, struct mbuf *, m);
			goto drop;
		}
		if (mlen > ETHER_CRC_LEN) {
			mlen -= ETHER_CRC_LEN;
		} else {
			DTRACE_SKYWALK3(bad__pkt__size,
			    struct nexus_netif_adapter *,
			    nifna, struct mbuf *, m, int, mlen);
			goto drop;
		}
	}
	/*
	 * XXX
	 * If the source packet has any checksum flags, the filter packet will
	 * not have valid checksums. To fill in the checksums, we need to do
	 * something similar to bridge_finalize_cksum() for packets.
	 */
	err = __packet_initialize_with_mbuf(fpkt, m, off, hlen);
	VERIFY(err == 0);
	nif->nif_pkt_copy_from_mbuf(type, fph, off, m, 0,
	    mlen, FALSE, 0);

	err = __packet_finalize_with_mbuf(fpkt);
	VERIFY(err == 0);

	/*
	 * XXX
	 * __packet_finalize_with_mbuf() sets pkt_length to the non-truncated
	 * length. We need to change it back to the truncated length.
	 */
	fpkt->pkt_length = mlen;
	if (!is_l3) {
		fill_vlan_info(fpkt);
	}

	/*
	 * Verify that __packet_finalize_with_mbuf() is setting the truncated
	 * flag correctly.
	 */
	if (truncated) {
		VERIFY((fpkt->pkt_pflags & PKT_F_TRUNCATED) != 0);
	} else {
		VERIFY((fpkt->pkt_pflags & PKT_F_TRUNCATED) == 0);
	}
	return fpkt;
drop:
	if (fpkt != NULL) {
		/* ensure mbuf hasn't been attached */
		ASSERT(fpkt->pkt_mbuf == NULL &&
		    (fpkt->pkt_pflags & PKT_F_MBUF_DATA) == 0);
		nx_netif_free_packet(fpkt);
	}
	STATS_INC(nifs, NETIF_STATS_DROP);
	m_freem(m);
	return NULL;
}

struct __kern_packet *
nx_netif_mbuf_to_filter_pkt_chain(struct nexus_netif_adapter *nifna,
    struct mbuf *m_chain, uint32_t flags)
{
	struct mbuf *m = m_chain, *next;
	struct __kern_packet *pkt_head = NULL, *pkt;
	struct __kern_packet **pkt_tailp = &pkt_head;
	int c = 0;

	while (m != NULL) {
		next = m->m_nextpkt;
		m->m_nextpkt = NULL;

		pkt = nx_netif_mbuf_to_filter_pkt(nifna, m, flags);
		if (pkt != NULL) {
			c++;
			*pkt_tailp = pkt;
			pkt_tailp = &pkt->pkt_nextpkt;
		}
		m = next;
	}
	DTRACE_SKYWALK2(pkt__chain, struct __kern_packet *, pkt_head,
	    int, c);
	return pkt_head;
}

static struct mbuf *
nx_netif_filter_pkt_to_mbuf(struct nexus_netif_adapter *nifna,
    struct __kern_packet *pkt, uint32_t flags)
{
#pragma unused (nifna)
	struct mbuf *m;

	__check_convert_flags(flags);
	ASSERT((pkt->pkt_pflags & PKT_F_MBUF_DATA) != 0);

	m = pkt->pkt_mbuf;
	ASSERT(m != NULL);
	KPKT_CLEAR_MBUF_DATA(pkt);
	nx_netif_free_packet(pkt);
	return m;
}

struct mbuf *
nx_netif_filter_pkt_to_mbuf_chain(struct nexus_netif_adapter *nifna,
    struct __kern_packet *pkt_chain, uint32_t flags)
{
	struct __kern_packet *pkt = pkt_chain, *next;
	struct mbuf *m_head = NULL, *m;
	struct mbuf **m_tailp = &m_head;
	int c = 0;

	while (pkt != NULL) {
		next = pkt->pkt_nextpkt;
		pkt->pkt_nextpkt = NULL;

		m = nx_netif_filter_pkt_to_mbuf(nifna, pkt, flags);
		if (m != NULL) {
			c++;
			*m_tailp = m;
			m_tailp = &m->m_nextpkt;
		}
		pkt = next;
	}
	DTRACE_SKYWALK2(mbuf__chain, struct mbuf *, m_head, int, c);
	return m_head;
}

struct __kern_packet *
nx_netif_pkt_to_filter_pkt(struct nexus_netif_adapter *nifna,
    struct __kern_packet *pkt, uint32_t flags)
{
	struct __kern_packet *fpkt = NULL;
	struct nx_netif *nif = nifna->nifna_netif;
	struct netif_stats *nifs = &nif->nif_stats;
	struct kern_pbufpool *pp = nif->nif_filter_pp;
	ifnet_t ifp = nif->nif_ifp;
	boolean_t is_l3, truncated = FALSE;
	enum txrx type;
	uint8_t off, hlen;
	struct mbuf *m = NULL;
	kern_packet_t fph, ph;
	int err, plen;

	__check_convert_flags(flags);
	ph = SK_PKT2PH(pkt);
	is_l3 = (ifp->if_family == IFNET_FAMILY_UTUN ||
	    ifp->if_family == IFNET_FAMILY_IPSEC);

	off = ((flags & NETIF_CONVERT_TX) != 0) ?
	    (uint8_t)ifp->if_tx_headroom : 0;
	hlen = is_l3 ? 0 : ifnet_hdrlen(ifp);

	/*
	 * The packet coming from the compat path could be empty or has
	 * truncated contents. We have to copy the contents from the
	 * attached mbuf. We also don't support attaching a filter
	 * packet (one that already has a packet attached) to another
	 * filter packet.
	 */
	ASSERT((pkt->pkt_pflags & PKT_F_PKT_DATA) == 0);
	if ((pkt->pkt_pflags & PKT_F_MBUF_DATA) != 0) {
		m = pkt->pkt_mbuf;
		plen = m_pktlen(m);
	} else {
		plen = pkt->pkt_length;
	}
	ASSERT(pp != NULL);
	if (__improbable((off + plen) > PP_BUF_SIZE_DEF(pp))) {
		VERIFY(off < PP_BUF_SIZE_DEF(pp));
		plen = PP_BUF_SIZE_DEF(pp) - off;
		truncated = TRUE;

		DTRACE_SKYWALK5(pkt__truncated,
		    struct nexus_netif_adapter *, nifna,
		    struct __kern_packet *, pkt, uint8_t, off,
		    int, plen, uint32_t, PP_BUF_SIZE_DEF(pp));
		STATS_INC(nifs, NETIF_STATS_FILTER_PKT_TRUNCATED);
	}
	fpkt = nx_netif_alloc_packet(pp, off + plen, &fph);
	if (__improbable(fpkt == NULL)) {
		DTRACE_SKYWALK2(alloc__fail, struct nexus_netif_adapter *,
		    nifna, struct __kern_packet *, pkt);
		STATS_INC(nifs, NETIF_STATS_FILTER_DROP_PKT_ALLOC_FAIL);
		goto drop;
	}
	fpkt->pkt_link_flags = 0;
	fpkt->pkt_headroom = off;
	fpkt->pkt_l2_len = hlen;
	type = ((flags & NETIF_CONVERT_TX) != 0) ? NR_TX : NR_RX;

	if (__improbable((pkt->pkt_link_flags & PKT_LINKF_ETHFCS) != 0 ||
	    (m != NULL && (m->m_flags & M_HASFCS) != 0))) {
		if (type != NR_RX) {
			/*
			 * There shouldn't be an FCS for TX packets
			 */
			DTRACE_SKYWALK2(bad__flags,
			    struct nexus_netif_adapter *, nifna,
			    struct __kern_packet *, pkt);
			goto drop;
		}
		if (plen > ETHER_CRC_LEN) {
			plen -= ETHER_CRC_LEN;
		} else {
			DTRACE_SKYWALK3(bad__pkt__size,
			    struct nexus_netif_adapter *, nifna,
			    struct __kern_packet *, pkt, int, plen);
			goto drop;
		}
	}
	/*
	 * XXX
	 * If the source packet has any checksum flags, the filter packet will
	 * not have valid checksums. To fill in the checksums, we need to do
	 * something similar to bridge_finalize_cksum() for packets.
	 */
	if (m != NULL) {
		nif->nif_pkt_copy_from_mbuf(type, fph, off, m, 0,
		    plen, FALSE, 0);
	} else {
		nif->nif_pkt_copy_from_pkt(type, fph, off, ph,
		    pkt->pkt_headroom, plen, FALSE, 0, 0, FALSE);
	}
	ASSERT((fpkt->pkt_pflags & PKT_F_PKT_DATA) == 0);
	ASSERT((fpkt->pkt_pflags & PKT_F_MBUF_DATA) == 0);
	ASSERT(fpkt->pkt_pkt == NULL);
	ASSERT(pkt->pkt_nextpkt == NULL);
	fpkt->pkt_pkt = pkt;
	fpkt->pkt_pflags |= PKT_F_PKT_DATA;
	if (truncated) {
		fpkt->pkt_pflags |= PKT_F_TRUNCATED;
	}
	/*
	 * XXX
	 * Unlike the mbuf case, __packet_finalize below correctly sets
	 * pkt_length to the buflet length (possibly truncated). We set
	 * pkt_length here so that fill_vlan_info can use it.
	 */
	fpkt->pkt_length = plen;
	if (!is_l3) {
		fill_vlan_info(fpkt);
	}
	err = __packet_finalize(fph);
	VERIFY(err == 0);
	return fpkt;
drop:
	if (fpkt != NULL) {
		/* ensure pkt hasn't been attached */
		ASSERT(fpkt->pkt_pkt == NULL &&
		    (fpkt->pkt_pflags & PKT_F_PKT_DATA) == 0);
		nx_netif_free_packet(fpkt);
	}
	STATS_INC(nifs, NETIF_STATS_DROP);
	nx_netif_free_packet(pkt);
	return NULL;
}

struct __kern_packet *
nx_netif_pkt_to_filter_pkt_chain(struct nexus_netif_adapter *nifna,
    struct __kern_packet *pkt_chain, uint32_t flags)
{
	struct __kern_packet *pkt = pkt_chain, *next;
	struct __kern_packet *p_head = NULL, *p;
	struct __kern_packet **p_tailp = &p_head;
	int c = 0;

	while (pkt != NULL) {
		next = pkt->pkt_nextpkt;
		pkt->pkt_nextpkt = NULL;

		p = nx_netif_pkt_to_filter_pkt(nifna, pkt, flags);
		if (p != NULL) {
			c++;
			*p_tailp = p;
			p_tailp = &p->pkt_nextpkt;
		}
		pkt = next;
	}
	DTRACE_SKYWALK2(pkt__chain, struct __kern_packet *, p_head, int, c);
	return p_head;
}

static struct __kern_packet *
nx_netif_filter_pkt_to_pkt(struct nexus_netif_adapter *nifna,
    struct __kern_packet *fpkt, uint32_t flags)
{
#pragma unused (nifna)
	struct __kern_packet *pkt;

	__check_convert_flags(flags);
	ASSERT((fpkt->pkt_pflags & PKT_F_PKT_DATA) != 0);
	ASSERT((fpkt->pkt_pflags & PKT_F_MBUF_DATA) == 0);

	pkt = fpkt->pkt_pkt;
	ASSERT(pkt != NULL);
	KPKT_CLEAR_PKT_DATA(fpkt);
	nx_netif_free_packet(fpkt);
	return pkt;
}

struct __kern_packet *
nx_netif_filter_pkt_to_pkt_chain(struct nexus_netif_adapter *nifna,
    struct __kern_packet *pkt_chain, uint32_t flags)
{
	struct __kern_packet *pkt = pkt_chain, *next;
	struct __kern_packet *p_head = NULL, *p;
	struct __kern_packet **p_tailp = &p_head;
	int c = 0;

	while (pkt != NULL) {
		next = pkt->pkt_nextpkt;
		pkt->pkt_nextpkt = NULL;

		p = nx_netif_filter_pkt_to_pkt(nifna, pkt, flags);
		if (p != NULL) {
			c++;
			*p_tailp = p;
			p_tailp = &p->pkt_nextpkt;
		}
		pkt = next;
	}
	DTRACE_SKYWALK2(pkt__chain, struct __kern_packet *, p_head, int, c);
	return p_head;
}

struct mbuf *
nx_netif_pkt_to_mbuf(struct nexus_netif_adapter *nifna,
    struct __kern_packet *pkt, uint32_t flags)
{
	struct nx_netif *nif = nifna->nifna_netif;
	ifnet_t ifp = nif->nif_ifp;
	struct mbuf *m;
	unsigned int one = 1;
	size_t len;
	uint16_t pad, hlen;
	kern_packet_t ph;
	enum txrx type;
	int err;

	__check_convert_flags(flags);
	/* Compat packets or filter packets should never land here */
	ASSERT((pkt->pkt_pflags & PKT_F_MBUF_DATA) == 0);
	ASSERT((pkt->pkt_pflags & PKT_F_PKT_DATA) == 0);

	/* Outbound packets should not have this */
	ASSERT((pkt->pkt_link_flags & PKT_LINKF_ETHFCS) == 0);

	/* This function is only meant to be used in the custom ether TX path */
	ASSERT((flags & NETIF_CONVERT_TX) != 0);
	type = NR_TX;

	/* Packet must include L2 header */
	hlen = ifnet_hdrlen(ifp);
	pad = (uint16_t)P2ROUNDUP(hlen, sizeof(uint32_t)) - hlen;
	len = pkt->pkt_length;

	err = mbuf_allocpacket(MBUF_WAITOK, pad + len, &one, &m);
	VERIFY(err == 0);
	m->m_data += pad;
	m->m_pkthdr.pkt_hdr = mtod(m, uint8_t *);
	ph = SK_PTR_ENCODE(pkt, METADATA_TYPE(pkt), METADATA_SUBTYPE(pkt));

	nif->nif_pkt_copy_to_mbuf(type, ph, pkt->pkt_headroom,
	    m, 0, (uint32_t)len, FALSE, 0);
	nx_netif_free_packet(pkt);
	return m;
}

struct __kern_packet *
nx_netif_pkt_to_pkt(struct nexus_netif_adapter *nifna,
    struct __kern_packet *pkt, uint32_t flags)
{
	struct nx_netif *nif = nifna->nifna_netif;
	struct nexus_adapter *na = &nifna->nifna_up;
	struct netif_stats *nifs = &nif->nif_stats;
	ifnet_t ifp = nif->nif_ifp;
	struct kern_pbufpool *pp;
	struct __kern_packet *dpkt = NULL;
	struct mbuf *m = NULL;
	uint8_t off, hlen;
	int len;
	kern_packet_t ph, dph;
	enum txrx type;
	int err;

	__check_convert_flags(flags);
	/* Filter packets should never land here */
	ASSERT((pkt->pkt_pflags & PKT_F_PKT_DATA) == 0);

	/* Only support these target NAs for now */
	type = ((flags & NETIF_CONVERT_TX) != 0) ? NR_TX : NR_RX;
	if (type == NR_TX) {
		ASSERT(na->na_type == NA_NETIF_DEV ||
		    na->na_type == NA_NETIF_COMPAT_DEV);
		pp = skmem_arena_nexus(na->na_arena)->arn_tx_pp;
		off = (uint8_t)ifp->if_tx_headroom;
	} else {
		ASSERT(na->na_type == NA_NETIF_VP);
		pp = skmem_arena_nexus(na->na_arena)->arn_rx_pp;
		off = 0;
	}
	/* Packet must include L2 header */
	hlen = ifnet_hdrlen(ifp);

	/*
	 * Source packet has no data. Need to copy from the attached mbuf.
	 */
	if ((pkt->pkt_pflags & PKT_F_MBUF_DATA) != 0) {
		/* An outbound packet shouldn't have an mbuf attached */
		ASSERT(na->na_type == NA_NETIF_VP);
		m = pkt->pkt_mbuf;
		len = m_pktlen(m);
	} else {
		len = pkt->pkt_length;
	}
	ASSERT(pp != NULL);

	ph = SK_PKT2PH(pkt);
	dpkt = nx_netif_alloc_packet(pp, off + len, &dph);
	if (__improbable(dpkt == NULL)) {
		if (type == NR_TX) {
			STATS_INC(nifs, NETIF_STATS_VP_DROP_TX_ALLOC_FAIL);
		} else {
			STATS_INC(nifs, NETIF_STATS_VP_DROP_RX_ALLOC_FAIL);
		}
		DTRACE_SKYWALK2(alloc__fail, struct nexus_netif_adapter *,
		    nifna, struct __kern_packet *, pkt);
		goto drop;
	}
	if (__improbable((off + len) > PP_BUF_SIZE_DEF(pp))) {
		STATS_INC(nifs, NETIF_STATS_VP_DROP_PKT_TOO_BIG);
		DTRACE_SKYWALK5(pkt__too__large,
		    struct nexus_netif_adapter *, nifna,
		    struct __kern_packet *, pkt, uint8_t, off, int, len,
		    uint32_t, PP_BUF_SIZE_DEF(pp));
		goto drop;
	}
	if (__improbable((pkt->pkt_link_flags & PKT_LINKF_ETHFCS) != 0 ||
	    (m != NULL && (m->m_flags & M_HASFCS) != 0))) {
		if (type != NR_RX) {
			/*
			 * There shouldn't be an FCS for TX packets
			 */
			DTRACE_SKYWALK2(bad__flags,
			    struct nexus_netif_adapter *, nifna,
			    struct __kern_packet *, pkt);
			goto drop;
		}
		if (len > ETHER_CRC_LEN) {
			len -= ETHER_CRC_LEN;
		} else {
			DTRACE_SKYWALK3(bad__pkt__size,
			    struct nexus_netif_adapter *, nifna,
			    struct __kern_packet *, pkt, int, len);
			goto drop;
		}
	}
	dpkt->pkt_link_flags = 0;
	dpkt->pkt_headroom = off;
	dpkt->pkt_l2_len = hlen;

	/* Copy optional metadata */
	dpkt->pkt_pflags = (pkt->pkt_pflags & PKT_F_COPY_MASK);
	_PKT_COPY_OPT_DATA(pkt, dpkt);

	/* Copy Transmit completion metadata */
	_PKT_COPY_TX_PORT_DATA(pkt, dpkt);

	/* Copy packet contents */
	if (m != NULL) {
		nif->nif_pkt_copy_from_mbuf(type, dph, off, m, 0,
		    len, FALSE, 0);
	} else {
		nif->nif_pkt_copy_from_pkt(type, dph, off, ph,
		    pkt->pkt_headroom, len, FALSE, 0, 0, FALSE);
	}
	err = __packet_finalize(dph);
	VERIFY(err == 0);
	nx_netif_free_packet(pkt);
	return dpkt;

drop:
	if (dpkt != NULL) {
		nx_netif_free_packet(dpkt);
	}
	STATS_INC(nifs, NETIF_STATS_DROP);
	nx_netif_free_packet(pkt);
	return NULL;
}

void
nx_netif_mbuf_chain_info(struct mbuf *m_head, struct mbuf **m_tail,
    uint32_t *cnt, uint32_t *bytes)
{
	struct mbuf *m = m_head, *tail = NULL;
	uint32_t c = 0, b = 0;

	while (m != NULL) {
		c++;
		b += m_pktlen(m);
		tail = m;
		m = m->m_nextpkt;
	}
	if (m_tail != NULL) {
		*m_tail = tail;
	}
	if (cnt != NULL) {
		*cnt = c;
	}
	if (bytes != NULL) {
		*bytes = b;
	}
}

void
nx_netif_pkt_chain_info(struct __kern_packet *p_head,
    struct __kern_packet **p_tail, uint32_t *cnt, uint32_t *bytes)
{
	struct __kern_packet *p = p_head, *tail = NULL;
	uint32_t c = 0, b = 0;

	while (p != NULL) {
		c++;
		b += p->pkt_length;
		tail = p;
		p = p->pkt_nextpkt;
	}
	if (p_tail != NULL) {
		*p_tail = tail;
	}
	if (cnt != NULL) {
		*cnt = c;
	}
	if (bytes != NULL) {
		*bytes = b;
	}
}

int
nx_netif_get_max_mtu(ifnet_t ifp, uint32_t *max_mtu)
{
	struct ifreq ifr;
	int err;

	bzero(&ifr, sizeof(ifr));
	err = ifnet_ioctl(ifp, 0, SIOCGIFDEVMTU, &ifr);
	if (err != 0) {
		SK_ERR("SIOCGIFDEVMTU failed for %s\n", if_name(ifp));
		return err;
	}
	*max_mtu = MAX(ifr.ifr_devmtu.ifdm_max, ifr.ifr_devmtu.ifdm_current);
	return 0;
}

void
nx_netif_pktap_output(ifnet_t ifp, int af, struct __kern_packet *pkt)
{
	uint32_t dlt;
	uint32_t flags = PTH_FLAG_SOCKET;

	switch (ifp->if_family) {
	case IFNET_FAMILY_ETHERNET:
		dlt = DLT_EN10MB;
		break;
	case IFNET_FAMILY_CELLULAR:
	case IFNET_FAMILY_UTUN:
	case IFNET_FAMILY_IPSEC:
		dlt = DLT_RAW;
		break;
	default:
		DTRACE_SKYWALK1(invalid__family, ifnet_t, ifp);
		return;
	}
	if ((pkt->pkt_pflags & PKT_F_KEEPALIVE) != 0) {
		flags |= PTH_FLAG_KEEP_ALIVE;
	}
	if ((pkt->pkt_pflags & PKT_F_REXMT) != 0) {
		flags |= PTH_FLAG_REXMIT;
	}
	pktap_output_packet(ifp, af, dlt, -1, NULL, -1, NULL, SK_PKT2PH(pkt),
	    NULL, 0, pkt->pkt_flow_ip_proto, pkt->pkt_flow_token, flags);
}