The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_mchain.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2000, 2001 Boris Popov
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the author nor the names of any co-contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: releng/8.3/sys/kern/subr_mchain.c 230217 2012-01-16 14:40:22Z kevlo $");
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/endian.h>
   36 #include <sys/errno.h>
   37 #include <sys/mbuf.h>
   38 #include <sys/module.h>
   39 #include <sys/uio.h>
   40 
   41 #include <sys/mchain.h>
   42 
   43 MODULE_VERSION(libmchain, 1);
   44 
   45 #define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \
   46                                     __LINE__ , ## __VA_ARGS__)
   47 
   48 #define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \
   49                                     __LINE__ , ## __VA_ARGS__)
   50 
   51 /*
   52  * Various helper functions
   53  */
   54 int
   55 mb_init(struct mbchain *mbp)
   56 {
   57         struct mbuf *m;
   58 
   59         m = m_gethdr(M_WAIT, MT_DATA);
   60         m->m_len = 0;
   61         mb_initm(mbp, m);
   62         return 0;
   63 }
   64 
   65 void
   66 mb_initm(struct mbchain *mbp, struct mbuf *m)
   67 {
   68         bzero(mbp, sizeof(*mbp));
   69         mbp->mb_top = mbp->mb_cur = m;
   70         mbp->mb_mleft = M_TRAILINGSPACE(m);
   71 }
   72 
   73 void
   74 mb_done(struct mbchain *mbp)
   75 {
   76         if (mbp->mb_top) {
   77                 m_freem(mbp->mb_top);
   78                 mbp->mb_top = NULL;
   79         }
   80 }
   81 
   82 struct mbuf *
   83 mb_detach(struct mbchain *mbp)
   84 {
   85         struct mbuf *m;
   86 
   87         m = mbp->mb_top;
   88         mbp->mb_top = NULL;
   89         return m;
   90 }
   91 
   92 int
   93 mb_fixhdr(struct mbchain *mbp)
   94 {
   95         return mbp->mb_top->m_pkthdr.len = m_fixhdr(mbp->mb_top);
   96 }
   97 
   98 /*
   99  * Check if object of size 'size' fit to the current position and
  100  * allocate new mbuf if not. Advance pointers and increase length of mbuf(s).
  101  * Return pointer to the object placeholder or NULL if any error occured.
  102  * Note: size should be <= MLEN 
  103  */
  104 caddr_t
  105 mb_reserve(struct mbchain *mbp, int size)
  106 {
  107         struct mbuf *m, *mn;
  108         caddr_t bpos;
  109 
  110         if (size > MLEN)
  111                 panic("mb_reserve: size = %d\n", size);
  112         m = mbp->mb_cur;
  113         if (mbp->mb_mleft < size) {
  114                 mn = m_get(M_WAIT, MT_DATA);
  115                 mbp->mb_cur = m->m_next = mn;
  116                 m = mn;
  117                 m->m_len = 0;
  118                 mbp->mb_mleft = M_TRAILINGSPACE(m);
  119         }
  120         mbp->mb_mleft -= size;
  121         mbp->mb_count += size;
  122         bpos = mtod(m, caddr_t) + m->m_len;
  123         m->m_len += size;
  124         return bpos;
  125 }
  126 
  127 int
  128 mb_put_padbyte(struct mbchain *mbp)
  129 {
  130         caddr_t dst;
  131         char x = 0;
  132 
  133         dst = mtod(mbp->mb_cur, caddr_t) + mbp->mb_cur->m_len;
  134 
  135         /* only add padding if address is odd */
  136         if ((unsigned long)dst & 1)
  137                 return mb_put_mem(mbp, (caddr_t)&x, 1, MB_MSYSTEM);
  138         else
  139         return 0;
  140 }
  141 
  142 int
  143 mb_put_uint8(struct mbchain *mbp, u_int8_t x)
  144 {
  145         return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
  146 }
  147 
  148 int
  149 mb_put_uint16be(struct mbchain *mbp, u_int16_t x)
  150 {
  151         x = htobe16(x);
  152         return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
  153 }
  154 
  155 int
  156 mb_put_uint16le(struct mbchain *mbp, u_int16_t x)
  157 {
  158         x = htole16(x);
  159         return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
  160 }
  161 
  162 int
  163 mb_put_uint32be(struct mbchain *mbp, u_int32_t x)
  164 {
  165         x = htobe32(x);
  166         return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
  167 }
  168 
  169 int
  170 mb_put_uint32le(struct mbchain *mbp, u_int32_t x)
  171 {
  172         x = htole32(x);
  173         return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
  174 }
  175 
  176 int
  177 mb_put_int64be(struct mbchain *mbp, int64_t x)
  178 {
  179         x = htobe64(x);
  180         return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
  181 }
  182 
  183 int
  184 mb_put_int64le(struct mbchain *mbp, int64_t x)
  185 {
  186         x = htole64(x);
  187         return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
  188 }
  189 
  190 int
  191 mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type)
  192 {
  193         struct mbuf *m;
  194         caddr_t dst;
  195         c_caddr_t src;
  196         int cplen, error, mleft, count;
  197         size_t srclen, dstlen;
  198 
  199         m = mbp->mb_cur;
  200         mleft = mbp->mb_mleft;
  201 
  202         while (size > 0) {
  203                 if (mleft == 0) {
  204                         if (m->m_next == NULL)
  205                                 m = m_getm(m, size, M_WAIT, MT_DATA);
  206                         else
  207                                 m = m->m_next;
  208                         mleft = M_TRAILINGSPACE(m);
  209                         continue;
  210                 }
  211                 cplen = mleft > size ? size : mleft;
  212                 srclen = dstlen = cplen;
  213                 dst = mtod(m, caddr_t) + m->m_len;
  214                 switch (type) {
  215                     case MB_MCUSTOM:
  216                         srclen = size;
  217                         dstlen = mleft;
  218                         error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen);
  219                         if (error)
  220                                 return error;
  221                         break;
  222                     case MB_MINLINE:
  223                         for (src = source, count = cplen; count; count--)
  224                                 *dst++ = *src++;
  225                         break;
  226                     case MB_MSYSTEM:
  227                         bcopy(source, dst, cplen);
  228                         break;
  229                     case MB_MUSER:
  230                         error = copyin(source, dst, cplen);
  231                         if (error)
  232                                 return error;
  233                         break;
  234                     case MB_MZERO:
  235                         bzero(dst, cplen);
  236                         break;
  237                 }
  238                 size -= srclen;
  239                 source += srclen;
  240                 m->m_len += dstlen;
  241                 mleft -= dstlen;
  242                 mbp->mb_count += dstlen;
  243         }
  244         mbp->mb_cur = m;
  245         mbp->mb_mleft = mleft;
  246         return 0;
  247 }
  248 
  249 int
  250 mb_put_mbuf(struct mbchain *mbp, struct mbuf *m)
  251 {
  252         mbp->mb_cur->m_next = m;
  253         while (m) {
  254                 mbp->mb_count += m->m_len;
  255                 if (m->m_next == NULL)
  256                         break;
  257                 m = m->m_next;
  258         }
  259         mbp->mb_mleft = M_TRAILINGSPACE(m);
  260         mbp->mb_cur = m;
  261         return 0;
  262 }
  263 
  264 /*
  265  * copies a uio scatter/gather list to an mbuf chain.
  266  */
  267 int
  268 mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size)
  269 {
  270         long left;
  271         int mtype, error;
  272 
  273         mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
  274 
  275         while (size > 0 && uiop->uio_resid) {
  276                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
  277                         return EFBIG;
  278                 left = uiop->uio_iov->iov_len;
  279                 if (left == 0) {
  280                         uiop->uio_iov++;
  281                         uiop->uio_iovcnt--;
  282                         continue;
  283                 }
  284                 if (left > size)
  285                         left = size;
  286                 error = mb_put_mem(mbp, uiop->uio_iov->iov_base, left, mtype);
  287                 if (error)
  288                         return error;
  289                 uiop->uio_offset += left;
  290                 uiop->uio_resid -= left;
  291                 uiop->uio_iov->iov_base =
  292                     (char *)uiop->uio_iov->iov_base + left;
  293                 uiop->uio_iov->iov_len -= left;
  294                 size -= left;
  295         }
  296         return 0;
  297 }
  298 
  299 /*
  300  * Routines for fetching data from an mbuf chain
  301  */
  302 int
  303 md_init(struct mdchain *mdp)
  304 {
  305         struct mbuf *m;
  306 
  307         m = m_gethdr(M_WAIT, MT_DATA);
  308         m->m_len = 0;
  309         md_initm(mdp, m);
  310         return 0;
  311 }
  312 
  313 void
  314 md_initm(struct mdchain *mdp, struct mbuf *m)
  315 {
  316         bzero(mdp, sizeof(*mdp));
  317         mdp->md_top = mdp->md_cur = m;
  318         mdp->md_pos = mtod(m, u_char*);
  319 }
  320 
  321 void
  322 md_done(struct mdchain *mdp)
  323 {
  324         if (mdp->md_top) {
  325                 m_freem(mdp->md_top);
  326                 mdp->md_top = NULL;
  327         }
  328 }
  329 
  330 /*
  331  * Append a separate mbuf chain. It is caller responsibility to prevent
  332  * multiple calls to fetch/record routines.
  333  */
  334 void
  335 md_append_record(struct mdchain *mdp, struct mbuf *top)
  336 {
  337         struct mbuf *m;
  338 
  339         if (mdp->md_top == NULL) {
  340                 md_initm(mdp, top);
  341                 return;
  342         }
  343         m = mdp->md_top;
  344         while (m->m_nextpkt)
  345                 m = m->m_nextpkt;
  346         m->m_nextpkt = top;
  347         top->m_nextpkt = NULL;
  348         return;
  349 }
  350 
  351 /*
  352  * Put next record in place of existing
  353  */
  354 int
  355 md_next_record(struct mdchain *mdp)
  356 {
  357         struct mbuf *m;
  358 
  359         if (mdp->md_top == NULL)
  360                 return ENOENT;
  361         m = mdp->md_top->m_nextpkt;
  362         md_done(mdp);
  363         if (m == NULL)
  364                 return ENOENT;
  365         md_initm(mdp, m);
  366         return 0;
  367 }
  368 
  369 int
  370 md_get_uint8(struct mdchain *mdp, u_int8_t *x)
  371 {
  372         return md_get_mem(mdp, x, 1, MB_MINLINE);
  373 }
  374 
  375 int
  376 md_get_uint16(struct mdchain *mdp, u_int16_t *x)
  377 {
  378         return md_get_mem(mdp, (caddr_t)x, 2, MB_MINLINE);
  379 }
  380 
  381 int
  382 md_get_uint16le(struct mdchain *mdp, u_int16_t *x)
  383 {
  384         u_int16_t v;
  385         int error = md_get_uint16(mdp, &v);
  386 
  387         if (x != NULL)
  388                 *x = le16toh(v);
  389         return error;
  390 }
  391 
  392 int
  393 md_get_uint16be(struct mdchain *mdp, u_int16_t *x) {
  394         u_int16_t v;
  395         int error = md_get_uint16(mdp, &v);
  396 
  397         if (x != NULL)
  398                 *x = be16toh(v);
  399         return error;
  400 }
  401 
  402 int
  403 md_get_uint32(struct mdchain *mdp, u_int32_t *x)
  404 {
  405         return md_get_mem(mdp, (caddr_t)x, 4, MB_MINLINE);
  406 }
  407 
  408 int
  409 md_get_uint32be(struct mdchain *mdp, u_int32_t *x)
  410 {
  411         u_int32_t v;
  412         int error;
  413 
  414         error = md_get_uint32(mdp, &v);
  415         if (x != NULL)
  416                 *x = be32toh(v);
  417         return error;
  418 }
  419 
  420 int
  421 md_get_uint32le(struct mdchain *mdp, u_int32_t *x)
  422 {
  423         u_int32_t v;
  424         int error;
  425 
  426         error = md_get_uint32(mdp, &v);
  427         if (x != NULL)
  428                 *x = le32toh(v);
  429         return error;
  430 }
  431 
  432 int
  433 md_get_int64(struct mdchain *mdp, int64_t *x)
  434 {
  435         return md_get_mem(mdp, (caddr_t)x, 8, MB_MINLINE);
  436 }
  437 
  438 int
  439 md_get_int64be(struct mdchain *mdp, int64_t *x)
  440 {
  441         int64_t v;
  442         int error;
  443 
  444         error = md_get_int64(mdp, &v);
  445         if (x != NULL)
  446                 *x = be64toh(v);
  447         return error;
  448 }
  449 
  450 int
  451 md_get_int64le(struct mdchain *mdp, int64_t *x)
  452 {
  453         int64_t v;
  454         int error;
  455 
  456         error = md_get_int64(mdp, &v);
  457         if (x != NULL)
  458                 *x = le64toh(v);
  459         return error;
  460 }
  461 
  462 int
  463 md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type)
  464 {
  465         struct mbuf *m = mdp->md_cur;
  466         int error;
  467         u_int count;
  468         u_char *s;
  469         
  470         while (size > 0) {
  471                 if (m == NULL) {
  472                         MBERROR("incomplete copy\n");
  473                         return EBADRPC;
  474                 }
  475                 s = mdp->md_pos;
  476                 count = mtod(m, u_char*) + m->m_len - s;
  477                 if (count == 0) {
  478                         mdp->md_cur = m = m->m_next;
  479                         if (m)
  480                                 s = mdp->md_pos = mtod(m, caddr_t);
  481                         continue;
  482                 }
  483                 if (count > size)
  484                         count = size;
  485                 size -= count;
  486                 mdp->md_pos += count;
  487                 if (target == NULL)
  488                         continue;
  489                 switch (type) {
  490                     case MB_MUSER:
  491                         error = copyout(s, target, count);
  492                         if (error)
  493                                 return error;
  494                         break;
  495                     case MB_MSYSTEM:
  496                         bcopy(s, target, count);
  497                         break;
  498                     case MB_MINLINE:
  499                         while (count--)
  500                                 *target++ = *s++;
  501                         continue;
  502                 }
  503                 target += count;
  504         }
  505         return 0;
  506 }
  507 
  508 int
  509 md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **ret)
  510 {
  511         struct mbuf *m = mdp->md_cur, *rm;
  512 
  513         rm = m_copym(m, mdp->md_pos - mtod(m, u_char*), size, M_WAIT);
  514         md_get_mem(mdp, NULL, size, MB_MZERO);
  515         *ret = rm;
  516         return 0;
  517 }
  518 
  519 int
  520 md_get_uio(struct mdchain *mdp, struct uio *uiop, int size)
  521 {
  522         char *uiocp;
  523         long left;
  524         int mtype, error;
  525 
  526         mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
  527         while (size > 0 && uiop->uio_resid) {
  528                 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
  529                         return EFBIG;
  530                 left = uiop->uio_iov->iov_len;
  531                 if (left == 0) {
  532                         uiop->uio_iov++;
  533                         uiop->uio_iovcnt--;
  534                         continue;
  535                 }
  536                 uiocp = uiop->uio_iov->iov_base;
  537                 if (left > size)
  538                         left = size;
  539                 error = md_get_mem(mdp, uiocp, left, mtype);
  540                 if (error)
  541                         return error;
  542                 uiop->uio_offset += left;
  543                 uiop->uio_resid -= left;
  544                 uiop->uio_iov->iov_base =
  545                     (char *)uiop->uio_iov->iov_base + left;
  546                 uiop->uio_iov->iov_len -= left;
  547                 size -= left;
  548         }
  549         return 0;
  550 }

Cache object: a1270be240b014641162366717d4bcd3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.