1/*
2 * Copyright 2009, Colin Günther, coling@gmx.de.
3 * Copyright 2007, Hugo Santos. All Rights Reserved.
4 * Distributed under the terms of the MIT License.
5 */
6#ifndef _FBSD_COMPAT_SYS_MBUF_H_
7#define _FBSD_COMPAT_SYS_MBUF_H_
8
9
10#include <sys/queue.h>
11#include <sys/systm.h>
12#include <vm/uma.h>
13
14
15#define MLEN		((int)(MSIZE - sizeof(struct m_hdr)))
16#define MHLEN		((int)(MLEN - sizeof(struct pkthdr)))
17
18#define MINCLSIZE	(MHLEN + 1)
19
20#define MBTOM(how)	(how)
21#define M_DONTWAIT	M_NOWAIT
22#define M_TRYWAIT	M_WAITOK
23#define M_WAIT		M_WAITOK
24
25#define MT_DATA		1
26
27#define M_EXT		0x00000001
28#define M_PKTHDR	0x00000002
29#define M_RDONLY	0x00000008
30#define M_PROTO1	0x00000010
31#define M_PROTO2	0x00000020
32#define M_PROTO3	0x00000040
33#define M_PROTO4	0x00000080
34#define M_PROTO5	0x00000100
35#define M_BCAST		0x00000200
36#define M_MCAST		0x00000400
37#define M_FRAG		0x00000800
38#define M_FIRSTFRAG	0x00001000
39#define M_LASTFRAG	0x00002000
40#define M_VLANTAG	0x00010000
41#define M_PROTO6	0x00080000
42#define M_PROTO7	0x00100000
43#define M_PROTO8	0x00200000
44
45#define M_COPYFLAGS (M_PKTHDR | M_RDONLY | M_BCAST | M_MCAST | M_FRAG \
46	| M_FIRSTFRAG | M_LASTFRAG | M_VLANTAG)
47	// Flags preserved when copying m_pkthdr
48
49#define M_MOVE_PKTHDR(to, from)	m_move_pkthdr((to), (from))
50#define MGET(m, how, type)		((m) = m_get((how), (type)))
51#define MGETHDR(m, how, type)	((m) = m_gethdr((how), (type)))
52#define MCLGET(m, how)			m_clget((m), (how))
53
54#define mtod(m, type)	((type)((m)->m_data))
55
56// Check if the supplied mbuf has a packet header, or else panic.
57#define M_ASSERTPKTHDR(m) KASSERT(m != NULL && m->m_flags & M_PKTHDR, \
58	("%s: no mbuf packet header!", __func__))
59
60#define MBUF_CHECKSLEEP(how) do { } while (0)
61
62#define MTAG_PERSISTENT	0x800
63
64#define EXT_CLUSTER		1		// 2048 bytes
65#define EXT_JUMBOP		4		// Page size
66#define EXT_JUMBO9		5		// 9 * 1024 bytes
67#define EXT_NET_DRV		100		// custom ext_buf provided by net driver
68
69#define CSUM_IP			0x0001
70#define CSUM_TCP		0x0002
71#define CSUM_UDP		0x0004
72#define CSUM_TSO		0x0020
73#define CSUM_IP_CHECKED	0x0100
74#define CSUM_IP_VALID	0x0200
75#define CSUM_DATA_VALID	0x0400
76#define CSUM_PSEUDO_HDR	0x0800
77#define CSUM_DELAY_DATA	(CSUM_TCP | CSUM_UDP)
78
79// TODO After all network driver are updated to the FreeBSD 8 version this can
80// changed
81#if __FreeBSD_version__ >= 8
82#define MEXTADD(m, buf, size, free, arg1, arg2, flags, type) \
83	m_extadd((m), (caddr_t)(buf), (size), (free),(arg1),(arg2),(flags), (type))
84#else
85#define MEXTADD(m, buf, size, free, args, flags, type) \
86	m_extadd((m), (caddr_t)(buf), (size), (free),(args),(flags), (type))
87#endif
88
89
90extern int max_linkhdr;
91extern int max_protohdr;
92extern int max_hdr;
93extern int max_datalen;		// MHLEN - max_hdr
94
95
96struct m_hdr {
97	struct mbuf*	mh_next;
98	struct mbuf*	mh_nextpkt;
99	caddr_t			mh_data;
100	int				mh_len;
101	int				mh_flags;
102	short			mh_type;
103};
104
105struct pkthdr {
106	struct ifnet*					rcvif;
107	int								len;
108	int								csum_flags;
109	int								csum_data;
110	uint16_t						tso_segsz;
111	uint16_t						ether_vtag;
112	SLIST_HEAD(packet_tags, m_tag)	tags;
113};
114
115struct m_tag {
116	SLIST_ENTRY(m_tag)	m_tag_link;		// List of packet tags
117	u_int16_t			m_tag_id;		// Tag ID
118	u_int16_t			m_tag_len;		// Length of data
119	u_int32_t			m_tag_cookie;	// ABI/Module ID
120	void				(*m_tag_free)(struct m_tag*);
121};
122
123struct m_ext {
124	caddr_t			ext_buf;
125	unsigned int	ext_size;
126	int				ext_type;
127};
128
129struct mbuf {
130	struct m_hdr m_hdr;
131	union {
132		struct {
133			struct pkthdr	MH_pkthdr;
134			union {
135				struct m_ext	MH_ext;
136				char			MH_databuf[MHLEN];
137			} MH_dat;
138		} MH;
139		char M_databuf[MLEN];
140	} M_dat;
141};
142
143
144#define m_next		m_hdr.mh_next
145#define m_len		m_hdr.mh_len
146#define m_data		m_hdr.mh_data
147#define m_type		m_hdr.mh_type
148#define m_flags		m_hdr.mh_flags
149#define m_nextpkt	m_hdr.mh_nextpkt
150#define m_act		m_nextpkt
151#define m_pkthdr	M_dat.MH.MH_pkthdr
152#define m_ext		M_dat.MH.MH_dat.MH_ext
153#define m_pktdat	M_dat.MH.MH_dat.MH_databuf
154#define m_dat		M_dat.M_databuf
155
156
157void			m_adj(struct mbuf*, int);
158void			m_align(struct mbuf*, int);
159int				m_append(struct mbuf*, int, c_caddr_t);
160void			m_cat(struct mbuf*, struct mbuf*);
161void			m_clget(struct mbuf*, int);
162void*			m_cljget(struct mbuf*, int, int);
163struct mbuf*	m_collapse(struct mbuf*, int, int);
164void			m_copyback(struct mbuf*, int, int, caddr_t);
165void			m_copydata(const struct mbuf*, int, int, caddr_t);
166struct mbuf*	m_copypacket(struct mbuf*, int);
167struct mbuf*	m_defrag(struct mbuf*, int);
168struct mbuf*	m_devget(char*, int, int, struct ifnet*,
169	void(*) (char*, caddr_t, u_int));
170
171struct mbuf*	m_dup(struct mbuf*, int);
172int				m_dup_pkthdr(struct mbuf*, struct mbuf*, int);
173
174// TODO After all network driver are updated to the FreeBSD 8 version this can
175// changed
176#if __FreeBSD_version__ >= 8
177void			m_extadd(struct mbuf*, caddr_t, u_int, void(*) (void*, void*),
178	void*, void*, int, int);
179#else
180void 			m_extadd(struct mbuf*, caddr_t, u_int, void(*) (void*, void*),
181	void*, int, int);
182#endif
183
184u_int			m_fixhdr(struct mbuf*);
185struct mbuf*	m_free(struct mbuf*);
186void			m_freem(struct mbuf*);
187struct mbuf*	m_get(int, short);
188struct mbuf*	m_gethdr(int, short);
189struct mbuf*	m_getjcl(int, short, int, int);
190u_int			m_length(struct mbuf*, struct mbuf**);
191struct mbuf*	m_getcl(int, short, int);
192void			m_move_pkthdr(struct mbuf*, struct mbuf*);
193struct mbuf*	m_prepend(struct mbuf*, int, int);
194struct mbuf*	m_pulldown(struct mbuf*, int, int, int*);
195struct mbuf*	m_pullup(struct mbuf*, int);
196struct mbuf*	m_split(struct mbuf*, int, int);
197struct mbuf*	m_unshare(struct mbuf*, int);
198
199struct m_tag*	m_tag_alloc(u_int32_t, int, int, int);
200void			m_tag_delete(struct mbuf*, struct m_tag*);
201void			m_tag_delete_chain(struct mbuf*, struct m_tag*);
202void			m_tag_free_default(struct m_tag*);
203struct m_tag*	m_tag_locate(struct mbuf*, u_int32_t, int, struct m_tag*);
204struct m_tag*	m_tag_copy(struct m_tag*, int);
205int				m_tag_copy_chain(struct mbuf*, struct mbuf*, int);
206void			m_tag_delete_nonpersistent(struct mbuf*);
207
208
209static inline void
210m_tag_setup(struct m_tag* tagPointer, u_int32_t cookie, int type, int length)
211{
212	tagPointer->m_tag_id = type;
213	tagPointer->m_tag_len = length;
214	tagPointer->m_tag_cookie = cookie;
215}
216
217
218static inline void
219m_tag_free(struct m_tag* tag)
220{
221	(*tag->m_tag_free)(tag);
222}
223
224
225static inline void
226m_tag_prepend(struct mbuf* memoryBuffer, struct m_tag* tag)
227{
228	SLIST_INSERT_HEAD(&memoryBuffer->m_pkthdr.tags, tag, m_tag_link);
229}
230
231
232static inline void
233m_tag_unlink(struct mbuf* memoryBuffer, struct m_tag* tag)
234{
235	SLIST_REMOVE(&memoryBuffer->m_pkthdr.tags, tag, m_tag, m_tag_link);
236}
237
238
239#include <sys/mbuf-fbsd.h>
240
241#endif	/* _FBSD_COMPAT_SYS_MBUF_H_ */
242