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