| 6 |
| * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of the University nor the names of its contributors |
| 15 | * may be used to endorse or promote products derived from this software |
| 16 | * without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | * |
| 30 | */ |
| 31 |
| 32 | /* |
| 33 | * __Userspace__ version of /usr/src/sys/kern/kern_mbuf.c |
| 34 | * We are initializing two zones for Mbufs and Clusters. |
| 35 | * |
| 36 | */ |
| 37 |
| 38 | #include <stdio.h> |
| 39 | #include <string.h> |
| 40 | /* #include <sys/param.h> This defines MSIZE 256 */ |
| 41 | #if !defined(SCTP_SIMPLE_ALLOCATOR) |
| 42 | #include "umem.h" |
| 43 | #endif |
| 44 | #include "user_mbuf.h" |
| 45 | #include "user_environment.h" |
| 46 | #include "user_atomic.h" |
| 47 | #include "netinet/sctp_pcb.h" |
| 48 |
| 49 | #define KIPC_MAX_LINKHDR 4 /* int: max length of link header (see sys/sysclt.h) */ |
| 50 | #define KIPC_MAX_PROTOHDR 5 /* int: max length of network header (see sys/sysclt.h)*/ |
| 51 | int max_linkhdr = KIPC_MAX_LINKHDR; |
| 52 | int max_protohdr = KIPC_MAX_PROTOHDR; /* Size of largest protocol layer header. */ |
| 53 |
| 54 | /* |
| 55 | * Zones from which we allocate. |
| 56 | */ |
| 57 | sctp_zone_t zone_mbuf; |
| 58 | sctp_zone_t zone_clust; |
| 59 | sctp_zone_t zone_ext_refcnt; |
| 60 |
| 61 | /* __Userspace__ clust_mb_args will be passed as callback data to mb_ctor_clust |
| 62 | * and mb_dtor_clust. |
| 63 | * Note: I had to use struct clust_args as an encapsulation for an mbuf pointer. |
| 64 | * struct mbuf * clust_mb_args; does not work. |
| 65 | */ |
| 66 | struct clust_args clust_mb_args; |
| 67 |
| 68 |
| 69 | /* __Userspace__ |
| 70 | * Local prototypes. |
| 71 | */ |
| 72 | static int mb_ctor_mbuf(void *, void *, int); |
| 73 | static int mb_ctor_clust(void *, void *, int); |
| 74 | static void mb_dtor_mbuf(void *, void *); |
| 75 | static void mb_dtor_clust(void *, void *); |
| 76 |
| 77 |
| 78 | /***************** Functions taken from user_mbuf.h *************/ |
| 79 |
| 80 | static int mbuf_constructor_dup(struct mbuf *m, int pkthdr, short type) |
| 81 | { |
| 82 | int flags = pkthdr; |
| 83 |
| 84 | m->m_next = NULL; |
| 85 | m->m_nextpkt = NULL; |
| 86 | m->m_len = 0; |
| 87 | m->m_flags = flags; |
| 88 | m->m_type = type; |
| 89 | if (flags & M_PKTHDR) { |
| 90 | m->m_data = m->m_pktdat; |
| 91 | m->m_pkthdr.rcvif = NULL; |
| 92 | m->m_pkthdr.len = 0; |
| 93 | m->m_pkthdr.header = NULL; |
| 94 | m->m_pkthdr.csum_flags = 0; |
| 95 | m->m_pkthdr.csum_data = 0; |
| 96 | m->m_pkthdr.tso_segsz = 0; |
| 97 | m->m_pkthdr.ether_vtag = 0; |
| 98 | SLIST_INIT(&m->m_pkthdr.tags); |
| 99 | } else |
| 100 | m->m_data = m->m_dat; |
| 101 |
| 102 | return (0); |
| 103 | } |
| 104 |
| 105 | /* __Userspace__ */ |
| 106 | struct mbuf * |
| 107 | m_get(int how, short type) |
| 108 | { |
| 109 | struct mbuf *mret; |
| 110 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 111 | struct mb_args mbuf_mb_args; |
| 112 |
| 113 | /* The following setter function is not yet being enclosed within |
| 114 | * #if USING_MBUF_CONSTRUCTOR - #endif, until I have thoroughly tested |
| 115 | * mb_dtor_mbuf. See comment there |
| 116 | */ |
| 117 | mbuf_mb_args.flags = 0; |
| 118 | mbuf_mb_args.type = type; |
| 119 | #endif |
| 120 | /* Mbuf master zone, zone_mbuf, has already been |
| 121 | * created in mbuf_initialize() */ |
| 122 | mret = SCTP_ZONE_GET(zone_mbuf, struct mbuf); |
| 123 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 124 | mb_ctor_mbuf(mret, &mbuf_mb_args, 0); |
| 125 | #endif |
| 126 | /*mret = ((struct mbuf *)umem_cache_alloc(zone_mbuf, UMEM_DEFAULT));*/ |
| 127 |
| 128 | /* There are cases when an object available in the current CPU's |
| 129 | * loaded magazine and in those cases the object's constructor is not applied. |
| 130 | * If that is the case, then we are duplicating constructor initialization here, |
| 131 | * so that the mbuf is properly constructed before returning it. |
| 132 | */ |
| 133 | if (mret) { |
| 134 | #if USING_MBUF_CONSTRUCTOR |
| 135 | if (! (mret->m_type == type) ) { |
| 136 | mbuf_constructor_dup(mret, 0, type); |
| 137 | } |
| 138 | #else |
| 139 | mbuf_constructor_dup(mret, 0, type); |
| 140 | #endif |
| 141 |
| 142 | } |
| 143 | return mret; |
| 144 | } |
| 145 |
| 146 |
| 147 | /* __Userspace__ */ |
| 148 | struct mbuf * |
| 149 | m_gethdr(int how, short type) |
| 150 | { |
| 151 | struct mbuf *mret; |
| 152 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 153 | struct mb_args mbuf_mb_args; |
| 154 |
| 155 | /* The following setter function is not yet being enclosed within |
| 156 | * #if USING_MBUF_CONSTRUCTOR - #endif, until I have thoroughly tested |
| 157 | * mb_dtor_mbuf. See comment there |
| 158 | */ |
| 159 | mbuf_mb_args.flags = M_PKTHDR; |
| 160 | mbuf_mb_args.type = type; |
| 161 | #endif |
| 162 | mret = SCTP_ZONE_GET(zone_mbuf, struct mbuf); |
| 163 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 164 | mb_ctor_mbuf(mret, &mbuf_mb_args, 0); |
| 165 | #endif |
| 166 | /*mret = ((struct mbuf *)umem_cache_alloc(zone_mbuf, UMEM_DEFAULT));*/ |
| 167 | /* There are cases when an object available in the current CPU's |
| 168 | * loaded magazine and in those cases the object's constructor is not applied. |
| 169 | * If that is the case, then we are duplicating constructor initialization here, |
| 170 | * so that the mbuf is properly constructed before returning it. |
| 171 | */ |
| 172 | if (mret) { |
| 173 | #if USING_MBUF_CONSTRUCTOR |
| 174 | if (! ((mret->m_flags & M_PKTHDR) && (mret->m_type == type)) ) { |
| 175 | mbuf_constructor_dup(mret, M_PKTHDR, type); |
| 176 | } |
| 177 | #else |
| 178 | mbuf_constructor_dup(mret, M_PKTHDR, type); |
| 179 | #endif |
| 180 | } |
| 181 | return mret; |
| 182 | } |
| 183 |
| 184 | /* __Userspace__ */ |
| 185 | struct mbuf * |
| 186 | m_free(struct mbuf *m) |
| 187 | { |
| 188 |
| 189 | struct mbuf *n = m->m_next; |
| 190 |
| 191 | if (m->m_flags & M_EXT) |
| 192 | mb_free_ext(m); |
| 193 | else if ((m->m_flags & M_NOFREE) == 0) { |
| 194 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 195 | mb_dtor_mbuf(m, NULL); |
| 196 | #endif |
| 197 | SCTP_ZONE_FREE(zone_mbuf, m); |
| 198 | } |
| 199 | /*umem_cache_free(zone_mbuf, m);*/ |
| 200 | return (n); |
| 201 | } |
| 202 |
| 203 |
| 204 | static void |
| 205 | clust_constructor_dup(caddr_t m_clust, struct mbuf* m) |
| 206 | { |
| 207 | u_int *refcnt; |
| 208 | int type, size; |
| 209 |
| 210 | if (m == NULL) { |
| 211 | return; |
| 212 | } |
| 213 | /* Assigning cluster of MCLBYTES. TODO: Add jumbo frame functionality */ |
| 214 | type = EXT_CLUSTER; |
| 215 | size = MCLBYTES; |
| 216 |
| 217 | refcnt = SCTP_ZONE_GET(zone_ext_refcnt, u_int); |
| 218 | /*refcnt = (u_int *)umem_cache_alloc(zone_ext_refcnt, UMEM_DEFAULT);*/ |
| 219 | #if !defined(SCTP_SIMPLE_ALLOCATOR) |
| 220 | if (refcnt == NULL) { |
| 221 | umem_reap(); |
| 222 | refcnt = SCTP_ZONE_GET(zone_ext_refcnt, u_int); |
| 223 | /*refcnt = (u_int *)umem_cache_alloc(zone_ext_refcnt, UMEM_DEFAULT);*/ |
| 224 | } |
| 225 | #endif |
| 226 | *refcnt = 1; |
| 227 | m->m_ext.ext_buf = (caddr_t)m_clust; |
| 228 | m->m_data = m->m_ext.ext_buf; |
| 229 | m->m_flags |= M_EXT; |
| 230 | m->m_ext.ext_free = NULL; |
| 231 | m->m_ext.ext_args = NULL; |
| 232 | m->m_ext.ext_size = size; |
| 233 | m->m_ext.ext_type = type; |
| 234 | m->m_ext.ref_cnt = refcnt; |
| 235 | return; |
| 236 | } |
| 237 |
| 238 |
| 239 | /* __Userspace__ */ |
| 240 | void |
| 241 | m_clget(struct mbuf *m, int how) |
| 242 | { |
| 243 | caddr_t mclust_ret; |
| 244 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 245 | struct clust_args clust_mb_args_l; |
| 246 | #endif |
| 247 | if (m->m_flags & M_EXT) { |
| 248 | SCTPDBG(SCTP_DEBUG_USR, "%s: %p mbuf already has cluster\n", __func__, (void *)m); |
| 249 | } |
| 250 | m->m_ext.ext_buf = (char *)NULL; |
| 251 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 252 | clust_mb_args_l.parent_mbuf = m; |
| 253 | #endif |
| 254 | mclust_ret = SCTP_ZONE_GET(zone_clust, char); |
| 255 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 256 | mb_ctor_clust(mclust_ret, &clust_mb_args_l, 0); |
| 257 | #endif |
| 258 | /*mclust_ret = umem_cache_alloc(zone_clust, UMEM_DEFAULT);*/ |
| 259 | /* |
| 260 | On a cluster allocation failure, call umem_reap() and retry. |
| 261 | */ |
| 262 |
| 263 | if (mclust_ret == NULL) { |
| 264 | #if !defined(SCTP_SIMPLE_ALLOCATOR) |
| 265 | /* mclust_ret = SCTP_ZONE_GET(zone_clust, char); |
| 266 | mb_ctor_clust(mclust_ret, &clust_mb_args, 0); |
| 267 | #else*/ |
| 268 | umem_reap(); |
| 269 | mclust_ret = SCTP_ZONE_GET(zone_clust, char); |
| 270 | #endif |
| 271 | /*mclust_ret = umem_cache_alloc(zone_clust, UMEM_DEFAULT);*/ |
| 272 | /* if (NULL == mclust_ret) { */ |
| 273 | SCTPDBG(SCTP_DEBUG_USR, "Memory allocation failure in %s\n", __func__); |
| 274 | /* } */ |
| 275 | } |
| 276 |
| 277 | #if USING_MBUF_CONSTRUCTOR |
| 278 | if ((m->m_ext.ext_buf == NULL)) { |
| 279 | clust_constructor_dup(mclust_ret, m); |
| 280 | } |
| 281 | #else |
| 282 | clust_constructor_dup(mclust_ret, m); |
| 283 | #endif |
| 284 | } |
| 285 |
| 286 | struct mbuf * |
| 287 | m_getm2(struct mbuf *m, int len, int how, short type, int flags, int allonebuf) |
| 288 | { |
| 289 | struct mbuf *mb, *nm = NULL, *mtail = NULL; |
| 290 | int size, mbuf_threshold, space_needed = len; |
| 291 |
| 292 | KASSERT(len >= 0, ("%s: len is < 0", __func__)); |
| 293 |
| 294 | /* Validate flags. */ |
| 295 | flags &= (M_PKTHDR | M_EOR); |
| 296 |
| 297 | /* Packet header mbuf must be first in chain. */ |
| 298 | if ((flags & M_PKTHDR) && m != NULL) { |
| 299 | flags &= ~M_PKTHDR; |
| 300 | } |
| 301 |
| 302 | if (allonebuf == 0) |
| 303 | mbuf_threshold = SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count); |
| 304 | else |
| 305 | mbuf_threshold = 1; |
| 306 |
| 307 | /* Loop and append maximum sized mbufs to the chain tail. */ |
| 308 | while (len > 0) { |
| 309 | if ((!allonebuf && len >= MCLBYTES) || (len > (int)(((mbuf_threshold - 1) * MLEN) + MHLEN))) { |
| 310 | mb = m_gethdr(how, type); |
| 311 | MCLGET(mb, how); |
| 312 | size = MCLBYTES; |
| 313 | /* SCTP_BUF_LEN(mb) = MCLBYTES; */ |
| 314 | } else if (flags & M_PKTHDR) { |
| 315 | mb = m_gethdr(how, type); |
| 316 | if (len < MHLEN) { |
| 317 | size = len; |
| 318 | } else { |
| 319 | size = MHLEN; |
| 320 | } |
| 321 | } else { |
| 322 | mb = m_get(how, type); |
| 323 | if (len < MLEN) { |
| 324 | size = len; |
| 325 | } else { |
| 326 | size = MLEN; |
| 327 | } |
| 328 | } |
| 329 |
| 330 | /* Fail the whole operation if one mbuf can't be allocated. */ |
| 331 | if (mb == NULL) { |
| 332 | if (nm != NULL) |
| 333 | m_freem(nm); |
| 334 | return (NULL); |
| 335 | } |
| 336 |
| 337 | if (allonebuf != 0 && size < space_needed) { |
| 338 | m_freem(mb); |
| 339 | return (NULL); |
| 340 | } |
| 341 |
| 342 | /* Book keeping. */ |
| 343 | len -= size; |
| 344 | if (mtail != NULL) |
| 345 | mtail->m_next = mb; |
| 346 | else |
| 347 | nm = mb; |
| 348 | mtail = mb; |
| 349 | flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */ |
| 350 | } |
| 351 | if (flags & M_EOR) { |
| 352 | mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */ |
| 353 | } |
| 354 |
| 355 | /* If mbuf was supplied, append new chain to the end of it. */ |
| 356 | if (m != NULL) { |
| 357 | for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); |
| 358 | mtail->m_next = nm; |
| 359 | mtail->m_flags &= ~M_EOR; |
| 360 | } else { |
| 361 | m = nm; |
| 362 | } |
| 363 |
| 364 | return (m); |
| 365 | } |
| 366 |
| 367 | /* |
| 368 | * Copy the contents of uio into a properly sized mbuf chain. |
| 369 | */ |
| 370 | struct mbuf * |
| 371 | m_uiotombuf(struct uio *uio, int how, int len, int align, int flags) |
| 372 | { |
| 373 | struct mbuf *m, *mb; |
| 374 | int error, length; |
| 375 | ssize_t total; |
| 376 | int progress = 0; |
| 377 |
| 378 | /* |
| 379 | * len can be zero or an arbitrary large value bound by |
| 380 | * the total data supplied by the uio. |
| 381 | */ |
| 382 | if (len > 0) |
| 383 | total = min(uio->uio_resid, len); |
| 384 | else |
| 385 | total = uio->uio_resid; |
| 386 | /* |
| 387 | * The smallest unit returned by m_getm2() is a single mbuf |
| 388 | * with pkthdr. We can't align past it. |
| 389 | */ |
| 390 | if (align >= MHLEN) |
| 391 | return (NULL); |
| 392 | /* |
| 393 | * Give us the full allocation or nothing. |
| 394 | * If len is zero return the smallest empty mbuf. |
| 395 | */ |
| 396 | m = m_getm2(NULL, (int)max(total + align, 1), how, MT_DATA, flags, 0); |
| 397 | if (m == NULL) |
| 398 | return (NULL); |
| 399 | m->m_data += align; |
| 400 |
| 401 | /* Fill all mbufs with uio data and update header information. */ |
| 402 | for (mb = m; mb != NULL; mb = mb->m_next) { |
| 403 | length = (int)min(M_TRAILINGSPACE(mb), total - progress); |
| 404 | error = uiomove(mtod(mb, void *), length, uio); |
| 405 | if (error) { |
| 406 | m_freem(m); |
| 407 | return (NULL); |
| 408 | } |
| 409 |
| 410 | mb->m_len = length; |
| 411 | progress += length; |
| 412 | if (flags & M_PKTHDR) |
| 413 | m->m_pkthdr.len += length; |
| 414 | } |
| 415 | KASSERT(progress == total, ("%s: progress != total", __func__)); |
| 416 |
| 417 | return (m); |
| 418 | } |
| 419 |
| 420 | u_int |
| 421 | m_length(struct mbuf *m0, struct mbuf **last) |
| 422 | { |
| 423 | struct mbuf *m; |
| 424 | u_int len; |
| 425 |
| 426 | len = 0; |
| 427 | for (m = m0; m != NULL; m = m->m_next) { |
| 428 | len += m->m_len; |
| 429 | if (m->m_next == NULL) |
| 430 | break; |
| 431 | } |
| 432 | if (last != NULL) |
| 433 | *last = m; |
| 434 | return (len); |
| 435 | } |
| 436 |
| 437 | struct mbuf * |
| 438 | m_last(struct mbuf *m) |
| 439 | { |
| 440 | while (m->m_next) { |
| 441 | m = m->m_next; |
| 442 | } |
| 443 | return (m); |
| 444 | } |
| 445 |
| 446 | /* |
| 447 | * Unlink a tag from the list of tags associated with an mbuf. |
| 448 | */ |
| 449 | static __inline void |
| 450 | m_tag_unlink(struct mbuf *m, struct m_tag *t) |
| 451 | { |
| 452 |
| 453 | SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); |
| 454 | } |
| 455 |
| 456 | /* |
| 457 | * Reclaim resources associated with a tag. |
| 458 | */ |
| 459 | static __inline void |
| 460 | m_tag_free(struct m_tag *t) |
| 461 | { |
| 462 |
| 463 | (*t->m_tag_free)(t); |
| 464 | } |
| 465 |
| 466 | /* |
| 467 | * Set up the contents of a tag. Note that this does not fill in the free |
| 468 | * method; the caller is expected to do that. |
| 469 | * |
| 470 | * XXX probably should be called m_tag_init, but that was already taken. |
| 471 | */ |
| 472 | static __inline void |
| 473 | m_tag_setup(struct m_tag *t, uint32_t cookie, int type, int len) |
| 474 | { |
| 475 |
| 476 | t->m_tag_id = type; |
| 477 | t->m_tag_len = len; |
| 478 | t->m_tag_cookie = cookie; |
| 479 | } |
| 480 |
| 481 | /************ End functions from user_mbuf.h ******************/ |
| 482 |
| 483 |
| 484 |
| 485 | /************ End functions to substitute umem_cache_alloc and umem_cache_free **************/ |
| 486 |
| 487 | void |
| 488 | mbuf_initialize(void *dummy) |
| 489 | { |
| 490 |
| 491 | /* |
| 492 | * __Userspace__Configure UMA zones for Mbufs and Clusters. |
| 493 | * (TODO: m_getcl() - using packet secondary zone). |
| 494 | * There is no provision for trash_init and trash_fini in umem. |
| 495 | * |
| 496 | */ |
| 497 | /* zone_mbuf = umem_cache_create(MBUF_MEM_NAME, MSIZE, 0, |
| 498 | mb_ctor_mbuf, mb_dtor_mbuf, NULL, |
| 499 | &mbuf_mb_args, |
| 500 | NULL, 0); |
| 501 | zone_mbuf = umem_cache_create(MBUF_MEM_NAME, MSIZE, 0, NULL, NULL, NULL, NULL, NULL, 0);*/ |
| 502 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 503 | SCTP_ZONE_INIT(zone_mbuf, MBUF_MEM_NAME, MSIZE, 0); |
| 504 | #else |
| 505 | zone_mbuf = umem_cache_create(MBUF_MEM_NAME, MSIZE, 0, |
| 506 | mb_ctor_mbuf, mb_dtor_mbuf, NULL, |
| 507 | NULL, |
| 508 | NULL, 0); |
| 509 | #endif |
| 510 | /*zone_ext_refcnt = umem_cache_create(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int), 0, |
| 511 | NULL, NULL, NULL, |
| 512 | NULL, |
| 513 | NULL, 0);*/ |
| 514 | SCTP_ZONE_INIT(zone_ext_refcnt, MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int), 0); |
| 515 |
| 516 | /*zone_clust = umem_cache_create(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0, |
| 517 | mb_ctor_clust, mb_dtor_clust, NULL, |
| 518 | &clust_mb_args, |
| 519 | NULL, 0); |
| 520 | zone_clust = umem_cache_create(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0, NULL, NULL, NULL, NULL, NULL,0);*/ |
| 521 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 522 | SCTP_ZONE_INIT(zone_clust, MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0); |
| 523 | #else |
| 524 | zone_clust = umem_cache_create(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0, |
| 525 | mb_ctor_clust, mb_dtor_clust, NULL, |
| 526 | &clust_mb_args, |
| 527 | NULL, 0); |
| 528 | #endif |
| 529 |
| 530 | /* uma_prealloc() goes here... */ |
| 531 |
| 532 | /* __Userspace__ Add umem_reap here for low memory situation? |
| 533 | * |
| 534 | */ |
| 535 |
| 536 | } |
| 537 |
| 538 |
| 539 |
| 540 | /* |
| 541 | * __Userspace__ |
| 542 | * |
| 543 | * Constructor for Mbuf master zone. We have a different constructor |
| 544 | * for allocating the cluster. |
| 545 | * |
| 546 | * The 'arg' pointer points to a mb_args structure which |
| 547 | * contains call-specific information required to support the |
| 548 | * mbuf allocation API. See user_mbuf.h. |
| 549 | * |
| 550 | * The flgs parameter below can be UMEM_DEFAULT or UMEM_NOFAIL depending on what |
| 551 | * was passed when umem_cache_alloc was called. |
| 552 | * TODO: Use UMEM_NOFAIL in umem_cache_alloc and also define a failure handler |
| 553 | * and call umem_nofail_callback(my_failure_handler) in the stack initialization routines |
| 554 | * The advantage of using UMEM_NOFAIL is that we don't have to check if umem_cache_alloc |
| 555 | * was successful or not. The failure handler would take care of it, if we use the UMEM_NOFAIL |
| 556 | * flag. |
| 557 | * |
| 558 | * NOTE Ref: http://docs.sun.com/app/docs/doc/819-2243/6n4i099p2?l=en&a=view&q=umem_zalloc) |
| 559 | * The umem_nofail_callback() function sets the **process-wide** UMEM_NOFAIL callback. |
| 560 | * It also mentions that umem_nofail_callback is Evolving. |
| 561 | * |
| 562 | */ |
| 563 | static int |
| 564 | mb_ctor_mbuf(void *mem, void *arg, int flgs) |
| 565 | { |
| 566 | #if USING_MBUF_CONSTRUCTOR |
| 567 | struct mbuf *m; |
| 568 | struct mb_args *args; |
| 569 |
| 570 | int flags; |
| 571 | short type; |
| 572 |
| 573 | m = (struct mbuf *)mem; |
| 574 | args = (struct mb_args *)arg; |
| 575 | flags = args->flags; |
| 576 | type = args->type; |
| 577 |
| 578 | m->m_next = NULL; |
| 579 | m->m_nextpkt = NULL; |
| 580 | m->m_len = 0; |
| 581 | m->m_flags = flags; |
| 582 | m->m_type = type; |
| 583 | if (flags & M_PKTHDR) { |
| 584 | m->m_data = m->m_pktdat; |
| 585 | m->m_pkthdr.rcvif = NULL; |
| 586 | m->m_pkthdr.len = 0; |
| 587 | m->m_pkthdr.header = NULL; |
| 588 | m->m_pkthdr.csum_flags = 0; |
| 589 | m->m_pkthdr.csum_data = 0; |
| 590 | m->m_pkthdr.tso_segsz = 0; |
| 591 | m->m_pkthdr.ether_vtag = 0; |
| 592 | SLIST_INIT(&m->m_pkthdr.tags); |
| 593 | } else |
| 594 | m->m_data = m->m_dat; |
| 595 | #endif |
| 596 | return (0); |
| 597 | } |
| 598 |
| 599 |
| 600 | /* |
| 601 | * __Userspace__ |
| 602 | * The Mbuf master zone destructor. |
| 603 | * This would be called in response to umem_cache_destroy |
| 604 | * TODO: Recheck if this is what we want to do in this destructor. |
| 605 | * (Note: the number of times mb_dtor_mbuf is called is equal to the |
| 606 | * number of individual mbufs allocated from zone_mbuf. |
| 607 | */ |
| 608 | static void |
| 609 | mb_dtor_mbuf(void *mem, void *arg) |
| 610 | { |
| 611 | struct mbuf *m; |
| 612 |
| 613 | m = (struct mbuf *)mem; |
| 614 | if ((m->m_flags & M_PKTHDR) != 0) { |
| 615 | m_tag_delete_chain(m, NULL); |
| 616 | } |
| 617 | } |
| 618 |
| 619 |
| 620 | /* __Userspace__ |
| 621 | * The Cluster zone constructor. |
| 622 | * |
| 623 | * Here the 'arg' pointer points to the Mbuf which we |
| 624 | * are configuring cluster storage for. If 'arg' is |
| 625 | * empty we allocate just the cluster without setting |
| 626 | * the mbuf to it. See mbuf.h. |
| 627 | */ |
| 628 | static int |
| 629 | mb_ctor_clust(void *mem, void *arg, int flgs) |
| 630 | { |
| 631 |
| 632 | #if USING_MBUF_CONSTRUCTOR |
| 633 | struct mbuf *m; |
| 634 | struct clust_args * cla; |
| 635 | u_int *refcnt; |
| 636 | int type, size; |
| 637 | sctp_zone_t zone; |
| 638 |
| 639 | /* Assigning cluster of MCLBYTES. TODO: Add jumbo frame functionality */ |
| 640 | type = EXT_CLUSTER; |
| 641 | zone = zone_clust; |
| 642 | size = MCLBYTES; |
| 643 |
| 644 | cla = (struct clust_args *)arg; |
| 645 | m = cla->parent_mbuf; |
| 646 |
| 647 | refcnt = SCTP_ZONE_GET(zone_ext_refcnt, u_int); |
| 648 | /*refcnt = (u_int *)umem_cache_alloc(zone_ext_refcnt, UMEM_DEFAULT);*/ |
| 649 | *refcnt = 1; |
| 650 |
| 651 | if (m != NULL) { |
| 652 | m->m_ext.ext_buf = (caddr_t)mem; |
| 653 | m->m_data = m->m_ext.ext_buf; |
| 654 | m->m_flags |= M_EXT; |
| 655 | m->m_ext.ext_free = NULL; |
| 656 | m->m_ext.ext_args = NULL; |
| 657 | m->m_ext.ext_size = size; |
| 658 | m->m_ext.ext_type = type; |
| 659 | m->m_ext.ref_cnt = refcnt; |
| 660 | } |
| 661 | #endif |
| 662 | return (0); |
| 663 | } |
| 664 |
| 665 | /* __Userspace__ */ |
| 666 | static void |
| 667 | mb_dtor_clust(void *mem, void *arg) |
| 668 | { |
| 669 |
| 670 | /* mem is of type caddr_t. In sys/types.h we have typedef char * caddr_t; */ |
| 671 | /* mb_dtor_clust is called at time of umem_cache_destroy() (the number of times |
| 672 | * mb_dtor_clust is called is equal to the number of individual mbufs allocated |
| 673 | * from zone_clust. Similarly for mb_dtor_mbuf). |
| 674 | * At this point the following: |
| 675 | * struct mbuf *m; |
| 676 | * m = (struct mbuf *)arg; |
| 677 | * assert (*(m->m_ext.ref_cnt) == 0); is not meaningful since m->m_ext.ref_cnt = NULL; |
| 678 | * has been done in mb_free_ext(). |
| 679 | */ |
| 680 |
| 681 | } |
| 682 |
| 683 |
| 684 |
| 685 |
| 686 | /* Unlink and free a packet tag. */ |
| 687 | void |
| 688 | m_tag_delete(struct mbuf *m, struct m_tag *t) |
| 689 | { |
| 690 | KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", (void *)m, (void *)t)); |
| 691 | m_tag_unlink(m, t); |
| 692 | m_tag_free(t); |
| 693 | } |
| 694 |
| 695 |
| 696 | /* Unlink and free a packet tag chain, starting from given tag. */ |
| 697 | void |
| 698 | m_tag_delete_chain(struct mbuf *m, struct m_tag *t) |
| 699 | { |
| 700 |
| 701 | struct m_tag *p, *q; |
| 702 |
| 703 | KASSERT(m, ("m_tag_delete_chain: null mbuf")); |
| 704 | if (t != NULL) |
| 705 | p = t; |
| 706 | else |
| 707 | p = SLIST_FIRST(&m->m_pkthdr.tags); |
| 708 | if (p == NULL) |
| 709 | return; |
| 710 | while ((q = SLIST_NEXT(p, m_tag_link)) != NULL) |
| 711 | m_tag_delete(m, q); |
| 712 | m_tag_delete(m, p); |
| 713 | } |
| 714 |
| 715 | #if 0 |
| 716 | static void |
| 717 | sctp_print_mbuf_chain(struct mbuf *m) |
| 718 | { |
| 719 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, "Printing mbuf chain %p.\n", (void *)m); |
| 720 | for(; m; m=m->m_next) { |
| 721 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, "%p: m_len = %ld, m_type = %x, m_next = %p.\n", (void *)m, m->m_len, m->m_type, (void *)m->m_next); |
| 722 | if (m->m_flags & M_EXT) |
| 723 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, "%p: extend_size = %d, extend_buffer = %p, ref_cnt = %d.\n", (void *)m, m->m_ext.ext_size, (void *)m->m_ext.ext_buf, *(m->m_ext.ref_cnt)); |
| 724 | } |
| 725 | } |
| 726 | #endif |
| 727 |
| 728 | /* |
| 729 | * Free an entire chain of mbufs and associated external buffers, if |
| 730 | * applicable. |
| 731 | */ |
| 732 | void |
| 733 | m_freem(struct mbuf *mb) |
| 734 | { |
| 735 | while (mb != NULL) |
| 736 | mb = m_free(mb); |
| 737 | } |
| 738 |
| 739 | /* |
| 740 | * __Userspace__ |
| 741 | * clean mbufs with M_EXT storage attached to them |
| 742 | * if the reference count hits 1. |
| 743 | */ |
| 744 | void |
| 745 | mb_free_ext(struct mbuf *m) |
| 746 | { |
| 747 |
| 748 | int skipmbuf; |
| 749 |
| 750 | KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); |
| 751 | KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); |
| 752 |
| 753 | /* |
| 754 | * check if the header is embedded in the cluster |
| 755 | */ |
| 756 | skipmbuf = (m->m_flags & M_NOFREE); |
| 757 |
| 758 | /* Free the external attached storage if this |
| 759 | * mbuf is the only reference to it. |
| 760 | *__Userspace__ TODO: jumbo frames |
| 761 | * |
| 762 | */ |
| 763 | /* NOTE: We had the same code that SCTP_DECREMENT_AND_CHECK_REFCOUNT |
| 764 | reduces to here before but the IPHONE malloc commit had changed |
| 765 | this to compare to 0 instead of 1 (see next line). Why? |
| 766 | . .. this caused a huge memory leak in Linux. |
| 767 | */ |
| 768 | #ifdef IPHONE |
| 769 | if (atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 0) |
| 770 | #else |
| 771 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(m->m_ext.ref_cnt)) |
| 772 | #endif |
| 773 | { |
| 774 | if (m->m_ext.ext_type == EXT_CLUSTER){ |
| 775 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 776 | mb_dtor_clust(m->m_ext.ext_buf, &clust_mb_args); |
| 777 | #endif |
| 778 | SCTP_ZONE_FREE(zone_clust, m->m_ext.ext_buf); |
| 779 | SCTP_ZONE_FREE(zone_ext_refcnt, (u_int*)m->m_ext.ref_cnt); |
| 780 | m->m_ext.ref_cnt = NULL; |
| 781 | } |
| 782 | } |
| 783 |
| 784 | if (skipmbuf) |
| 785 | return; |
| 786 |
| 787 |
| 788 | /* __Userspace__ Also freeing the storage for ref_cnt |
| 789 | * Free this mbuf back to the mbuf zone with all m_ext |
| 790 | * information purged. |
| 791 | */ |
| 792 | m->m_ext.ext_buf = NULL; |
| 793 | m->m_ext.ext_free = NULL; |
| 794 | m->m_ext.ext_args = NULL; |
| 795 | m->m_ext.ref_cnt = NULL; |
| 796 | m->m_ext.ext_size = 0; |
| 797 | m->m_ext.ext_type = 0; |
| 798 | m->m_flags &= ~M_EXT; |
| 799 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
| 800 | mb_dtor_mbuf(m, NULL); |
| 801 | #endif |
| 802 | SCTP_ZONE_FREE(zone_mbuf, m); |
| 803 |
| 804 | /*umem_cache_free(zone_mbuf, m);*/ |
| 805 | } |
| 806 |
| 807 | /* |
| 808 | * "Move" mbuf pkthdr from "from" to "to". |
| 809 | * "from" must have M_PKTHDR set, and "to" must be empty. |
| 810 | */ |
| 811 | void |
| 812 | m_move_pkthdr(struct mbuf *to, struct mbuf *from) |
| 813 | { |
| 814 |
| 815 | to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); |
| 816 | if ((to->m_flags & M_EXT) == 0) |
| 817 | to->m_data = to->m_pktdat; |
| 818 | to->m_pkthdr = from->m_pkthdr; /* especially tags */ |
| 819 | SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ |
| 820 | from->m_flags &= ~M_PKTHDR; |
| 821 | } |
| 822 |
| 823 |
| 824 | /* |
| 825 | * Rearange an mbuf chain so that len bytes are contiguous |
| 826 | * and in the data area of an mbuf (so that mtod and dtom |
| 827 | * will work for a structure of size len). Returns the resulting |
| 828 | * mbuf chain on success, frees it and returns null on failure. |
| 829 | * If there is room, it will add up to max_protohdr-len extra bytes to the |
| 830 | * contiguous region in an attempt to avoid being called next time. |
| 831 | */ |
| 832 | struct mbuf * |
| 833 | m_pullup(struct mbuf *n, int len) |
| 834 | { |
| 835 | struct mbuf *m; |
| 836 | int count; |
| 837 | int space; |
| 838 |
| 839 | /* |
| 840 | * If first mbuf has no cluster, and has room for len bytes |
| 841 | * without shifting current data, pullup into it, |
| 842 | * otherwise allocate a new mbuf to prepend to the chain. |
| 843 | */ |
| 844 | if ((n->m_flags & M_EXT) == 0 && |
| 845 | n->m_data + len < &n->m_dat[MLEN] && n->m_next) { |
| 846 | if (n->m_len >= len) |
| 847 | return (n); |
| 848 | m = n; |
| 849 | n = n->m_next; |
| 850 | len -= m->m_len; |
| 851 | } else { |
| 852 | if (len > MHLEN) |
| 853 | goto bad; |
| 854 | MGET(m, M_NOWAIT, n->m_type); |
| 855 | if (m == NULL) |
| 856 | goto bad; |
| 857 | m->m_len = 0; |
| 858 | if (n->m_flags & M_PKTHDR) |
| 859 | M_MOVE_PKTHDR(m, n); |
| 860 | } |
| 861 | space = (int)(&m->m_dat[MLEN] - (m->m_data + m->m_len)); |
| 862 | do { |
| 863 | count = min(min(max(len, max_protohdr), space), n->m_len); |
| 864 | memcpy(mtod(m, caddr_t) + m->m_len,mtod(n, caddr_t), (u_int)count); |
| 865 | len -= count; |
| 866 | m->m_len += count; |
| 867 | n->m_len -= count; |
| 868 | space -= count; |
| 869 | if (n->m_len) |
| 870 | n->m_data += count; |
| 871 | else |
| 872 | n = m_free(n); |
| 873 | } while (len > 0 && n); |
| 874 | if (len > 0) { |
| 875 | (void) m_free(m); |
| 876 | goto bad; |
| 877 | } |
| 878 | m->m_next = n; |
| 879 | return (m); |
| 880 | bad: |
| 881 | m_freem(n); |
| 882 | return (NULL); |
| 883 | } |
| 884 |
| 885 |
| 886 | static struct mbuf * |
| 887 | m_dup1(struct mbuf *m, int off, int len, int wait) |
| 888 | { |
| 889 | struct mbuf *n = NULL; |
| 890 | int copyhdr; |
| 891 |
| 892 | if (len > MCLBYTES) |
| 893 | return NULL; |
| 894 | if (off == 0 && (m->m_flags & M_PKTHDR) != 0) |
| 895 | copyhdr = 1; |
| 896 | else |
| 897 | copyhdr = 0; |
| 898 | if (len >= MINCLSIZE) { |
| 899 | if (copyhdr == 1) { |
| 900 | m_clget(n, wait); /* TODO: include code for copying the header */ |
| 901 | m_dup_pkthdr(n, m, wait); |
| 902 | } else |
| 903 | m_clget(n, wait); |
| 904 | } else { |
| 905 | if (copyhdr == 1) |
| 906 | n = m_gethdr(wait, m->m_type); |
| 907 | else |
| 908 | n = m_get(wait, m->m_type); |
| 909 | } |
| 910 | if (!n) |
| 911 | return NULL; /* ENOBUFS */ |
| 912 |
| 913 | if (copyhdr && !m_dup_pkthdr(n, m, wait)) { |
| 914 | m_free(n); |
| 915 | return NULL; |
| 916 | } |
| 917 | m_copydata(m, off, len, mtod(n, caddr_t)); |
| 918 | n->m_len = len; |
| 919 | return n; |
| 920 | } |
| 921 |
| 922 |
| 923 | /* Taken from sys/kern/uipc_mbuf2.c */ |
| 924 | struct mbuf * |
| 925 | m_pulldown(struct mbuf *m, int off, int len, int *offp) |
| 926 | { |
| 927 | struct mbuf *n, *o; |
| 928 | int hlen, tlen, olen; |
| 929 | int writable; |
| 930 |
| 931 | /* check invalid arguments. */ |
| 932 | KASSERT(m, ("m == NULL in m_pulldown()")); |
| 933 | if (len > MCLBYTES) { |
| 934 | m_freem(m); |
| 935 | return NULL; /* impossible */ |
| 936 | } |
| 937 |
| 938 | #ifdef PULLDOWN_DEBUG |
| 939 | { |
| 940 | struct mbuf *t; |
| 941 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, "before:"); |
| 942 | for (t = m; t; t = t->m_next) |
| 943 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, " %d", t->m_len); |
| 944 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, "\n"); |
| 945 | } |
| 946 | #endif |
| 947 | n = m; |
| 948 | while (n != NULL && off > 0) { |
| 949 | if (n->m_len > off) |
| 950 | break; |
| 951 | off -= n->m_len; |
| 952 | n = n->m_next; |
| 953 | } |
| 954 | /* be sure to point non-empty mbuf */ |
| 955 | while (n != NULL && n->m_len == 0) |
| 956 | n = n->m_next; |
| 957 | if (!n) { |
| 958 | m_freem(m); |
| 959 | return NULL; /* mbuf chain too short */ |
| 960 | } |
| 961 |
| 962 | writable = 0; |
| 963 | if ((n->m_flags & M_EXT) == 0 || |
| 964 | (n->m_ext.ext_type == EXT_CLUSTER && M_WRITABLE(n))) |
| 965 | writable = 1; |
| 966 |
| 967 | /* |
| 968 | * the target data is on <n, off>. |
| 969 | * if we got enough data on the mbuf "n", we're done. |
| 970 | */ |
| 971 | if ((off == 0 || offp) && len <= n->m_len - off && writable) |
| 972 | goto ok; |
| 973 |
| 974 | /* |
| 975 | * when len <= n->m_len - off and off != 0, it is a special case. |
| 976 | * len bytes from <n, off> sits in single mbuf, but the caller does |
| 977 | * not like the starting position (off). |
| 978 | * chop the current mbuf into two pieces, set off to 0. |
| 979 | */ |
| 980 | if (len <= n->m_len - off) { |
| 981 | o = m_dup1(n, off, n->m_len - off, M_NOWAIT); |
| 982 | if (o == NULL) { |
| 983 | m_freem(m); |
| 984 | return NULL; /* ENOBUFS */ |
| 985 | } |
| 986 | n->m_len = off; |
| 987 | o->m_next = n->m_next; |
| 988 | n->m_next = o; |
| 989 | n = n->m_next; |
| 990 | off = 0; |
| 991 | goto ok; |
| 992 | } |
| 993 | /* |
| 994 | * we need to take hlen from <n, off> and tlen from <n->m_next, 0>, |
| 995 | * and construct contiguous mbuf with m_len == len. |
| 996 | * note that hlen + tlen == len, and tlen > 0. |
| 997 | */ |
| 998 | hlen = n->m_len - off; |
| 999 | tlen = len - hlen; |
| 1000 |
| 1001 | /* |
| 1002 | * ensure that we have enough trailing data on mbuf chain. |
| 1003 | * if not, we can do nothing about the chain. |
| 1004 | */ |
| 1005 | olen = 0; |
| 1006 | for (o = n->m_next; o != NULL; o = o->m_next) |
| 1007 | olen += o->m_len; |
| 1008 | if (hlen + olen < len) { |
| 1009 | m_freem(m); |
| 1010 | return NULL; /* mbuf chain too short */ |
| 1011 | } |
| 1012 |
| 1013 | /* |
| 1014 | * easy cases first. |
| 1015 | * we need to use m_copydata() to get data from <n->m_next, 0>. |
| 1016 | */ |
| 1017 | if ((off == 0 || offp) && (M_TRAILINGSPACE(n) >= tlen) && writable) { |
| 1018 | m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len); |
| 1019 | n->m_len += tlen; |
| 1020 | m_adj(n->m_next, tlen); |
| 1021 | goto ok; |
| 1022 | } |
| 1023 |
| 1024 | if ((off == 0 || offp) && (M_LEADINGSPACE(n->m_next) >= hlen) && writable) { |
| 1025 | n->m_next->m_data -= hlen; |
| 1026 | n->m_next->m_len += hlen; |
| 1027 | memcpy( mtod(n->m_next, caddr_t), mtod(n, caddr_t) + off,hlen); |
| 1028 | n->m_len -= hlen; |
| 1029 | n = n->m_next; |
| 1030 | off = 0; |
| 1031 | goto ok; |
| 1032 | } |
| 1033 |
| 1034 | /* |
| 1035 | * now, we need to do the hard way. don't m_copy as there's no room |
| 1036 | * on both end. |
| 1037 | */ |
| 1038 | if (len > MLEN) |
| 1039 | m_clget(o, M_NOWAIT); |
| 1040 | /* o = m_getcl(M_NOWAIT, m->m_type, 0);*/ |
| 1041 | else |
| 1042 | o = m_get(M_NOWAIT, m->m_type); |
| 1043 | if (!o) { |
| 1044 | m_freem(m); |
| 1045 | return NULL; /* ENOBUFS */ |
| 1046 | } |
| 1047 | /* get hlen from <n, off> into <o, 0> */ |
| 1048 | o->m_len = hlen; |
| 1049 | memcpy(mtod(o, caddr_t), mtod(n, caddr_t) + off, hlen); |
| 1050 | n->m_len -= hlen; |
| 1051 | /* get tlen from <n->m_next, 0> into <o, hlen> */ |
| 1052 | m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len); |
| 1053 | o->m_len += tlen; |
| 1054 | m_adj(n->m_next, tlen); |
| 1055 | o->m_next = n->m_next; |
| 1056 | n->m_next = o; |
| 1057 | n = o; |
| 1058 | off = 0; |
| 1059 | ok: |
| 1060 | #ifdef PULLDOWN_DEBUG |
| 1061 | { |
| 1062 | struct mbuf *t; |
| 1063 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, "after:"); |
| 1064 | for (t = m; t; t = t->m_next) |
| 1065 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, "%c%d", t == n ? '*' : ' ', t->m_len); |
| 1066 | SCTP_DEBUG_USR(SCTP_DEBUG_USR, " (off=%d)\n", off); |
| 1067 | } |
| 1068 | #endif |
| 1069 | if (offp) |
| 1070 | *offp = off; |
| 1071 | return n; |
| 1072 | } |
| 1073 |
| 1074 | /* |
| 1075 | * Attach the the cluster from *m to *n, set up m_ext in *n |
| 1076 | * and bump the refcount of the cluster. |
| 1077 | */ |
| 1078 | static void |
| 1079 | mb_dupcl(struct mbuf *n, struct mbuf *m) |
| 1080 | { |
| 1081 | KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); |
| 1082 | KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); |
| 1083 | KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); |
| 1084 |
| 1085 | if (*(m->m_ext.ref_cnt) == 1) |
| 1086 | *(m->m_ext.ref_cnt) += 1; |
| 1087 | else |
| 1088 | atomic_add_int(m->m_ext.ref_cnt, 1); |
| 1089 | n->m_ext.ext_buf = m->m_ext.ext_buf; |
| 1090 | n->m_ext.ext_free = m->m_ext.ext_free; |
| 1091 | n->m_ext.ext_args = m->m_ext.ext_args; |
| 1092 | n->m_ext.ext_size = m->m_ext.ext_size; |
| 1093 | n->m_ext.ref_cnt = m->m_ext.ref_cnt; |
| 1094 | n->m_ext.ext_type = m->m_ext.ext_type; |
| 1095 | n->m_flags |= M_EXT; |
| 1096 | } |
| 1097 |
| 1098 |
| 1099 | /* |
| 1100 | * Make a copy of an mbuf chain starting "off0" bytes from the beginning, |
| 1101 | * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. |
| 1102 | * The wait parameter is a choice of M_TRYWAIT/M_NOWAIT from caller. |
| 1103 | * Note that the copy is read-only, because clusters are not copied, |
| 1104 | * only their reference counts are incremented. |
| 1105 | */ |
| 1106 |
| 1107 | struct mbuf * |
| 1108 | m_copym(struct mbuf *m, int off0, int len, int wait) |
| 1109 | { |
| 1110 | struct mbuf *n, **np; |
| 1111 | int off = off0; |
| 1112 | struct mbuf *top; |
| 1113 | int copyhdr = 0; |
| 1114 |
| 1115 | KASSERT(off >= 0, ("m_copym, negative off %d", off)); |
| 1116 | KASSERT(len >= 0, ("m_copym, negative len %d", len)); |
| 1117 | KASSERT(m != NULL, ("m_copym, m is NULL")); |
| 1118 |
| 1119 | #if !defined(INVARIANTS) |
| 1120 | if (m == NULL) { |
| 1121 | return (NULL); |
| 1122 | } |
| 1123 | #endif |
| 1124 | if (off == 0 && m->m_flags & M_PKTHDR) |
| 1125 | copyhdr = 1; |
| 1126 | while (off > 0) { |
| 1127 | KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); |
| 1128 | if (off < m->m_len) |
| 1129 | break; |
| 1130 | off -= m->m_len; |
| 1131 | m = m->m_next; |
| 1132 | } |
| 1133 | np = ⊤ |
| 1134 | top = 0; |
| 1135 | while (len > 0) { |
| 1136 | if (m == NULL) { |
| 1137 | KASSERT(len == M_COPYALL, ("m_copym, length > size of mbuf chain")); |
| 1138 | break; |
| 1139 | } |
| 1140 | if (copyhdr) |
| 1141 | MGETHDR(n, wait, m->m_type); |
| 1142 | else |
| 1143 | MGET(n, wait, m->m_type); |
| 1144 | *np = n; |
| 1145 | if (n == NULL) |
| 1146 | goto nospace; |
| 1147 | if (copyhdr) { |
| 1148 | if (!m_dup_pkthdr(n, m, wait)) |
| 1149 | goto nospace; |
| 1150 | if (len == M_COPYALL) |
| 1151 | n->m_pkthdr.len -= off0; |
| 1152 | else |
| 1153 | n->m_pkthdr.len = len; |
| 1154 | copyhdr = 0; |
| 1155 | } |
| 1156 | n->m_len = min(len, m->m_len - off); |
| 1157 | if (m->m_flags & M_EXT) { |
| 1158 | n->m_data = m->m_data + off; |
| 1159 | mb_dupcl(n, m); |
| 1160 | } else |
| 1161 | memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, (u_int)n->m_len); |
| 1162 | if (len != M_COPYALL) |
| 1163 | len -= n->m_len; |
| 1164 | off = 0; |
| 1165 | m = m->m_next; |
| 1166 | np = &n->m_next; |
| 1167 | } |
| 1168 |
| 1169 | return (top); |
| 1170 | nospace: |
| 1171 | m_freem(top); |
| 1172 | return (NULL); |
| 1173 | } |
| 1174 |
| 1175 |
| 1176 | int |
| 1177 | m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how) |
| 1178 | { |
| 1179 | struct m_tag *p, *t, *tprev = NULL; |
| 1180 |
| 1181 | KASSERT(to && from, ("m_tag_copy_chain: null argument, to %p from %p", (void *)to, (void *)from)); |
| 1182 | m_tag_delete_chain(to, NULL); |
| 1183 | SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) { |
| 1184 | t = m_tag_copy(p, how); |
| 1185 | if (t == NULL) { |
| 1186 | m_tag_delete_chain(to, NULL); |
| 1187 | return 0; |
| 1188 | } |
| 1189 | if (tprev == NULL) |
| 1190 | SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link); |
| 1191 | else |
| 1192 | SLIST_INSERT_AFTER(tprev, t, m_tag_link); |
| 1193 | tprev = t; |
| 1194 | } |
| 1195 | return 1; |
| 1196 | } |
| 1197 |
| 1198 | /* |
| 1199 | * Duplicate "from"'s mbuf pkthdr in "to". |
| 1200 | * "from" must have M_PKTHDR set, and "to" must be empty. |
| 1201 | * In particular, this does a deep copy of the packet tags. |
| 1202 | */ |
| 1203 | int |
| 1204 | m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) |
| 1205 | { |
| 1206 |
| 1207 | KASSERT(to, ("m_dup_pkthdr: to is NULL")); |
| 1208 | KASSERT(from, ("m_dup_pkthdr: from is NULL")); |
| 1209 | to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); |
| 1210 | if ((to->m_flags & M_EXT) == 0) |
| 1211 | to->m_data = to->m_pktdat; |
| 1212 | to->m_pkthdr = from->m_pkthdr; |
| 1213 | SLIST_INIT(&to->m_pkthdr.tags); |
| 1214 | return (m_tag_copy_chain(to, from, MBTOM(how))); |
| 1215 | } |
| 1216 |
| 1217 | /* Copy a single tag. */ |
| 1218 | struct m_tag * |
| 1219 | m_tag_copy(struct m_tag *t, int how) |
| 1220 | { |
| 1221 | struct m_tag *p; |
| 1222 |
| 1223 | KASSERT(t, ("m_tag_copy: null tag")); |
| 1224 | p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how); |
| 1225 | if (p == NULL) |
| 1226 | return (NULL); |
| 1227 | memcpy(p + 1, t + 1, t->m_tag_len); /* Copy the data */ |
| 1228 | return p; |
| 1229 | } |
| 1230 |
| 1231 | /* Get a packet tag structure along with specified data following. */ |
| 1232 | struct m_tag * |
| 1233 | m_tag_alloc(uint32_t cookie, int type, int len, int wait) |
| 1234 | { |
| 1235 | struct m_tag *t; |
| 1236 |
| 1237 | if (len < 0) |
| 1238 | return NULL; |
| 1239 | t = malloc(len + sizeof(struct m_tag)); |
| 1240 | if (t == NULL) |
| 1241 | return NULL; |
| 1242 | m_tag_setup(t, cookie, type, len); |
| 1243 | t->m_tag_free = m_tag_free_default; |
| 1244 | return t; |
| 1245 | } |
| 1246 |
| 1247 | /* Free a packet tag. */ |
| 1248 | void |
| 1249 | m_tag_free_default(struct m_tag *t) |
| 1250 | { |
| 1251 | free(t); |
| 1252 | } |
| 1253 |
| 1254 | /* |
| 1255 | * Copy data from a buffer back into the indicated mbuf chain, |
| 1256 | * starting "off" bytes from the beginning, extending the mbuf |
| 1257 | * chain if necessary. |
| 1258 | */ |
| 1259 | void |
| 1260 | m_copyback(struct mbuf *m0, int off, int len, caddr_t cp) |
| 1261 | { |
| 1262 | int mlen; |
| 1263 | struct mbuf *m = m0, *n; |
| 1264 | int totlen = 0; |
| 1265 |
| 1266 | if (m0 == NULL) |
| 1267 | return; |
| 1268 | while (off > (mlen = m->m_len)) { |
| 1269 | off -= mlen; |
| 1270 | totlen += mlen; |
| 1271 | if (m->m_next == NULL) { |
| 1272 | n = m_get(M_NOWAIT, m->m_type); |
| 1273 | if (n == NULL) |
| 1274 | goto out; |
| 1275 | memset(mtod(n, caddr_t), 0, MLEN); |
| 1276 | n->m_len = min(MLEN, len + off); |
| 1277 | m->m_next = n; |
| 1278 | } |
| 1279 | m = m->m_next; |
| 1280 | } |
| 1281 | while (len > 0) { |
| 1282 | mlen = min (m->m_len - off, len); |
| 1283 | memcpy(off + mtod(m, caddr_t), cp, (u_int)mlen); |
| 1284 | cp += mlen; |
| 1285 | len -= mlen; |
| 1286 | mlen += off; |
| 1287 | off = 0; |
| 1288 | totlen += mlen; |
| 1289 | if (len == 0) |
| 1290 | break; |
| 1291 | if (m->m_next == NULL) { |
| 1292 | n = m_get(M_NOWAIT, m->m_type); |
| 1293 | if (n == NULL) |
| 1294 | break; |
| 1295 | n->m_len = min(MLEN, len); |
| 1296 | m->m_next = n; |
| 1297 | } |
| 1298 | m = m->m_next; |
| 1299 | } |
| 1300 | out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) |
| 1301 | m->m_pkthdr.len = totlen; |
| 1302 | } |
| 1303 |
| 1304 |
| 1305 | /* |
| 1306 | * Lesser-used path for M_PREPEND: |
| 1307 | * allocate new mbuf to prepend to chain, |
| 1308 | * copy junk along. |
| 1309 | */ |
| 1310 | struct mbuf * |
| 1311 | m_prepend(struct mbuf *m, int len, int how) |
| 1312 | { |
| 1313 | struct mbuf *mn; |
| 1314 |
| 1315 | if (m->m_flags & M_PKTHDR) |
| 1316 | MGETHDR(mn, how, m->m_type); |
| 1317 | else |
| 1318 | MGET(mn, how, m->m_type); |
| 1319 | if (mn == NULL) { |
| 1320 | m_freem(m); |
| 1321 | return (NULL); |
| 1322 | } |
| 1323 | if (m->m_flags & M_PKTHDR) |
| 1324 | M_MOVE_PKTHDR(mn, m); |
| 1325 | mn->m_next = m; |
| 1326 | m = mn; |
| 1327 | if (m->m_flags & M_PKTHDR) { |
| 1328 | if (len < MHLEN) |
| 1329 | MH_ALIGN(m, len); |
| 1330 | } else { |
| 1331 | if (len < MLEN) |
| 1332 | M_ALIGN(m, len); |
| 1333 | } |
| 1334 | m->m_len = len; |
| 1335 | return (m); |
| 1336 | } |
| 1337 |
| 1338 | /* |
| 1339 | * Copy data from an mbuf chain starting "off" bytes from the beginning, |
| 1340 | * continuing for "len" bytes, into the indicated buffer. |
| 1341 | */ |
| 1342 | void |
| 1343 | m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) |
| 1344 | { |
| 1345 | u_int count; |
| 1346 |
| 1347 | KASSERT(off >= 0, ("m_copydata, negative off %d", off)); |
| 1348 | KASSERT(len >= 0, ("m_copydata, negative len %d", len)); |
| 1349 | while (off > 0) { |
| 1350 | KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); |
| 1351 | if (off < m->m_len) |
| 1352 | break; |
| 1353 | off -= m->m_len; |
| 1354 | m = m->m_next; |
| 1355 | } |
| 1356 | while (len > 0) { |
| 1357 | KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); |
| 1358 | count = min(m->m_len - off, len); |
| 1359 | memcpy(cp, mtod(m, caddr_t) + off, count); |
| 1360 | len -= count; |
| 1361 | cp += count; |
| 1362 | off = 0; |
| 1363 | m = m->m_next; |
| 1364 | } |
| 1365 | } |
| 1366 |
| 1367 |
| 1368 | /* |
| 1369 | * Concatenate mbuf chain n to m. |
| 1370 | * Both chains must be of the same type (e.g. MT_DATA). |
| 1371 | * Any m_pkthdr is not updated. |
| 1372 | */ |
| 1373 | void |
| 1374 | m_cat(struct mbuf *m, struct mbuf *n) |
| 1375 | { |
| 1376 | while (m->m_next) |
| 1377 | m = m->m_next; |
| 1378 | while (n) { |
| 1379 | if (m->m_flags & M_EXT || |
| 1380 | m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { |
| 1381 | /* just join the two chains */ |
| 1382 | m->m_next = n; |
| 1383 | return; |
| 1384 | } |
| 1385 | /* splat the data from one into the other */ |
| 1386 | memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), (u_int)n->m_len); |
| 1387 | m->m_len += n->m_len; |
| 1388 | n = m_free(n); |
| 1389 | } |
| 1390 | } |
| 1391 |
| 1392 |
| 1393 | void |
| 1394 | m_adj(struct mbuf *mp, int req_len) |
| 1395 | { |
| 1396 | int len = req_len; |
| 1397 | struct mbuf *m; |
| 1398 | int count; |
| 1399 |
| 1400 | if ((m = mp) == NULL) |
| 1401 | return; |
| 1402 | if (len >= 0) { |
| 1403 | /* |
| 1404 | * Trim from head. |
| 1405 | */ |
| 1406 | while (m != NULL && len > 0) { |
| 1407 | if (m->m_len <= len) { |
| 1408 | len -= m->m_len; |
| 1409 | m->m_len = 0; |
| 1410 | m = m->m_next; |
| 1411 | } else { |
| 1412 | m->m_len -= len; |
| 1413 | m->m_data += len; |
| 1414 | len = 0; |
| 1415 | } |
| 1416 | } |
| 1417 | m = mp; |
| 1418 | if (mp->m_flags & M_PKTHDR) |
| 1419 | m->m_pkthdr.len -= (req_len - len); |
| 1420 | } else { |
| 1421 | /* |
| 1422 | * Trim from tail. Scan the mbuf chain, |
| 1423 | * calculating its length and finding the last mbuf. |
| 1424 | * If the adjustment only affects this mbuf, then just |
| 1425 | * adjust and return. Otherwise, rescan and truncate |
| 1426 | * after the remaining size. |
| 1427 | */ |
| 1428 | len = -len; |
| 1429 | count = 0; |
| 1430 | for (;;) { |
| 1431 | count += m->m_len; |
| 1432 | if (m->m_next == (struct mbuf *)0) |
| 1433 | break; |
| 1434 | m = m->m_next; |
| 1435 | } |
| 1436 | if (m->m_len >= len) { |
| 1437 | m->m_len -= len; |
| 1438 | if (mp->m_flags & M_PKTHDR) |
| 1439 | mp->m_pkthdr.len -= len; |
| 1440 | return; |
| 1441 | } |
| 1442 | count -= len; |
| 1443 | if (count < 0) |
| 1444 | count = 0; |
| 1445 | /* |
| 1446 | * Correct length for chain is "count". |
| 1447 | * Find the mbuf with last data, adjust its length, |
| 1448 | * and toss data from remaining mbufs on chain. |
| 1449 | */ |
| 1450 | m = mp; |
| 1451 | if (m->m_flags & M_PKTHDR) |
| 1452 | m->m_pkthdr.len = count; |
| 1453 | for (; m; m = m->m_next) { |
| 1454 | if (m->m_len >= count) { |
| 1455 | m->m_len = count; |
| 1456 | if (m->m_next != NULL) { |
| 1457 | m_freem(m->m_next); |
| 1458 | m->m_next = NULL; |
| 1459 | } |
| 1460 | break; |
| 1461 | } |
| 1462 | count -= m->m_len; |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 |
| 1467 |
| 1468 | /* m_split is used within sctp_handle_cookie_echo. */ |
| 1469 |
| 1470 | /* |
| 1471 | * Partition an mbuf chain in two pieces, returning the tail -- |
| 1472 | * all but the first len0 bytes. In case of failure, it returns NULL and |
| 1473 | * attempts to restore the chain to its original state. |
| 1474 | * |
| 1475 | * Note that the resulting mbufs might be read-only, because the new |
| 1476 | * mbuf can end up sharing an mbuf cluster with the original mbuf if |
| 1477 | * the "breaking point" happens to lie within a cluster mbuf. Use the |
| 1478 | * M_WRITABLE() macro to check for this case. |
| 1479 | */ |
| 1480 | struct mbuf * |
| 1481 | m_split(struct mbuf *m0, int len0, int wait) |
| 1482 | { |
| 1483 | struct mbuf *m, *n; |
| 1484 | u_int len = len0, remain; |
| 1485 |
| 1486 | /* MBUF_CHECKSLEEP(wait); */ |
| 1487 | for (m = m0; m && (int)len > m->m_len; m = m->m_next) |
| 1488 | len -= m->m_len; |
| 1489 | if (m == NULL) |
| 1490 | return (NULL); |
| 1491 | remain = m->m_len - len; |
| 1492 | if (m0->m_flags & M_PKTHDR) { |
| 1493 | MGETHDR(n, wait, m0->m_type); |
| 1494 | if (n == NULL) |
| 1495 | return (NULL); |
| 1496 | n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; |
| 1497 | n->m_pkthdr.len = m0->m_pkthdr.len - len0; |
| 1498 | m0->m_pkthdr.len = len0; |
| 1499 | if (m->m_flags & M_EXT) |
| 1500 | goto extpacket; |
| 1501 | if (remain > MHLEN) { |
| 1502 | /* m can't be the lead packet */ |
| 1503 | MH_ALIGN(n, 0); |
| 1504 | n->m_next = m_split(m, len, wait); |
| 1505 | if (n->m_next == NULL) { |
| 1506 | (void) m_free(n); |
| 1507 | return (NULL); |
| 1508 | } else { |
| 1509 | n->m_len = 0; |
| 1510 | return (n); |
| 1511 | } |
| 1512 | } else |
| 1513 | MH_ALIGN(n, remain); |
| 1514 | } else if (remain == 0) { |
| 1515 | n = m->m_next; |
| 1516 | m->m_next = NULL; |
| 1517 | return (n); |
| 1518 | } else { |
| 1519 | MGET(n, wait, m->m_type); |
| 1520 | if (n == NULL) |
| 1521 | return (NULL); |
| 1522 | M_ALIGN(n, remain); |
| 1523 | } |
| 1524 | extpacket: |
| 1525 | if (m->m_flags & M_EXT) { |
| 1526 | n->m_data = m->m_data + len; |
| 1527 | mb_dupcl(n, m); |
| 1528 | } else { |
| 1529 | memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain); |
| 1530 | } |
| 1531 | n->m_len = remain; |
| 1532 | m->m_len = len; |
| 1533 | n->m_next = m->m_next; |
| 1534 | m->m_next = NULL; |
| 1535 | return (n); |
| 1536 | } |
| 1537 |
| 1538 |
| 1539 |
| 1540 |
| 1541 | int |
| 1542 | pack_send_buffer(caddr_t buffer, struct mbuf* mb){ |
| 1543 |
| 1544 | int count_to_copy; |
| 1545 | int total_count_copied = 0; |
| 1546 | int offset = 0; |
| 1547 |
| 1548 | do { |
| 1549 | count_to_copy = mb->m_len; |
| 1550 | memcpy(buffer+offset, mtod(mb, caddr_t), count_to_copy); |
| 1551 | offset += count_to_copy; |
| 1552 | total_count_copied += count_to_copy; |
| 1553 | mb = mb->m_next; |
| 1554 | } while(mb); |
| 1555 |
| 1556 | return (total_count_copied); |
| 1557 | } |
| 1558 |