]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/kern/kpi_mbuf.c
xnu-792.25.20.tar.gz
[apple/xnu.git] / bsd / kern / kpi_mbuf.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#define __KPI__
24//#include <sys/kpi_interface.h>
25
26#include <sys/param.h>
27#include <sys/mbuf.h>
28#include <sys/socket.h>
29#include <kern/debug.h>
30#include <libkern/OSAtomic.h>
31#include <kern/kalloc.h>
32#include <string.h>
33
34void mbuf_tag_id_first_last(u_long *first, u_long *last);
35errno_t mbuf_tag_id_find_internal(const char *string, u_long *out_id, int create);
36
37static const mbuf_flags_t mbuf_flags_mask = MBUF_EXT | MBUF_PKTHDR | MBUF_EOR |
38 MBUF_BCAST | MBUF_MCAST | MBUF_FRAG | MBUF_FIRSTFRAG |
39 MBUF_LASTFRAG | MBUF_PROMISC;
40
41void* mbuf_data(mbuf_t mbuf)
42{
43 return m_mtod(mbuf);
44}
45
46void* mbuf_datastart(mbuf_t mbuf)
47{
48 if (mbuf->m_flags & M_EXT)
49 return mbuf->m_ext.ext_buf;
50 if (mbuf->m_flags & M_PKTHDR)
51 return mbuf->m_pktdat;
52 return mbuf->m_dat;
53}
54
55errno_t mbuf_setdata(mbuf_t mbuf, void* data, size_t len)
56{
57 size_t start = (size_t)((char*)mbuf_datastart(mbuf));
58 size_t maxlen = mbuf_maxlen(mbuf);
59
60 if ((size_t)data < start || ((size_t)data) + len > start + maxlen)
61 return EINVAL;
62 mbuf->m_data = data;
63 mbuf->m_len = len;
64
65 return 0;
66}
67
68errno_t mbuf_align_32(mbuf_t mbuf, size_t len)
69{
70 if ((mbuf->m_flags & M_EXT) != 0 && m_mclhasreference(mbuf))
71 return ENOTSUP;
72 mbuf->m_data = mbuf_datastart(mbuf);
73 mbuf->m_data += ((mbuf_trailingspace(mbuf) - len) &~ (sizeof(u_int32_t) - 1));
74
75 return 0;
76}
77
78addr64_t mbuf_data_to_physical(void* ptr)
79{
80 return (addr64_t)mcl_to_paddr(ptr);
81}
82
83errno_t mbuf_get(mbuf_how_t how, mbuf_type_t type, mbuf_t *mbuf)
84{
85 /* Must set *mbuf to NULL in failure case */
86 *mbuf = m_get(how, type);
87
88 return (*mbuf == NULL) ? ENOMEM : 0;
89}
90
91errno_t mbuf_gethdr(mbuf_how_t how, mbuf_type_t type, mbuf_t *mbuf)
92{
93 /* Must set *mbuf to NULL in failure case */
94 *mbuf = m_gethdr(how, type);
95
96 return (*mbuf == NULL) ? ENOMEM : 0;
97}
98
99extern struct mbuf * m_mbigget(struct mbuf *m, int nowait);
100
101errno_t mbuf_getcluster(mbuf_how_t how, mbuf_type_t type, size_t size, mbuf_t* mbuf)
102{
103 /* Must set *mbuf to NULL in failure case */
104 errno_t error = 0;
105 int created = 0;
106
107 if (mbuf == NULL)
108 return EINVAL;
109 if (*mbuf == NULL) {
110 *mbuf = m_get(how, type);
111 if (*mbuf == NULL)
112 return ENOMEM;
113 created = 1;
114 }
115 /*
116 * At the time this code was written, m_mclget and m_mbigget would always
117 * return the same value that was passed in to it.
118 */
119 if (size == MCLBYTES) {
120 *mbuf = m_mclget(*mbuf, how);
121 } else if (size == NBPG) {
122 *mbuf = m_mbigget(*mbuf, how);
123 } else {
124 error = EINVAL;
125 goto out;
126 }
127 if (*mbuf == NULL || ((*mbuf)->m_flags & M_EXT) == 0)
128 error = ENOMEM;
129out:
130 if (created && error != 0) {
131 error = ENOMEM;
132 mbuf_free(*mbuf);
133 *mbuf = NULL;
134 }
135 return error;
136}
137
138errno_t mbuf_mclget(mbuf_how_t how, mbuf_type_t type, mbuf_t *mbuf)
139{
140 /* Must set *mbuf to NULL in failure case */
141 errno_t error = 0;
142 int created = 0;
143 if (mbuf == NULL) return EINVAL;
144 if (*mbuf == NULL) {
145 error = mbuf_get(how, type, mbuf);
146 if (error)
147 return error;
148 created = 1;
149 }
150
151 /*
152 * At the time this code was written, m_mclget would always
153 * return the same value that was passed in to it.
154 */
155 *mbuf = m_mclget(*mbuf, how);
156
157 if (created && ((*mbuf)->m_flags & M_EXT) == 0) {
158 mbuf_free(*mbuf);
159 *mbuf = NULL;
160 }
161 if (*mbuf == NULL || ((*mbuf)->m_flags & M_EXT) == 0)
162 error = ENOMEM;
163 return error;
164}
165
166
167errno_t mbuf_getpacket(mbuf_how_t how, mbuf_t *mbuf)
168{
169 /* Must set *mbuf to NULL in failure case */
170 errno_t error = 0;
171
172 *mbuf = m_getpacket_how(how);
173
174 if (*mbuf == NULL) {
175 if (how == MBUF_WAITOK)
176 error = ENOMEM;
177 else
178 error = EWOULDBLOCK;
179 }
180
181 return error;
182}
183
184mbuf_t mbuf_free(mbuf_t mbuf)
185{
186 return m_free(mbuf);
187}
188
189void mbuf_freem(mbuf_t mbuf)
190{
191 m_freem(mbuf);
192}
193
194int mbuf_freem_list(mbuf_t mbuf)
195{
196 return m_freem_list(mbuf);
197}
198
199size_t mbuf_leadingspace(mbuf_t mbuf)
200{
201 return m_leadingspace(mbuf);
202}
203
204size_t mbuf_trailingspace(mbuf_t mbuf)
205{
206 return m_trailingspace(mbuf);
207}
208
209/* Manipulation */
210errno_t mbuf_copym(mbuf_t src, size_t offset, size_t len,
211 mbuf_how_t how, mbuf_t *new_mbuf)
212{
213 /* Must set *mbuf to NULL in failure case */
214 *new_mbuf = m_copym(src, offset, len, how);
215
216 return (*new_mbuf == NULL) ? ENOMEM : 0;
217}
218
219errno_t mbuf_dup(mbuf_t src, mbuf_how_t how, mbuf_t *new_mbuf)
220{
221 /* Must set *new_mbuf to NULL in failure case */
222 *new_mbuf = m_dup(src, how);
223
224 return (*new_mbuf == NULL) ? ENOMEM : 0;
225}
226
227errno_t mbuf_prepend(mbuf_t *orig, size_t len, mbuf_how_t how)
228{
229 /* Must set *orig to NULL in failure case */
230 *orig = m_prepend_2(*orig, len, how);
231
232 return (*orig == NULL) ? ENOMEM : 0;
233}
234
235errno_t mbuf_split(mbuf_t src, size_t offset,
236 mbuf_how_t how, mbuf_t *new_mbuf)
237{
238 /* Must set *new_mbuf to NULL in failure case */
239 *new_mbuf = m_split(src, offset, how);
240
241 return (*new_mbuf == NULL) ? ENOMEM : 0;
242}
243
244errno_t mbuf_pullup(mbuf_t *mbuf, size_t len)
245{
246 /* Must set *mbuf to NULL in failure case */
247 *mbuf = m_pullup(*mbuf, len);
248
249 return (*mbuf == NULL) ? ENOMEM : 0;
250}
251
252errno_t mbuf_pulldown(mbuf_t src, size_t *offset, size_t len, mbuf_t *location)
253{
254 /* Must set *location to NULL in failure case */
255 int new_offset;
256 *location = m_pulldown(src, *offset, len, &new_offset);
257 *offset = new_offset;
258
259 return (*location == NULL) ? ENOMEM : 0;
260}
261
262void mbuf_adj(mbuf_t mbuf, int len)
263{
264 m_adj(mbuf, len);
265}
266
267errno_t mbuf_copydata(mbuf_t m, size_t off, size_t len, void* out_data)
268{
269 /* Copied m_copydata, added error handling (don't just panic) */
270 int count;
271
272 while (off > 0) {
273 if (m == 0)
274 return EINVAL;
275 if (off < (size_t)m->m_len)
276 break;
277 off -= m->m_len;
278 m = m->m_next;
279 }
280 while (len > 0) {
281 if (m == 0)
282 return EINVAL;
283 count = m->m_len - off > len ? len : m->m_len - off;
284 bcopy(mtod(m, caddr_t) + off, out_data, count);
285 len -= count;
286 out_data = ((char*)out_data) + count;
287 off = 0;
288 m = m->m_next;
289 }
290
291 return 0;
292}
293
294int mbuf_mclref(mbuf_t mbuf)
295{
296 return m_mclref(mbuf);
297}
298
299int mbuf_mclunref(mbuf_t mbuf)
300{
301 return m_mclunref(mbuf);
302}
303
304int mbuf_mclhasreference(mbuf_t mbuf)
305{
306 if ((mbuf->m_flags & M_EXT))
307 return m_mclhasreference(mbuf);
308 else
309 return 0;
310}
311
312
313/* mbuf header */
314mbuf_t mbuf_next(mbuf_t mbuf)
315{
316 return mbuf->m_next;
317}
318
319errno_t mbuf_setnext(mbuf_t mbuf, mbuf_t next)
320{
321 if (next && ((next)->m_nextpkt != NULL ||
322 (next)->m_type == MT_FREE)) return EINVAL;
323 mbuf->m_next = next;
324
325 return 0;
326}
327
328mbuf_t mbuf_nextpkt(mbuf_t mbuf)
329{
330 return mbuf->m_nextpkt;
331}
332
333void mbuf_setnextpkt(mbuf_t mbuf, mbuf_t nextpkt)
334{
335 mbuf->m_nextpkt = nextpkt;
336}
337
338size_t mbuf_len(mbuf_t mbuf)
339{
340 return mbuf->m_len;
341}
342
343void mbuf_setlen(mbuf_t mbuf, size_t len)
344{
345 mbuf->m_len = len;
346}
347
348size_t mbuf_maxlen(mbuf_t mbuf)
349{
350 if (mbuf->m_flags & M_EXT)
351 return mbuf->m_ext.ext_size;
352 return &mbuf->m_dat[MLEN] - ((char*)mbuf_datastart(mbuf));
353}
354
355mbuf_type_t mbuf_type(mbuf_t mbuf)
356{
357 return mbuf->m_type;
358}
359
360errno_t mbuf_settype(mbuf_t mbuf, mbuf_type_t new_type)
361{
362 if (new_type == MBUF_TYPE_FREE) return EINVAL;
363
364 m_mchtype(mbuf, new_type);
365
366 return 0;
367}
368
369mbuf_flags_t mbuf_flags(mbuf_t mbuf)
370{
371 return mbuf->m_flags & mbuf_flags_mask;
372}
373
374errno_t mbuf_setflags(mbuf_t mbuf, mbuf_flags_t flags)
375{
376 if ((flags & ~mbuf_flags_mask) != 0) return EINVAL;
377 mbuf->m_flags = flags |
378 (mbuf->m_flags & ~mbuf_flags_mask);
379
380 return 0;
381}
382
383errno_t mbuf_setflags_mask(mbuf_t mbuf, mbuf_flags_t flags, mbuf_flags_t mask)
384{
385 if (((flags | mask) & ~mbuf_flags_mask) != 0) return EINVAL;
386
387 mbuf->m_flags = (flags & mask) | (mbuf->m_flags & ~mask);
388
389 return 0;
390}
391
392errno_t mbuf_copy_pkthdr(mbuf_t dest, mbuf_t src)
393{
394 if (((src)->m_flags & M_PKTHDR) == 0)
395 return EINVAL;
396
397 m_copy_pkthdr(dest, src);
398
399 return 0;
400}
401
402size_t mbuf_pkthdr_len(mbuf_t mbuf)
403{
404 return mbuf->m_pkthdr.len;
405}
406
407void mbuf_pkthdr_setlen(mbuf_t mbuf, size_t len)
408{
409 mbuf->m_pkthdr.len = len;
410}
411
412ifnet_t mbuf_pkthdr_rcvif(mbuf_t mbuf)
413{
414 // If we reference count ifnets, we should take a reference here before returning
415 return mbuf->m_pkthdr.rcvif;
416}
417
418errno_t mbuf_pkthdr_setrcvif(mbuf_t mbuf, ifnet_t ifnet)
419{
420 /* May want to walk ifnet list to determine if interface is valid */
421 mbuf->m_pkthdr.rcvif = (struct ifnet*)ifnet;
422 return 0;
423}
424
425void* mbuf_pkthdr_header(mbuf_t mbuf)
426{
427 return mbuf->m_pkthdr.header;
428}
429
430void mbuf_pkthdr_setheader(mbuf_t mbuf, void *header)
431{
432 mbuf->m_pkthdr.header = (void*)header;
433}
434
435/* mbuf aux data */
436errno_t mbuf_aux_add(mbuf_t mbuf, int family, mbuf_type_t type, mbuf_t *aux_mbuf)
437{
438 *aux_mbuf = m_aux_add(mbuf, family, type);
439 return (*aux_mbuf == NULL) ? ENOMEM : 0;
440}
441
442mbuf_t mbuf_aux_find(mbuf_t mbuf, int family, mbuf_type_t type)
443{
444 return m_aux_find(mbuf, family, type);
445}
446
447void mbuf_aux_delete(mbuf_t mbuf, mbuf_t aux)
448{
449 m_aux_delete(mbuf, aux);
450}
451
452void
453mbuf_inbound_modified(mbuf_t mbuf)
454{
455 /* Invalidate hardware generated checksum flags */
456 mbuf->m_pkthdr.csum_flags = 0;
457}
458
459extern void in_cksum_offset(struct mbuf* m, size_t ip_offset);
460extern void in_delayed_cksum_offset(struct mbuf *m, int ip_offset);
461
462void
463mbuf_outbound_finalize(mbuf_t mbuf, u_long protocol_family, size_t protocol_offset)
464{
465 if ((mbuf->m_pkthdr.csum_flags &
466 (CSUM_DELAY_DATA | CSUM_DELAY_IP | CSUM_TCP_SUM16)) == 0)
467 return;
468
469 /* Generate the packet in software, client needs it */
470 switch (protocol_family) {
471 case PF_INET:
472 if (mbuf->m_pkthdr.csum_flags & CSUM_TCP_SUM16) {
473 /*
474 * If you're wondering where this lovely code comes
475 * from, we're trying to undo what happens in ip_output.
476 * Look for CSUM_TCP_SUM16 in ip_output.
477 */
478 u_int16_t first, second;
479 mbuf->m_pkthdr.csum_flags &= ~CSUM_TCP_SUM16;
480 mbuf->m_pkthdr.csum_flags |= CSUM_TCP;
481 first = mbuf->m_pkthdr.csum_data >> 16;
482 second = mbuf->m_pkthdr.csum_data & 0xffff;
483 mbuf->m_pkthdr.csum_data = first - second;
484 }
485 if (mbuf->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
486 in_delayed_cksum_offset(mbuf, protocol_offset);
487 }
488
489 if (mbuf->m_pkthdr.csum_flags & CSUM_DELAY_IP) {
490 in_cksum_offset(mbuf, protocol_offset);
491 }
492
493 mbuf->m_pkthdr.csum_flags &= ~(CSUM_DELAY_DATA | CSUM_DELAY_IP);
494 break;
495
496 default:
497 /*
498 * Not sure what to do here if anything.
499 * Hardware checksum code looked pretty IPv4 specific.
500 */
501 if ((mbuf->m_pkthdr.csum_flags & (CSUM_DELAY_DATA | CSUM_DELAY_IP)) != 0)
502 panic("mbuf_outbound_finalize - CSUM flags set for non-IPv4 packet (%d)!\n", protocol_family);
503 }
504}
505
506errno_t
507mbuf_set_vlan_tag(
508 mbuf_t mbuf,
509 u_int16_t vlan)
510{
511 mbuf->m_pkthdr.csum_flags |= CSUM_VLAN_TAG_VALID;
512 mbuf->m_pkthdr.vlan_tag = vlan;
513
514 return 0;
515}
516
517errno_t
518mbuf_get_vlan_tag(
519 mbuf_t mbuf,
520 u_int16_t *vlan)
521{
522 if ((mbuf->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0)
523 return ENXIO; // No vlan tag set
524
525 *vlan = mbuf->m_pkthdr.vlan_tag;
526
527 return 0;
528}
529
530errno_t
531mbuf_clear_vlan_tag(
532 mbuf_t mbuf)
533{
534 mbuf->m_pkthdr.csum_flags &= ~CSUM_VLAN_TAG_VALID;
535 mbuf->m_pkthdr.vlan_tag = 0;
536
537 return 0;
538}
539
540static const mbuf_csum_request_flags_t mbuf_valid_csum_request_flags =
541 MBUF_CSUM_REQ_IP | MBUF_CSUM_REQ_TCP | MBUF_CSUM_REQ_UDP | MBUF_CSUM_REQ_SUM16;
542
543errno_t
544mbuf_set_csum_requested(
545 mbuf_t mbuf,
546 mbuf_csum_request_flags_t request,
547 u_int32_t value)
548{
549 request &= mbuf_valid_csum_request_flags;
550 mbuf->m_pkthdr.csum_flags = (mbuf->m_pkthdr.csum_flags & 0xffff0000) | request;
551 mbuf->m_pkthdr.csum_data = value;
552
553 return 0;
554}
555
556errno_t
557mbuf_get_csum_requested(
558 mbuf_t mbuf,
559 mbuf_csum_request_flags_t *request,
560 u_int32_t *value)
561{
562 *request = mbuf->m_pkthdr.csum_flags;
563 *request &= mbuf_valid_csum_request_flags;
564 if (value != NULL) {
565 *value = mbuf->m_pkthdr.csum_data;
566 }
567
568 return 0;
569}
570
571errno_t
572mbuf_clear_csum_requested(
573 mbuf_t mbuf)
574{
575 mbuf->m_pkthdr.csum_flags &= 0xffff0000;
576 mbuf->m_pkthdr.csum_data = 0;
577
578 return 0;
579}
580
581static const mbuf_csum_performed_flags_t mbuf_valid_csum_performed_flags =
582 MBUF_CSUM_DID_IP | MBUF_CSUM_IP_GOOD | MBUF_CSUM_DID_DATA |
583 MBUF_CSUM_PSEUDO_HDR | MBUF_CSUM_TCP_SUM16;
584
585errno_t
586mbuf_set_csum_performed(
587 mbuf_t mbuf,
588 mbuf_csum_performed_flags_t performed,
589 u_int32_t value)
590{
591 performed &= mbuf_valid_csum_performed_flags;
592 mbuf->m_pkthdr.csum_flags = (mbuf->m_pkthdr.csum_flags & 0xffff0000) | performed;
593 mbuf->m_pkthdr.csum_data = value;
594
595 return 0;
596}
597
598errno_t
599mbuf_get_csum_performed(
600 mbuf_t mbuf,
601 mbuf_csum_performed_flags_t *performed,
602 u_int32_t *value)
603{
604 *performed = mbuf->m_pkthdr.csum_flags & mbuf_valid_csum_performed_flags;
605 *value = mbuf->m_pkthdr.csum_data;
606
607 return 0;
608}
609
610errno_t
611mbuf_clear_csum_performed(
612 mbuf_t mbuf)
613{
614 mbuf->m_pkthdr.csum_flags &= 0xffff0000;
615 mbuf->m_pkthdr.csum_data = 0;
616
617 return 0;
618}
619
620/*
621 * Mbuf tag KPIs
622 */
623
624struct mbuf_tag_id_entry {
625 SLIST_ENTRY(mbuf_tag_id_entry) next;
626 mbuf_tag_id_t id;
627 char string[];
628};
629
630#define MBUF_TAG_ID_ENTRY_SIZE(__str) \
631 ((size_t)&(((struct mbuf_tag_id_entry*)0)->string[0]) + \
632 strlen(__str) + 1)
633
634#define MTAG_FIRST_ID 1000
635static u_long mtag_id_next = MTAG_FIRST_ID;
636static SLIST_HEAD(,mbuf_tag_id_entry) mtag_id_list = {NULL};
637static lck_mtx_t *mtag_id_lock = NULL;
638
639__private_extern__ void
640mbuf_tag_id_first_last(
641 u_long *first,
642 u_long *last)
643{
644 *first = MTAG_FIRST_ID;
645 *last = mtag_id_next - 1;
646}
647
648__private_extern__ errno_t
649mbuf_tag_id_find_internal(
650 const char *string,
651 u_long *out_id,
652 int create)
653{
654 struct mbuf_tag_id_entry *entry = NULL;
655
656
657 *out_id = 0;
658
659 if (string == NULL || out_id == NULL) {
660 return EINVAL;
661 }
662
663 /* Don't bother allocating the lock if we're only doing a lookup */
664 if (create == 0 && mtag_id_lock == NULL)
665 return ENOENT;
666
667 /* Allocate lock if necessary */
668 if (mtag_id_lock == NULL) {
669 lck_grp_attr_t *grp_attrib = NULL;
670 lck_attr_t *lck_attrb = NULL;
671 lck_grp_t *lck_group = NULL;
672 lck_mtx_t *new_lock = NULL;
673
674 grp_attrib = lck_grp_attr_alloc_init();
675 lck_group = lck_grp_alloc_init("mbuf_tag_allocate_id", grp_attrib);
676 lck_grp_attr_free(grp_attrib);
677 lck_attrb = lck_attr_alloc_init();
678
679 new_lock = lck_mtx_alloc_init(lck_group, lck_attrb);
680 if (!OSCompareAndSwap((UInt32)0, (UInt32)new_lock, (UInt32*)&mtag_id_lock)) {
681 /*
682 * If the atomic swap fails, someone else has already
683 * done this work. We can free the stuff we allocated.
684 */
685 lck_mtx_free(new_lock, lck_group);
686 lck_grp_free(lck_group);
687 }
688 lck_attr_free(lck_attrb);
689 }
690
691 /* Look for an existing entry */
692 lck_mtx_lock(mtag_id_lock);
693 SLIST_FOREACH(entry, &mtag_id_list, next) {
694 if (strcmp(string, entry->string) == 0) {
695 break;
696 }
697 }
698
699 if (entry == NULL) {
700 if (create == 0) {
701 lck_mtx_unlock(mtag_id_lock);
702 return ENOENT;
703 }
704
705 entry = kalloc(MBUF_TAG_ID_ENTRY_SIZE(string));
706 if (entry == NULL) {
707 lck_mtx_unlock(mtag_id_lock);
708 return ENOMEM;
709 }
710
711 strcpy(entry->string, string);
712 entry->id = mtag_id_next;
713 mtag_id_next++;
714 SLIST_INSERT_HEAD(&mtag_id_list, entry, next);
715 }
716 lck_mtx_unlock(mtag_id_lock);
717
718 *out_id = entry->id;
719
720 return 0;
721}
722
723errno_t
724mbuf_tag_id_find(
725 const char *string,
726 mbuf_tag_id_t *out_id)
727{
728 return mbuf_tag_id_find_internal(string, (u_long*)out_id, 1);
729}
730
731errno_t
732mbuf_tag_allocate(
733 mbuf_t mbuf,
734 mbuf_tag_id_t id,
735 mbuf_tag_type_t type,
736 size_t length,
737 mbuf_how_t how,
738 void** data_p)
739{
740 struct m_tag *tag;
741
742 if (data_p != NULL)
743 *data_p = NULL;
744
745 /* Sanity check parameters */
746 if (mbuf == NULL || (mbuf->m_flags & M_PKTHDR) == 0 || id < MTAG_FIRST_ID ||
747 id >= mtag_id_next || length < 1 || (length & 0xffff0000) != 0 ||
748 data_p == NULL) {
749 return EINVAL;
750 }
751
752 /* Make sure this mtag hasn't already been allocated */
753 tag = m_tag_locate(mbuf, id, type, NULL);
754 if (tag != NULL) {
755 return EEXIST;
756 }
757
758 /* Allocate an mtag */
759 tag = m_tag_alloc(id, type, length, how);
760 if (tag == NULL) {
761 return how == M_WAITOK ? ENOMEM : EWOULDBLOCK;
762 }
763
764 /* Attach the mtag and set *data_p */
765 m_tag_prepend(mbuf, tag);
766 *data_p = tag + 1;
767
768 return 0;
769}
770
771errno_t
772mbuf_tag_find(
773 mbuf_t mbuf,
774 mbuf_tag_id_t id,
775 mbuf_tag_type_t type,
776 size_t* length,
777 void** data_p)
778{
779 struct m_tag *tag;
780
781 if (length != NULL)
782 *length = 0;
783 if (data_p != NULL)
784 *data_p = NULL;
785
786 /* Sanity check parameters */
787 if (mbuf == NULL || (mbuf->m_flags & M_PKTHDR) == 0 || id < MTAG_FIRST_ID ||
788 id >= mtag_id_next || length == NULL || data_p == NULL) {
789 return EINVAL;
790 }
791
792 /* Locate an mtag */
793 tag = m_tag_locate(mbuf, id, type, NULL);
794 if (tag == NULL) {
795 return ENOENT;
796 }
797
798 /* Copy out the pointer to the data and the lenght value */
799 *length = tag->m_tag_len;
800 *data_p = tag + 1;
801
802 return 0;
803}
804
805void
806mbuf_tag_free(
807 mbuf_t mbuf,
808 mbuf_tag_id_t id,
809 mbuf_tag_type_t type)
810{
811 struct m_tag *tag;
812
813 if (mbuf == NULL || (mbuf->m_flags & M_PKTHDR) == 0 || id < MTAG_FIRST_ID ||
814 id >= mtag_id_next)
815 return;
816
817 tag = m_tag_locate(mbuf, id, type, NULL);
818 if (tag == NULL) {
819 return;
820 }
821
822 m_tag_delete(mbuf, tag);
823 return;
824}
825
826/* mbuf stats */
827void mbuf_stats(struct mbuf_stat *stats)
828{
829 stats->mbufs = mbstat.m_mbufs;
830 stats->clusters = mbstat.m_clusters;
831 stats->clfree = mbstat.m_clfree;
832 stats->drops = mbstat.m_drops;
833 stats->wait = mbstat.m_wait;
834 stats->drain = mbstat.m_drain;
835 __builtin_memcpy(stats->mtypes, mbstat.m_mtypes, sizeof(stats->mtypes));
836 stats->mcfail = mbstat.m_mcfail;
837 stats->mpfail = mbstat.m_mpfail;
838 stats->msize = mbstat.m_msize;
839 stats->mclbytes = mbstat.m_mclbytes;
840 stats->minclsize = mbstat.m_minclsize;
841 stats->mlen = mbstat.m_mlen;
842 stats->mhlen = mbstat.m_mhlen;
843 stats->bigclusters = mbstat.m_bigclusters;
844 stats->bigclfree = mbstat.m_bigclfree;
845 stats->bigmclbytes = mbstat.m_bigmclbytes;
846}
847
848errno_t
849mbuf_allocpacket(mbuf_how_t how, size_t packetlen, unsigned int *maxchunks, mbuf_t *mbuf)
850{
851 errno_t error;
852 struct mbuf *m;
853 unsigned int numpkts = 1;
854 unsigned int numchunks = maxchunks ? *maxchunks : 0;
855
856 if (packetlen == 0) {
857 error = EINVAL;
858 goto out;
859 }
860 m = m_allocpacket_internal(&numpkts, packetlen, maxchunks ? &numchunks : NULL, how, 1, 0);
861 if (m == 0) {
862 if (maxchunks && *maxchunks && numchunks > *maxchunks)
863 error = ENOBUFS;
864 else
865 error = ENOMEM;
866 } else {
867 error = 0;
868 *mbuf = m;
869 }
870out:
871 return error;
872}
873
874
875/*
876 * mbuf_copyback differs from m_copyback in a few ways:
877 * 1) mbuf_copyback will allocate clusters for new mbufs we append
878 * 2) mbuf_copyback will grow the last mbuf in the chain if possible
879 * 3) mbuf_copyback reports whether or not the operation succeeded
880 * 4) mbuf_copyback allows the caller to specify M_WAITOK or M_NOWAIT
881 */
882errno_t
883mbuf_copyback(
884 mbuf_t m,
885 size_t off,
886 size_t len,
887 const void *data,
888 mbuf_how_t how)
889{
890 size_t mlen;
891 mbuf_t m_start = m;
892 mbuf_t n;
893 int totlen = 0;
894 errno_t result = 0;
895 const char *cp = data;
896
897 if (m == NULL || len == 0 || data == NULL)
898 return EINVAL;
899
900 while (off > (mlen = m->m_len)) {
901 off -= mlen;
902 totlen += mlen;
903 if (m->m_next == 0) {
904 n = m_getclr(how, m->m_type);
905 if (n == 0) {
906 result = ENOBUFS;
907 goto out;
908 }
909 n->m_len = MIN(MLEN, len + off);
910 m->m_next = n;
911 }
912 m = m->m_next;
913 }
914
915 while (len > 0) {
916 mlen = MIN(m->m_len - off, len);
917 if (mlen < len && m->m_next == NULL && mbuf_trailingspace(m) > 0) {
918 size_t grow = MIN(mbuf_trailingspace(m), len - mlen);
919 mlen += grow;
920 m->m_len += grow;
921 }
922 bcopy(cp, off + (char*)mbuf_data(m), (unsigned)mlen);
923 cp += mlen;
924 len -= mlen;
925 mlen += off;
926 off = 0;
927 totlen += mlen;
928 if (len == 0)
929 break;
930 if (m->m_next == 0) {
931 n = m_get(how, m->m_type);
932 if (n == NULL) {
933 result = ENOBUFS;
934 goto out;
935 }
936 if (len > MINCLSIZE) {
937 /* cluter allocation failure is okay, we can grow chain */
938 mbuf_mclget(how, m->m_type, &n);
939 }
940 n->m_len = MIN(mbuf_maxlen(n), len);
941 m->m_next = n;
942 }
943 m = m->m_next;
944 }
945
946out:
947 if ((m_start->m_flags & M_PKTHDR) && (m_start->m_pkthdr.len < totlen))
948 m_start->m_pkthdr.len = totlen;
949
950 return result;
951}