Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /*- |
michael@0 | 2 | * Copyright (c) 1982, 1986, 1988, 1993 |
michael@0 | 3 | * The Regents of the University of California. |
michael@0 | 4 | * All rights reserved. |
michael@0 | 5 | * |
michael@0 | 6 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | * modification, are permitted provided that the following conditions |
michael@0 | 8 | * are met: |
michael@0 | 9 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | * documentation and/or other materials provided with the distribution. |
michael@0 | 14 | * 3. Neither the name of the University nor the names of its contributors |
michael@0 | 15 | * may be used to endorse or promote products derived from this software |
michael@0 | 16 | * without specific prior written permission. |
michael@0 | 17 | * |
michael@0 | 18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
michael@0 | 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
michael@0 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
michael@0 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
michael@0 | 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
michael@0 | 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
michael@0 | 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
michael@0 | 28 | * SUCH DAMAGE. |
michael@0 | 29 | * |
michael@0 | 30 | */ |
michael@0 | 31 | |
michael@0 | 32 | #ifndef _USER_MBUF_H_ |
michael@0 | 33 | #define _USER_MBUF_H_ |
michael@0 | 34 | |
michael@0 | 35 | /* __Userspace__ header file for mbufs */ |
michael@0 | 36 | #include <stdio.h> |
michael@0 | 37 | #if !defined(SCTP_SIMPLE_ALLOCATOR) |
michael@0 | 38 | #include "umem.h" |
michael@0 | 39 | #endif |
michael@0 | 40 | #include "user_malloc.h" |
michael@0 | 41 | #include "netinet/sctp_os_userspace.h" |
michael@0 | 42 | |
michael@0 | 43 | #define USING_MBUF_CONSTRUCTOR 0 |
michael@0 | 44 | |
michael@0 | 45 | /* For Linux */ |
michael@0 | 46 | #ifndef MSIZE |
michael@0 | 47 | #define MSIZE 256 |
michael@0 | 48 | /* #define MSIZE 1024 */ |
michael@0 | 49 | #endif |
michael@0 | 50 | #ifndef MCLBYTES |
michael@0 | 51 | #define MCLBYTES 2048 |
michael@0 | 52 | #endif |
michael@0 | 53 | |
michael@0 | 54 | struct mbuf * m_gethdr(int how, short type); |
michael@0 | 55 | struct mbuf * m_get(int how, short type); |
michael@0 | 56 | struct mbuf * m_free(struct mbuf *m); |
michael@0 | 57 | void m_clget(struct mbuf *m, int how); |
michael@0 | 58 | |
michael@0 | 59 | |
michael@0 | 60 | /* mbuf initialization function */ |
michael@0 | 61 | void mbuf_init(void *); |
michael@0 | 62 | |
michael@0 | 63 | #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from)) |
michael@0 | 64 | #define MGET(m, how, type) ((m) = m_get((how), (type))) |
michael@0 | 65 | #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type))) |
michael@0 | 66 | #define MCLGET(m, how) m_clget((m), (how)) |
michael@0 | 67 | |
michael@0 | 68 | |
michael@0 | 69 | #define M_HDR_PAD ((sizeof(intptr_t)==4) ? 2 : 6) /* modified for __Userspace__ */ |
michael@0 | 70 | |
michael@0 | 71 | /* Length to m_copy to copy all. */ |
michael@0 | 72 | #define M_COPYALL 1000000000 |
michael@0 | 73 | |
michael@0 | 74 | /* umem_cache_t is defined in user_include/umem.h as |
michael@0 | 75 | * typedef struct umem_cache umem_cache_t; |
michael@0 | 76 | * Note:umem_zone_t is a pointer. |
michael@0 | 77 | */ |
michael@0 | 78 | #if defined(SCTP_SIMPLE_ALLOCATOR) |
michael@0 | 79 | typedef size_t sctp_zone_t; |
michael@0 | 80 | #else |
michael@0 | 81 | typedef umem_cache_t *sctp_zone_t; |
michael@0 | 82 | #endif |
michael@0 | 83 | |
michael@0 | 84 | extern sctp_zone_t zone_mbuf; |
michael@0 | 85 | extern sctp_zone_t zone_clust; |
michael@0 | 86 | extern sctp_zone_t zone_ext_refcnt; |
michael@0 | 87 | |
michael@0 | 88 | /*- |
michael@0 | 89 | * Macros for type conversion: |
michael@0 | 90 | * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type. |
michael@0 | 91 | * dtom(x) -- Convert data pointer within mbuf to mbuf pointer (XXX). |
michael@0 | 92 | */ |
michael@0 | 93 | #define mtod(m, t) ((t)((m)->m_data)) |
michael@0 | 94 | #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1))) |
michael@0 | 95 | |
michael@0 | 96 | struct mb_args { |
michael@0 | 97 | int flags; /* Flags for mbuf being allocated */ |
michael@0 | 98 | short type; /* Type of mbuf being allocated */ |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | struct clust_args { |
michael@0 | 102 | struct mbuf * parent_mbuf; |
michael@0 | 103 | }; |
michael@0 | 104 | |
michael@0 | 105 | struct mbuf * m_split(struct mbuf *, int, int); |
michael@0 | 106 | void m_cat(struct mbuf *m, struct mbuf *n); |
michael@0 | 107 | void m_adj(struct mbuf *, int); |
michael@0 | 108 | void mb_free_ext(struct mbuf *); |
michael@0 | 109 | void m_freem(struct mbuf *); |
michael@0 | 110 | struct m_tag *m_tag_alloc(u_int32_t, int, int, int); |
michael@0 | 111 | struct mbuf *m_copym(struct mbuf *, int, int, int); |
michael@0 | 112 | void m_copyback(struct mbuf *, int, int, caddr_t); |
michael@0 | 113 | struct mbuf *m_pullup(struct mbuf *, int); |
michael@0 | 114 | struct mbuf *m_pulldown(struct mbuf *, int off, int len, int *offp); |
michael@0 | 115 | int m_dup_pkthdr(struct mbuf *, struct mbuf *, int); |
michael@0 | 116 | struct m_tag *m_tag_copy(struct m_tag *, int); |
michael@0 | 117 | int m_tag_copy_chain(struct mbuf *, struct mbuf *, int); |
michael@0 | 118 | struct mbuf *m_prepend(struct mbuf *, int, int); |
michael@0 | 119 | void m_copydata(const struct mbuf *, int, int, caddr_t); |
michael@0 | 120 | |
michael@0 | 121 | #define MBUF_MEM_NAME "mbuf" |
michael@0 | 122 | #define MBUF_CLUSTER_MEM_NAME "mbuf_cluster" |
michael@0 | 123 | #define MBUF_EXTREFCNT_MEM_NAME "mbuf_ext_refcnt" |
michael@0 | 124 | |
michael@0 | 125 | #define MT_NOINIT 255 /* Not a type but a flag to allocate |
michael@0 | 126 | a non-initialized mbuf */ |
michael@0 | 127 | |
michael@0 | 128 | /* |
michael@0 | 129 | * General mbuf allocator statistics structure. |
michael@0 | 130 | * __Userspace__ mbstat may be useful for gathering statistics. |
michael@0 | 131 | * In the kernel many of these statistics are no longer used as |
michael@0 | 132 | * they track allocator statistics through kernel UMA's built in statistics mechanism. |
michael@0 | 133 | */ |
michael@0 | 134 | struct mbstat { |
michael@0 | 135 | u_long m_mbufs; /* XXX */ |
michael@0 | 136 | u_long m_mclusts; /* XXX */ |
michael@0 | 137 | |
michael@0 | 138 | u_long m_drain; /* times drained protocols for space */ |
michael@0 | 139 | u_long m_mcfail; /* XXX: times m_copym failed */ |
michael@0 | 140 | u_long m_mpfail; /* XXX: times m_pullup failed */ |
michael@0 | 141 | u_long m_msize; /* length of an mbuf */ |
michael@0 | 142 | u_long m_mclbytes; /* length of an mbuf cluster */ |
michael@0 | 143 | u_long m_minclsize; /* min length of data to allocate a cluster */ |
michael@0 | 144 | u_long m_mlen; /* length of data in an mbuf */ |
michael@0 | 145 | u_long m_mhlen; /* length of data in a header mbuf */ |
michael@0 | 146 | |
michael@0 | 147 | /* Number of mbtypes (gives # elems in mbtypes[] array: */ |
michael@0 | 148 | short m_numtypes; |
michael@0 | 149 | |
michael@0 | 150 | /* XXX: Sendfile stats should eventually move to their own struct */ |
michael@0 | 151 | u_long sf_iocnt; /* times sendfile had to do disk I/O */ |
michael@0 | 152 | u_long sf_allocfail; /* times sfbuf allocation failed */ |
michael@0 | 153 | u_long sf_allocwait; /* times sfbuf allocation had to wait */ |
michael@0 | 154 | }; |
michael@0 | 155 | |
michael@0 | 156 | |
michael@0 | 157 | /* |
michael@0 | 158 | * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead. |
michael@0 | 159 | * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in |
michael@0 | 160 | * sys/param.h), which has no additional overhead and is used instead of the |
michael@0 | 161 | * internal data area; this is done when at least MINCLSIZE of data must be |
michael@0 | 162 | * stored. Additionally, it is possible to allocate a separate buffer |
michael@0 | 163 | * externally and attach it to the mbuf in a way similar to that of mbuf |
michael@0 | 164 | * clusters. |
michael@0 | 165 | */ |
michael@0 | 166 | #define MLEN ((int)(MSIZE - sizeof(struct m_hdr))) /* normal data len */ |
michael@0 | 167 | #define MHLEN ((int)(MLEN - sizeof(struct pkthdr))) /* data len w/pkthdr */ |
michael@0 | 168 | #define MINCLSIZE ((int)(MHLEN + 1)) /* smallest amount to put in cluster */ |
michael@0 | 169 | #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */ |
michael@0 | 170 | |
michael@0 | 171 | |
michael@0 | 172 | /* |
michael@0 | 173 | * Header present at the beginning of every mbuf. |
michael@0 | 174 | */ |
michael@0 | 175 | struct m_hdr { |
michael@0 | 176 | struct mbuf *mh_next; /* next buffer in chain */ |
michael@0 | 177 | struct mbuf *mh_nextpkt; /* next chain in queue/record */ |
michael@0 | 178 | caddr_t mh_data; /* location of data */ |
michael@0 | 179 | int mh_len; /* amount of data in this mbuf */ |
michael@0 | 180 | int mh_flags; /* flags; see below */ |
michael@0 | 181 | short mh_type; /* type of data in this mbuf */ |
michael@0 | 182 | uint8_t pad[M_HDR_PAD];/* word align */ |
michael@0 | 183 | }; |
michael@0 | 184 | |
michael@0 | 185 | /* |
michael@0 | 186 | * Packet tag structure (see below for details). |
michael@0 | 187 | */ |
michael@0 | 188 | struct m_tag { |
michael@0 | 189 | SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */ |
michael@0 | 190 | u_int16_t m_tag_id; /* Tag ID */ |
michael@0 | 191 | u_int16_t m_tag_len; /* Length of data */ |
michael@0 | 192 | u_int32_t m_tag_cookie; /* ABI/Module ID */ |
michael@0 | 193 | void (*m_tag_free)(struct m_tag *); |
michael@0 | 194 | }; |
michael@0 | 195 | |
michael@0 | 196 | /* |
michael@0 | 197 | * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set. |
michael@0 | 198 | */ |
michael@0 | 199 | struct pkthdr { |
michael@0 | 200 | struct ifnet *rcvif; /* rcv interface */ |
michael@0 | 201 | /* variables for ip and tcp reassembly */ |
michael@0 | 202 | void *header; /* pointer to packet header */ |
michael@0 | 203 | int len; /* total packet length */ |
michael@0 | 204 | /* variables for hardware checksum */ |
michael@0 | 205 | int csum_flags; /* flags regarding checksum */ |
michael@0 | 206 | int csum_data; /* data field used by csum routines */ |
michael@0 | 207 | u_int16_t tso_segsz; /* TSO segment size */ |
michael@0 | 208 | u_int16_t ether_vtag; /* Ethernet 802.1p+q vlan tag */ |
michael@0 | 209 | SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ |
michael@0 | 210 | }; |
michael@0 | 211 | |
michael@0 | 212 | /* |
michael@0 | 213 | * Description of external storage mapped into mbuf; valid only if M_EXT is |
michael@0 | 214 | * set. |
michael@0 | 215 | */ |
michael@0 | 216 | struct m_ext { |
michael@0 | 217 | caddr_t ext_buf; /* start of buffer */ |
michael@0 | 218 | void (*ext_free) /* free routine if not the usual */ |
michael@0 | 219 | (void *, void *); |
michael@0 | 220 | void *ext_args; /* optional argument pointer */ |
michael@0 | 221 | u_int ext_size; /* size of buffer, for ext_free */ |
michael@0 | 222 | volatile u_int *ref_cnt; /* pointer to ref count info */ |
michael@0 | 223 | int ext_type; /* type of external storage */ |
michael@0 | 224 | }; |
michael@0 | 225 | |
michael@0 | 226 | |
michael@0 | 227 | /* |
michael@0 | 228 | * The core of the mbuf object along with some shortcut defined for practical |
michael@0 | 229 | * purposes. |
michael@0 | 230 | */ |
michael@0 | 231 | struct mbuf { |
michael@0 | 232 | struct m_hdr m_hdr; |
michael@0 | 233 | union { |
michael@0 | 234 | struct { |
michael@0 | 235 | struct pkthdr MH_pkthdr; /* M_PKTHDR set */ |
michael@0 | 236 | union { |
michael@0 | 237 | struct m_ext MH_ext; /* M_EXT set */ |
michael@0 | 238 | char MH_databuf[MHLEN]; |
michael@0 | 239 | } MH_dat; |
michael@0 | 240 | } MH; |
michael@0 | 241 | char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */ |
michael@0 | 242 | } M_dat; |
michael@0 | 243 | }; |
michael@0 | 244 | |
michael@0 | 245 | #define m_next m_hdr.mh_next |
michael@0 | 246 | #define m_len m_hdr.mh_len |
michael@0 | 247 | #define m_data m_hdr.mh_data |
michael@0 | 248 | #define m_type m_hdr.mh_type |
michael@0 | 249 | #define m_flags m_hdr.mh_flags |
michael@0 | 250 | #define m_nextpkt m_hdr.mh_nextpkt |
michael@0 | 251 | #define m_act m_nextpkt |
michael@0 | 252 | #define m_pkthdr M_dat.MH.MH_pkthdr |
michael@0 | 253 | #define m_ext M_dat.MH.MH_dat.MH_ext |
michael@0 | 254 | #define m_pktdat M_dat.MH.MH_dat.MH_databuf |
michael@0 | 255 | #define m_dat M_dat.M_databuf |
michael@0 | 256 | |
michael@0 | 257 | |
michael@0 | 258 | /* |
michael@0 | 259 | * mbuf flags. |
michael@0 | 260 | */ |
michael@0 | 261 | #define M_EXT 0x0001 /* has associated external storage */ |
michael@0 | 262 | #define M_PKTHDR 0x0002 /* start of record */ |
michael@0 | 263 | #define M_EOR 0x0004 /* end of record */ |
michael@0 | 264 | #define M_RDONLY 0x0008 /* associated data is marked read-only */ |
michael@0 | 265 | #define M_PROTO1 0x0010 /* protocol-specific */ |
michael@0 | 266 | #define M_PROTO2 0x0020 /* protocol-specific */ |
michael@0 | 267 | #define M_PROTO3 0x0040 /* protocol-specific */ |
michael@0 | 268 | #define M_PROTO4 0x0080 /* protocol-specific */ |
michael@0 | 269 | #define M_PROTO5 0x0100 /* protocol-specific */ |
michael@0 | 270 | #define M_SKIP_FIREWALL 0x4000 /* skip firewall processing */ |
michael@0 | 271 | #define M_FREELIST 0x8000 /* mbuf is on the free list */ |
michael@0 | 272 | |
michael@0 | 273 | |
michael@0 | 274 | /* |
michael@0 | 275 | * Flags copied when copying m_pkthdr. |
michael@0 | 276 | */ |
michael@0 | 277 | #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\ |
michael@0 | 278 | M_PROTO3|M_PROTO4|M_PROTO5|M_SKIP_FIREWALL|\ |
michael@0 | 279 | M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\ |
michael@0 | 280 | M_VLANTAG|M_PROMISC) |
michael@0 | 281 | |
michael@0 | 282 | |
michael@0 | 283 | /* |
michael@0 | 284 | * mbuf pkthdr flags (also stored in m_flags). |
michael@0 | 285 | */ |
michael@0 | 286 | #define M_BCAST 0x0200 /* send/received as link-level broadcast */ |
michael@0 | 287 | #define M_MCAST 0x0400 /* send/received as link-level multicast */ |
michael@0 | 288 | #define M_FRAG 0x0800 /* packet is a fragment of a larger packet */ |
michael@0 | 289 | #define M_FIRSTFRAG 0x1000 /* packet is first fragment */ |
michael@0 | 290 | #define M_LASTFRAG 0x2000 /* packet is last fragment */ |
michael@0 | 291 | #define M_VLANTAG 0x10000 /* ether_vtag is valid */ |
michael@0 | 292 | #define M_PROMISC 0x20000 /* packet was not for us */ |
michael@0 | 293 | #define M_NOFREE 0x40000 /* do not free mbuf - it is embedded in the cluster */ |
michael@0 | 294 | |
michael@0 | 295 | |
michael@0 | 296 | /* |
michael@0 | 297 | * External buffer types: identify ext_buf type. |
michael@0 | 298 | */ |
michael@0 | 299 | #define EXT_CLUSTER 1 /* mbuf cluster */ |
michael@0 | 300 | #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */ |
michael@0 | 301 | #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */ |
michael@0 | 302 | #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */ |
michael@0 | 303 | #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */ |
michael@0 | 304 | #define EXT_PACKET 6 /* mbuf+cluster from packet zone */ |
michael@0 | 305 | #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */ |
michael@0 | 306 | #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */ |
michael@0 | 307 | #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */ |
michael@0 | 308 | #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */ |
michael@0 | 309 | #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */ |
michael@0 | 310 | |
michael@0 | 311 | |
michael@0 | 312 | /* |
michael@0 | 313 | * mbuf types. |
michael@0 | 314 | */ |
michael@0 | 315 | #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */ |
michael@0 | 316 | #define MT_DATA 1 /* dynamic (data) allocation */ |
michael@0 | 317 | #define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */ |
michael@0 | 318 | #define MT_SONAME 8 /* socket name */ |
michael@0 | 319 | #define MT_CONTROL 14 /* extra-data protocol message */ |
michael@0 | 320 | #define MT_OOBDATA 15 /* expedited data */ |
michael@0 | 321 | #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */ |
michael@0 | 322 | |
michael@0 | 323 | #define MT_NOINIT 255 /* Not a type but a flag to allocate |
michael@0 | 324 | a non-initialized mbuf */ |
michael@0 | 325 | |
michael@0 | 326 | /* |
michael@0 | 327 | * __Userspace__ flags like M_NOWAIT are defined in malloc.h |
michael@0 | 328 | * Flags like these are used in functions like uma_zalloc() |
michael@0 | 329 | * but don't have an equivalent in userland umem |
michael@0 | 330 | * Flags specifying how an allocation should be made. |
michael@0 | 331 | * |
michael@0 | 332 | * The flag to use is as follows: |
michael@0 | 333 | * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation. |
michael@0 | 334 | * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block. |
michael@0 | 335 | * |
michael@0 | 336 | * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and |
michael@0 | 337 | * if we cannot allocate immediately we may return NULL, whereas |
michael@0 | 338 | * M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate resources we |
michael@0 | 339 | * will block until they are available, and thus never return NULL. |
michael@0 | 340 | * |
michael@0 | 341 | * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT. |
michael@0 | 342 | */ |
michael@0 | 343 | #define MBTOM(how) (how) |
michael@0 | 344 | |
michael@0 | 345 | void m_tag_delete(struct mbuf *, struct m_tag *); |
michael@0 | 346 | void m_tag_delete_chain(struct mbuf *, struct m_tag *); |
michael@0 | 347 | void m_move_pkthdr(struct mbuf *, struct mbuf *); |
michael@0 | 348 | void m_tag_free_default(struct m_tag *); |
michael@0 | 349 | |
michael@0 | 350 | extern int max_linkhdr; /* Largest link-level header */ |
michael@0 | 351 | extern int max_protohdr; /* Size of largest protocol layer header. See user_mbuf.c */ |
michael@0 | 352 | |
michael@0 | 353 | extern struct mbstat mbstat; /* General mbuf stats/infos */ |
michael@0 | 354 | |
michael@0 | 355 | |
michael@0 | 356 | /* |
michael@0 | 357 | * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can |
michael@0 | 358 | * be both the local data payload, or an external buffer area, depending on |
michael@0 | 359 | * whether M_EXT is set). |
michael@0 | 360 | */ |
michael@0 | 361 | #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \ |
michael@0 | 362 | (!(((m)->m_flags & M_EXT)) || \ |
michael@0 | 363 | (*((m)->m_ext.ref_cnt) == 1)) ) \ |
michael@0 | 364 | |
michael@0 | 365 | |
michael@0 | 366 | /* |
michael@0 | 367 | * Compute the amount of space available before the current start of data in |
michael@0 | 368 | * an mbuf. |
michael@0 | 369 | * |
michael@0 | 370 | * The M_WRITABLE() is a temporary, conservative safety measure: the burden |
michael@0 | 371 | * of checking writability of the mbuf data area rests solely with the caller. |
michael@0 | 372 | */ |
michael@0 | 373 | #define M_LEADINGSPACE(m) \ |
michael@0 | 374 | ((m)->m_flags & M_EXT ? \ |
michael@0 | 375 | (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \ |
michael@0 | 376 | (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat : \ |
michael@0 | 377 | (m)->m_data - (m)->m_dat) |
michael@0 | 378 | |
michael@0 | 379 | /* |
michael@0 | 380 | * Compute the amount of space available after the end of data in an mbuf. |
michael@0 | 381 | * |
michael@0 | 382 | * The M_WRITABLE() is a temporary, conservative safety measure: the burden |
michael@0 | 383 | * of checking writability of the mbuf data area rests solely with the caller. |
michael@0 | 384 | */ |
michael@0 | 385 | #define M_TRAILINGSPACE(m) \ |
michael@0 | 386 | ((m)->m_flags & M_EXT ? \ |
michael@0 | 387 | (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \ |
michael@0 | 388 | - ((m)->m_data + (m)->m_len) : 0) : \ |
michael@0 | 389 | &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len)) |
michael@0 | 390 | |
michael@0 | 391 | |
michael@0 | 392 | |
michael@0 | 393 | /* |
michael@0 | 394 | * Arrange to prepend space of size plen to mbuf m. If a new mbuf must be |
michael@0 | 395 | * allocated, how specifies whether to wait. If the allocation fails, the |
michael@0 | 396 | * original mbuf chain is freed and m is set to NULL. |
michael@0 | 397 | */ |
michael@0 | 398 | #define M_PREPEND(m, plen, how) do { \ |
michael@0 | 399 | struct mbuf **_mmp = &(m); \ |
michael@0 | 400 | struct mbuf *_mm = *_mmp; \ |
michael@0 | 401 | int _mplen = (plen); \ |
michael@0 | 402 | int __mhow = (how); \ |
michael@0 | 403 | \ |
michael@0 | 404 | if (M_LEADINGSPACE(_mm) >= _mplen) { \ |
michael@0 | 405 | _mm->m_data -= _mplen; \ |
michael@0 | 406 | _mm->m_len += _mplen; \ |
michael@0 | 407 | } else \ |
michael@0 | 408 | _mm = m_prepend(_mm, _mplen, __mhow); \ |
michael@0 | 409 | if (_mm != NULL && _mm->m_flags & M_PKTHDR) \ |
michael@0 | 410 | _mm->m_pkthdr.len += _mplen; \ |
michael@0 | 411 | *_mmp = _mm; \ |
michael@0 | 412 | } while (0) |
michael@0 | 413 | |
michael@0 | 414 | /* |
michael@0 | 415 | * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an |
michael@0 | 416 | * object of the specified size at the end of the mbuf, longword aligned. |
michael@0 | 417 | */ |
michael@0 | 418 | #define M_ALIGN(m, len) do { \ |
michael@0 | 419 | KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \ |
michael@0 | 420 | ("%s: M_ALIGN not normal mbuf", __func__)); \ |
michael@0 | 421 | KASSERT((m)->m_data == (m)->m_dat, \ |
michael@0 | 422 | ("%s: M_ALIGN not a virgin mbuf", __func__)); \ |
michael@0 | 423 | (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \ |
michael@0 | 424 | } while (0) |
michael@0 | 425 | |
michael@0 | 426 | /* |
michael@0 | 427 | * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by |
michael@0 | 428 | * M_DUP/MOVE_PKTHDR. |
michael@0 | 429 | */ |
michael@0 | 430 | #define MH_ALIGN(m, len) do { \ |
michael@0 | 431 | KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \ |
michael@0 | 432 | ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \ |
michael@0 | 433 | KASSERT((m)->m_data == (m)->m_pktdat, \ |
michael@0 | 434 | ("%s: MH_ALIGN not a virgin mbuf", __func__)); \ |
michael@0 | 435 | (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \ |
michael@0 | 436 | } while (0) |
michael@0 | 437 | |
michael@0 | 438 | #endif |