user_mbuf.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*-
  2. * Copyright (c) 1982, 1986, 1988, 1993
  3. * The Regents of the University of California.
  4. * All rights reserved.
  5. *
  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. #ifndef _USER_MBUF_H_
  32. #define _USER_MBUF_H_
  33. /* __Userspace__ header file for mbufs */
  34. #include <stdio.h>
  35. #if !defined(SCTP_SIMPLE_ALLOCATOR)
  36. #include "umem.h"
  37. #endif
  38. #include "user_malloc.h"
  39. #include "netinet/sctp_os_userspace.h"
  40. #define USING_MBUF_CONSTRUCTOR 0
  41. /* For Linux */
  42. #ifndef MSIZE
  43. #define MSIZE 256
  44. /* #define MSIZE 1024 */
  45. #endif
  46. #ifndef MCLBYTES
  47. #define MCLBYTES 2048
  48. #endif
  49. struct mbuf * m_gethdr(int how, short type);
  50. struct mbuf * m_get(int how, short type);
  51. struct mbuf * m_free(struct mbuf *m);
  52. void m_clget(struct mbuf *m, int how);
  53. struct mbuf * m_getm2(struct mbuf *m, int len, int how, short type, int flags, int allonebuf);
  54. struct mbuf *m_uiotombuf(struct uio *uio, int how, int len, int align, int flags);
  55. u_int m_length(struct mbuf *m0, struct mbuf **last);
  56. struct mbuf *m_last(struct mbuf *m);
  57. /* mbuf initialization function */
  58. void mbuf_initialize(void *);
  59. #define M_MOVE_PKTHDR(to, from) m_move_pkthdr((to), (from))
  60. #define MGET(m, how, type) ((m) = m_get((how), (type)))
  61. #define MGETHDR(m, how, type) ((m) = m_gethdr((how), (type)))
  62. #define MCLGET(m, how) m_clget((m), (how))
  63. #define M_HDR_PAD ((sizeof(intptr_t)==4) ? 2 : 6) /* modified for __Userspace__ */
  64. /* Length to m_copy to copy all. */
  65. #define M_COPYALL 1000000000
  66. /* umem_cache_t is defined in user_include/umem.h as
  67. * typedef struct umem_cache umem_cache_t;
  68. * Note:umem_zone_t is a pointer.
  69. */
  70. #if defined(SCTP_SIMPLE_ALLOCATOR)
  71. typedef size_t sctp_zone_t;
  72. #else
  73. typedef umem_cache_t *sctp_zone_t;
  74. #endif
  75. extern sctp_zone_t zone_mbuf;
  76. extern sctp_zone_t zone_clust;
  77. extern sctp_zone_t zone_ext_refcnt;
  78. /*-
  79. * Macros for type conversion:
  80. * mtod(m, t) -- Convert mbuf pointer to data pointer of correct type.
  81. * dtom(x) -- Convert data pointer within mbuf to mbuf pointer (XXX).
  82. */
  83. #define mtod(m, t) ((t)((m)->m_data))
  84. #define dtom(x) ((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
  85. struct mb_args {
  86. int flags; /* Flags for mbuf being allocated */
  87. short type; /* Type of mbuf being allocated */
  88. };
  89. struct clust_args {
  90. struct mbuf * parent_mbuf;
  91. };
  92. struct mbuf * m_split(struct mbuf *, int, int);
  93. void m_cat(struct mbuf *m, struct mbuf *n);
  94. void m_adj(struct mbuf *, int);
  95. void mb_free_ext(struct mbuf *);
  96. void m_freem(struct mbuf *);
  97. struct m_tag *m_tag_alloc(uint32_t, int, int, int);
  98. struct mbuf *m_copym(struct mbuf *, int, int, int);
  99. void m_copyback(struct mbuf *, int, int, caddr_t);
  100. int m_apply(struct mbuf *, int, int, int (*)(void *, void *, u_int), void *arg);
  101. struct mbuf *m_pullup(struct mbuf *, int);
  102. struct mbuf *m_pulldown(struct mbuf *, int off, int len, int *offp);
  103. int m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
  104. struct m_tag *m_tag_copy(struct m_tag *, int);
  105. int m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
  106. struct mbuf *m_prepend(struct mbuf *, int, int);
  107. void m_copydata(const struct mbuf *, int, int, caddr_t);
  108. #define MBUF_MEM_NAME "mbuf"
  109. #define MBUF_CLUSTER_MEM_NAME "mbuf_cluster"
  110. #define MBUF_EXTREFCNT_MEM_NAME "mbuf_ext_refcnt"
  111. /*
  112. * Mbufs are of a single size, MSIZE (sys/param.h), which includes overhead.
  113. * An mbuf may add a single "mbuf cluster" of size MCLBYTES (also in
  114. * sys/param.h), which has no additional overhead and is used instead of the
  115. * internal data area; this is done when at least MINCLSIZE of data must be
  116. * stored. Additionally, it is possible to allocate a separate buffer
  117. * externally and attach it to the mbuf in a way similar to that of mbuf
  118. * clusters.
  119. */
  120. #define MLEN ((int)(MSIZE - sizeof(struct m_hdr))) /* normal data len */
  121. #define MHLEN ((int)(MLEN - sizeof(struct pkthdr))) /* data len w/pkthdr */
  122. #define MINCLSIZE ((int)(MHLEN + 1)) /* smallest amount to put in cluster */
  123. #define M_MAXCOMPRESS (MHLEN / 2) /* max amount to copy for compression */
  124. /*
  125. * Header present at the beginning of every mbuf.
  126. */
  127. struct m_hdr {
  128. struct mbuf *mh_next; /* next buffer in chain */
  129. struct mbuf *mh_nextpkt; /* next chain in queue/record */
  130. caddr_t mh_data; /* location of data */
  131. int mh_len; /* amount of data in this mbuf */
  132. int mh_flags; /* flags; see below */
  133. short mh_type; /* type of data in this mbuf */
  134. uint8_t pad[M_HDR_PAD];/* word align */
  135. };
  136. /*
  137. * Packet tag structure (see below for details).
  138. */
  139. struct m_tag {
  140. SLIST_ENTRY(m_tag) m_tag_link; /* List of packet tags */
  141. uint16_t m_tag_id; /* Tag ID */
  142. uint16_t m_tag_len; /* Length of data */
  143. uint32_t m_tag_cookie; /* ABI/Module ID */
  144. void (*m_tag_free)(struct m_tag *);
  145. };
  146. /*
  147. * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
  148. */
  149. struct pkthdr {
  150. struct ifnet *rcvif; /* rcv interface */
  151. /* variables for ip and tcp reassembly */
  152. void *header; /* pointer to packet header */
  153. int len; /* total packet length */
  154. /* variables for hardware checksum */
  155. int csum_flags; /* flags regarding checksum */
  156. int csum_data; /* data field used by csum routines */
  157. uint16_t tso_segsz; /* TSO segment size */
  158. uint16_t ether_vtag; /* Ethernet 802.1p+q vlan tag */
  159. SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
  160. };
  161. /*
  162. * Description of external storage mapped into mbuf; valid only if M_EXT is
  163. * set.
  164. */
  165. struct m_ext {
  166. caddr_t ext_buf; /* start of buffer */
  167. void (*ext_free) /* free routine if not the usual */
  168. (void *, void *);
  169. void *ext_args; /* optional argument pointer */
  170. u_int ext_size; /* size of buffer, for ext_free */
  171. volatile u_int *ref_cnt; /* pointer to ref count info */
  172. int ext_type; /* type of external storage */
  173. };
  174. /*
  175. * The core of the mbuf object along with some shortcut defined for practical
  176. * purposes.
  177. */
  178. struct mbuf {
  179. struct m_hdr m_hdr;
  180. union {
  181. struct {
  182. struct pkthdr MH_pkthdr; /* M_PKTHDR set */
  183. union {
  184. struct m_ext MH_ext; /* M_EXT set */
  185. char MH_databuf[MHLEN];
  186. } MH_dat;
  187. } MH;
  188. char M_databuf[MLEN]; /* !M_PKTHDR, !M_EXT */
  189. } M_dat;
  190. };
  191. #define m_next m_hdr.mh_next
  192. #define m_len m_hdr.mh_len
  193. #define m_data m_hdr.mh_data
  194. #define m_type m_hdr.mh_type
  195. #define m_flags m_hdr.mh_flags
  196. #define m_nextpkt m_hdr.mh_nextpkt
  197. #define m_act m_nextpkt
  198. #define m_pkthdr M_dat.MH.MH_pkthdr
  199. #define m_ext M_dat.MH.MH_dat.MH_ext
  200. #define m_pktdat M_dat.MH.MH_dat.MH_databuf
  201. #define m_dat M_dat.M_databuf
  202. /*
  203. * mbuf flags.
  204. */
  205. #define M_EXT 0x0001 /* has associated external storage */
  206. #define M_PKTHDR 0x0002 /* start of record */
  207. #define M_EOR 0x0004 /* end of record */
  208. #define M_RDONLY 0x0008 /* associated data is marked read-only */
  209. #define M_PROTO1 0x0010 /* protocol-specific */
  210. #define M_PROTO2 0x0020 /* protocol-specific */
  211. #define M_PROTO3 0x0040 /* protocol-specific */
  212. #define M_PROTO4 0x0080 /* protocol-specific */
  213. #define M_PROTO5 0x0100 /* protocol-specific */
  214. #define M_FREELIST 0x8000 /* mbuf is on the free list */
  215. /*
  216. * Flags copied when copying m_pkthdr.
  217. */
  218. #define M_COPYFLAGS (M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\
  219. M_PROTO3|M_PROTO4|M_PROTO5|\
  220. M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG|\
  221. M_VLANTAG|M_PROMISC)
  222. /*
  223. * mbuf pkthdr flags (also stored in m_flags).
  224. */
  225. #define M_BCAST 0x0200 /* send/received as link-level broadcast */
  226. #define M_MCAST 0x0400 /* send/received as link-level multicast */
  227. #define M_FRAG 0x0800 /* packet is a fragment of a larger packet */
  228. #define M_FIRSTFRAG 0x1000 /* packet is first fragment */
  229. #define M_LASTFRAG 0x2000 /* packet is last fragment */
  230. #define M_VLANTAG 0x10000 /* ether_vtag is valid */
  231. #define M_PROMISC 0x20000 /* packet was not for us */
  232. #define M_NOFREE 0x40000 /* do not free mbuf - it is embedded in the cluster */
  233. /*
  234. * External buffer types: identify ext_buf type.
  235. */
  236. #define EXT_CLUSTER 1 /* mbuf cluster */
  237. #define EXT_SFBUF 2 /* sendfile(2)'s sf_bufs */
  238. #define EXT_JUMBOP 3 /* jumbo cluster 4096 bytes */
  239. #define EXT_JUMBO9 4 /* jumbo cluster 9216 bytes */
  240. #define EXT_JUMBO16 5 /* jumbo cluster 16184 bytes */
  241. #define EXT_PACKET 6 /* mbuf+cluster from packet zone */
  242. #define EXT_MBUF 7 /* external mbuf reference (M_IOVEC) */
  243. #define EXT_NET_DRV 100 /* custom ext_buf provided by net driver(s) */
  244. #define EXT_MOD_TYPE 200 /* custom module's ext_buf type */
  245. #define EXT_DISPOSABLE 300 /* can throw this buffer away w/page flipping */
  246. #define EXT_EXTREF 400 /* has externally maintained ref_cnt ptr */
  247. /*
  248. * mbuf types.
  249. */
  250. #define MT_NOTMBUF 0 /* USED INTERNALLY ONLY! Object is not mbuf */
  251. #define MT_DATA 1 /* dynamic (data) allocation */
  252. #define MT_HEADER MT_DATA /* packet header, use M_PKTHDR instead */
  253. #define MT_SONAME 8 /* socket name */
  254. #define MT_CONTROL 14 /* extra-data protocol message */
  255. #define MT_OOBDATA 15 /* expedited data */
  256. #define MT_NTYPES 16 /* number of mbuf types for mbtypes[] */
  257. /*
  258. * __Userspace__ flags like M_NOWAIT are defined in malloc.h
  259. * Flags like these are used in functions like uma_zalloc()
  260. * but don't have an equivalent in userland umem
  261. * Flags specifying how an allocation should be made.
  262. *
  263. * The flag to use is as follows:
  264. * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation.
  265. * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block.
  266. *
  267. * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly and
  268. * if we cannot allocate immediately we may return NULL, whereas
  269. * M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate resources we
  270. * will block until they are available, and thus never return NULL.
  271. *
  272. * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT.
  273. */
  274. #define MBTOM(how) (how)
  275. void m_tag_delete(struct mbuf *, struct m_tag *);
  276. void m_tag_delete_chain(struct mbuf *, struct m_tag *);
  277. void m_move_pkthdr(struct mbuf *, struct mbuf *);
  278. void m_tag_free_default(struct m_tag *);
  279. extern int max_linkhdr; /* Largest link-level header */
  280. extern int max_protohdr; /* Size of largest protocol layer header. See user_mbuf.c */
  281. /*
  282. * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can
  283. * be both the local data payload, or an external buffer area, depending on
  284. * whether M_EXT is set).
  285. */
  286. #define M_WRITABLE(m) (!((m)->m_flags & M_RDONLY) && \
  287. (!(((m)->m_flags & M_EXT)) || \
  288. (*((m)->m_ext.ref_cnt) == 1)) ) \
  289. /* Check if the supplied mbuf has a packet header, or else panic. */
  290. #define M_ASSERTPKTHDR(m) \
  291. KASSERT((m) != NULL && (m)->m_flags & M_PKTHDR, \
  292. ("%s: no mbuf packet header!", __func__))
  293. /*
  294. * Compute the amount of space available before the current start of data in
  295. * an mbuf.
  296. *
  297. * The M_WRITABLE() is a temporary, conservative safety measure: the burden
  298. * of checking writability of the mbuf data area rests solely with the caller.
  299. */
  300. #define M_LEADINGSPACE(m) \
  301. (((m)->m_flags & M_EXT) ? \
  302. (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \
  303. ((m)->m_flags & M_PKTHDR)? (m)->m_data - (m)->m_pktdat : \
  304. (m)->m_data - (m)->m_dat)
  305. /*
  306. * Compute the amount of space available after the end of data in an mbuf.
  307. *
  308. * The M_WRITABLE() is a temporary, conservative safety measure: the burden
  309. * of checking writability of the mbuf data area rests solely with the caller.
  310. */
  311. #define M_TRAILINGSPACE(m) \
  312. (((m)->m_flags & M_EXT) ? \
  313. (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size \
  314. - ((m)->m_data + (m)->m_len) : 0) : \
  315. &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
  316. /*
  317. * Arrange to prepend space of size plen to mbuf m. If a new mbuf must be
  318. * allocated, how specifies whether to wait. If the allocation fails, the
  319. * original mbuf chain is freed and m is set to NULL.
  320. */
  321. #define M_PREPEND(m, plen, how) do { \
  322. struct mbuf **_mmp = &(m); \
  323. struct mbuf *_mm = *_mmp; \
  324. int _mplen = (plen); \
  325. int __mhow = (how); \
  326. \
  327. if (M_LEADINGSPACE(_mm) >= _mplen) { \
  328. _mm->m_data -= _mplen; \
  329. _mm->m_len += _mplen; \
  330. } else \
  331. _mm = m_prepend(_mm, _mplen, __mhow); \
  332. if (_mm != NULL && _mm->m_flags & M_PKTHDR) \
  333. _mm->m_pkthdr.len += _mplen; \
  334. *_mmp = _mm; \
  335. } while (0)
  336. /*
  337. * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place an
  338. * object of the specified size at the end of the mbuf, longword aligned.
  339. */
  340. #define M_ALIGN(m, len) do { \
  341. KASSERT(!((m)->m_flags & (M_PKTHDR|M_EXT)), \
  342. ("%s: M_ALIGN not normal mbuf", __func__)); \
  343. KASSERT((m)->m_data == (m)->m_dat, \
  344. ("%s: M_ALIGN not a virgin mbuf", __func__)); \
  345. (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1); \
  346. } while (0)
  347. /*
  348. * As above, for mbufs allocated with m_gethdr/MGETHDR or initialized by
  349. * M_DUP/MOVE_PKTHDR.
  350. */
  351. #define MH_ALIGN(m, len) do { \
  352. KASSERT((m)->m_flags & M_PKTHDR && !((m)->m_flags & M_EXT), \
  353. ("%s: MH_ALIGN not PKTHDR mbuf", __func__)); \
  354. KASSERT((m)->m_data == (m)->m_pktdat, \
  355. ("%s: MH_ALIGN not a virgin mbuf", __func__)); \
  356. (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1); \
  357. } while (0)
  358. #define M_SIZE(m) \
  359. (((m)->m_flags & M_EXT) ? (m)->m_ext.ext_size : \
  360. ((m)->m_flags & M_PKTHDR) ? MHLEN : \
  361. MLEN)
  362. #endif