#ifndef _SYS_MBUF_H_
#define _SYS_MBUF_H_
#include <sys/queue.h>
#define MSIZE 256
#define MLEN (MSIZE - sizeof(struct m_hdr))
#define MHLEN (MLEN - sizeof(struct pkthdr))
#define MAXMCLBYTES (64 * 1024)
#define MINCLSIZE (MHLEN + MLEN + 1)
#define MCLSHIFT 11
#define MCLBYTES (1 << MCLSHIFT)
struct m_tag {
SLIST_ENTRY(m_tag) m_tag_link;
u_int16_t m_tag_id;
u_int16_t m_tag_len;
};
#define mtod(m,t) ((t)((m)->m_data))
struct m_hdr {
struct mbuf *mh_next;
struct mbuf *mh_nextpkt;
caddr_t mh_data;
u_int mh_len;
short mh_type;
u_short mh_flags;
#ifndef __LP64__
u_int mh_pad;
#endif
};
struct pf_state_key;
struct inpcb;
struct pkthdr_pf {
struct pf_state_key *statekey;
struct inpcb *inp;
u_int32_t qid;
u_int16_t tag;
u_int16_t delay;
u_int8_t flags;
u_int8_t routed;
u_int8_t prio;
u_int8_t pad[1];
};
#define PF_TAG_GENERATED 0x01
#define PF_TAG_SYNCOOKIE_RECREATED 0x02
#define PF_TAG_TRANSLATE_LOCALHOST 0x04
#define PF_TAG_DIVERTED 0x08
#define PF_TAG_DIVERTED_PACKET 0x10
#define PF_TAG_REROUTE 0x20
#define PF_TAG_REFRAGMENTED 0x40
#define PF_TAG_PROCESSED 0x80
#ifdef _KERNEL
#define MPF_BITS \
("\20\1GENERATED\2SYNCOOKIE_RECREATED\3TRANSLATE_LOCALHOST\4DIVERTED" \
"\5DIVERTED_PACKET\6REROUTE\7REFRAGMENTED\10PROCESSED")
#endif
struct pkthdr {
void *ph_cookie;
SLIST_HEAD(, m_tag) ph_tags;
int64_t ph_timestamp;
int len;
u_int ph_rtableid;
u_int ph_ifidx;
u_int16_t ph_tagsset;
u_int16_t ph_flowid;
u_int16_t csum_flags;
u_int16_t ether_vtag;
u_int16_t ph_mss;
u_int8_t ph_loopcnt;
u_int8_t ph_family;
struct pkthdr_pf pf;
};
struct mbuf_ext {
caddr_t ext_buf;
void *ext_arg;
u_int ext_free_fn;
u_int ext_size;
struct mbuf *ext_nextref;
struct mbuf *ext_prevref;
#ifdef DEBUG
const char *ext_ofile;
const char *ext_nfile;
int ext_oline;
int ext_nline;
#endif
};
struct mbuf {
struct m_hdr m_hdr;
union {
struct {
struct pkthdr MH_pkthdr;
union {
struct mbuf_ext MH_ext;
char MH_databuf[MHLEN];
} MH_dat;
} MH;
char M_databuf[MLEN];
} M_dat;
};
#define m_next m_hdr.mh_next
#define m_len m_hdr.mh_len
#define m_data m_hdr.mh_data
#define m_type m_hdr.mh_type
#define m_flags m_hdr.mh_flags
#define m_nextpkt m_hdr.mh_nextpkt
#define m_pkthdr M_dat.MH.MH_pkthdr
#define m_ext M_dat.MH.MH_dat.MH_ext
#define m_pktdat M_dat.MH.MH_dat.MH_databuf
#define m_dat M_dat.M_databuf
#define M_EXT 0x0001
#define M_PKTHDR 0x0002
#define M_EOR 0x0004
#define M_EXTWR 0x0008
#define M_PROTO1 0x0010
#define M_VLANTAG 0x0020
#define M_LOOP 0x0040
#define M_BCAST 0x0100
#define M_MCAST 0x0200
#define M_CONF 0x0400
#define M_AUTH 0x0800
#define M_TUNNEL 0x1000
#define M_ZEROIZE 0x2000
#define M_COMP 0x4000
#define M_LINK0 0x8000
#ifdef _KERNEL
#define M_BITS \
("\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_EXTWR\5M_PROTO1\6M_VLANTAG\7M_LOOP" \
"\11M_BCAST\12M_MCAST\13M_CONF\14M_AUTH\15M_TUNNEL" \
"\16M_ZEROIZE\17M_COMP\20M_LINK0")
#endif
#define M_COPYFLAGS (M_PKTHDR|M_EOR|M_PROTO1|M_BCAST|M_MCAST|M_CONF|M_COMP|\
M_AUTH|M_LOOP|M_TUNNEL|M_LINK0|M_VLANTAG|M_ZEROIZE)
#define M_IPV4_CSUM_OUT 0x0001
#define M_TCP_CSUM_OUT 0x0002
#define M_UDP_CSUM_OUT 0x0004
#define M_IPV4_CSUM_IN_OK 0x0008
#define M_IPV4_CSUM_IN_BAD 0x0010
#define M_TCP_CSUM_IN_OK 0x0020
#define M_TCP_CSUM_IN_BAD 0x0040
#define M_UDP_CSUM_IN_OK 0x0080
#define M_UDP_CSUM_IN_BAD 0x0100
#define M_ICMP_CSUM_OUT 0x0200
#define M_ICMP_CSUM_IN_OK 0x0400
#define M_ICMP_CSUM_IN_BAD 0x0800
#define M_IPV6_DF_OUT 0x1000
#define M_TIMESTAMP 0x2000
#define M_FLOWID 0x4000
#define M_TCP_TSO 0x8000
#ifdef _KERNEL
#define MCS_BITS \
("\20\1IPV4_CSUM_OUT\2TCP_CSUM_OUT\3UDP_CSUM_OUT\4IPV4_CSUM_IN_OK" \
"\5IPV4_CSUM_IN_BAD\6TCP_CSUM_IN_OK\7TCP_CSUM_IN_BAD\10UDP_CSUM_IN_OK" \
"\11UDP_CSUM_IN_BAD\12ICMP_CSUM_OUT\13ICMP_CSUM_IN_OK\14ICMP_CSUM_IN_BAD" \
"\15IPV6_NODF_OUT" "\16TIMESTAMP" "\17FLOWID" "\20TCP_TSO")
#endif
#define MT_FREE 0
#define MT_DATA 1
#define MT_HEADER 2
#define MT_SONAME 3
#define MT_SOOPTS 4
#define MT_FTABLE 5
#define MT_CONTROL 6
#define MT_OOBDATA 7
#define MT_NTYPES 8
#include <sys/malloc.h>
#define M_DONTWAIT M_NOWAIT
#define M_WAIT M_WAITOK
#define MGET(m, how, type) m = m_get((how), (type))
#define MGETHDR(m, how, type) m = m_gethdr((how), (type))
#ifdef DEBUG
#define MCLREFDEBUGN(m, file, line) do { \
(m)->m_ext.ext_nfile = (file); \
(m)->m_ext.ext_nline = (line); \
} while ( 0)
#define MCLREFDEBUGO(m, file, line) do { \
(m)->m_ext.ext_ofile = (file); \
(m)->m_ext.ext_oline = (line); \
} while ( 0)
#else
#define MCLREFDEBUGN(m, file, line)
#define MCLREFDEBUGO(m, file, line)
#endif
#define MCLISREFERENCED(m) ((m)->m_ext.ext_nextref != (m))
#define MCLADDREFERENCE(o, n) m_extref((o), (n))
#define MCLINITREFERENCE(m) do { \
(m)->m_ext.ext_prevref = (m); \
(m)->m_ext.ext_nextref = (m); \
MCLREFDEBUGO((m), __FILE__, __LINE__); \
MCLREFDEBUGN((m), NULL, 0); \
} while ( 0)
#define MEXTADD(m, buf, size, mflags, freefn, arg) do { \
(m)->m_data = (m)->m_ext.ext_buf = (caddr_t)(buf); \
(m)->m_flags |= M_EXT | (mflags & M_EXTWR); \
(m)->m_ext.ext_size = (size); \
(m)->m_ext.ext_free_fn = (freefn); \
(m)->m_ext.ext_arg = (arg); \
MCLINITREFERENCE(m); \
} while ( 0)
#define MCLGET(m, how) (void) m_clget((m), (how), MCLBYTES)
#define MCLGETL(m, how, l) m_clget((m), (how), (l))
u_int mextfree_register(void (*)(caddr_t, u_int, void *));
#define MEXTFREE_POOL 0
#define M_MOVE_HDR(to, from) do { \
(to)->m_pkthdr = (from)->m_pkthdr; \
(from)->m_flags &= ~M_PKTHDR; \
SLIST_INIT(&(from)->m_pkthdr.ph_tags); \
(from)->m_pkthdr.pf.statekey = NULL; \
} while ( 0)
#define M_MOVE_PKTHDR(to, from) do { \
(to)->m_flags = ((to)->m_flags & (M_EXT | M_EXTWR)); \
(to)->m_flags |= (from)->m_flags & M_COPYFLAGS; \
M_MOVE_HDR((to), (from)); \
if (((to)->m_flags & M_EXT) == 0) \
(to)->m_data = (to)->m_pktdat; \
} while ( 0)
#define M_READONLY(m) \
(((m)->m_flags & M_EXT) != 0 && \
(((m)->m_flags & M_EXTWR) == 0 || MCLISREFERENCED(m)))
#define M_PREPEND(m, plen, how) \
(m) = m_prepend((m), (plen), (how))
#define M_COPYALL 1000000000
enum mbstat_counters {
mbs_drops = MT_NTYPES,
mbs_wait,
mbs_drain,
mbs_defrag_alloc,
mbs_prepend_alloc,
mbs_pullup_alloc,
mbs_pullup_copy,
mbs_pulldown_alloc,
mbs_pulldown_copy,
mbs_ncounters
};
struct mbstat {
u_long m_drops;
u_long m_wait;
u_long m_drain;
u_long m_mtypes[MT_NTYPES];
u_long m_defrag_alloc;
u_long m_prepend_alloc;
u_long m_pullup_alloc;
u_long m_pullup_copy;
u_long m_pulldown_alloc;
u_long m_pulldown_copy;
};
#include <sys/mutex.h>
struct mbuf_list {
struct mbuf *ml_head;
struct mbuf *ml_tail;
u_int ml_len;
};
struct mbuf_queue {
struct mutex mq_mtx;
struct mbuf_list mq_list;
u_int mq_maxlen;
u_int mq_drops;
};
#ifdef _KERNEL
struct pool;
extern long nmbclust;
extern int max_linkhdr;
extern int max_protohdr;
extern int max_hdr;
extern struct cpumem *mbstat;
void mbinit(void);
void mbcpuinit(void);
int nmbclust_update(long);
struct mbuf *m_copym(struct mbuf *, int, int, int);
struct mbuf *m_free(struct mbuf *);
struct mbuf *m_get(int, int);
struct mbuf *m_getclr(int, int);
struct mbuf *m_gethdr(int, int);
struct mbuf *m_inithdr(struct mbuf *);
void m_removehdr(struct mbuf *);
void m_resethdr(struct mbuf *);
void m_calchdrlen(struct mbuf *);
int m_defrag(struct mbuf *, int);
struct mbuf *m_prepend(struct mbuf *, int, int);
struct mbuf *m_pulldown(struct mbuf *, int, int, int *);
struct mbuf *m_pullup(struct mbuf *, int);
struct mbuf *m_split(struct mbuf *, int, int);
struct mbuf *m_makespace(struct mbuf *, int, int, int *);
struct mbuf *m_getptr(struct mbuf *, int, int *);
int m_leadingspace(struct mbuf *);
int m_trailingspace(struct mbuf *);
void m_align(struct mbuf *, int);
struct mbuf *m_clget(struct mbuf *, int, u_int);
void m_extref(struct mbuf *, struct mbuf *);
void m_pool_init(struct pool *, u_int, u_int, const char *);
u_int m_pool_used(void);
void m_extfree_pool(caddr_t, u_int, void *);
void m_adj(struct mbuf *, int);
int m_copyback(struct mbuf *, int, int, const void *, int);
struct mbuf *m_freem(struct mbuf *);
void m_purge(struct mbuf *);
void m_copydata(struct mbuf *, int, int, void *);
void m_cat(struct mbuf *, struct mbuf *);
struct mbuf *m_devget(char *, int, int);
int m_apply(struct mbuf *, int, int,
int (*)(caddr_t, caddr_t, unsigned int), caddr_t);
struct mbuf *m_dup_pkt(struct mbuf *, unsigned int, int);
int m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
void m_microtime(const struct mbuf *, struct timeval *);
static inline struct mbuf *
m_freemp(struct mbuf **mp)
{
struct mbuf *m = *mp;
*mp = NULL;
return m_freem(m);
}
#include <sys/percpu.h>
static inline void
mbstat_inc(enum mbstat_counters c)
{
int s = splnet();
counters_inc(mbstat, c);
splx(s);
}
struct m_tag *m_tag_get(int, int, int);
void m_tag_prepend(struct mbuf *, struct m_tag *);
void m_tag_delete(struct mbuf *, struct m_tag *);
void m_tag_delete_chain(struct mbuf *);
struct m_tag *m_tag_find(struct mbuf *, int, struct m_tag *);
struct m_tag *m_tag_copy(struct m_tag *, int);
int m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
struct m_tag *m_tag_first(struct mbuf *);
struct m_tag *m_tag_next(struct mbuf *, struct m_tag *);
#define PACKET_TAG_IPSEC_IN_DONE 0x0001
#define PACKET_TAG_IPSEC_OUT_DONE 0x0002
#define PACKET_TAG_IPSEC_FLOWINFO 0x0004
#define PACKET_TAG_IP_OFFNXT 0x0010
#define PACKET_TAG_IP6_OFFNXT 0x0020
#define PACKET_TAG_WIREGUARD 0x0040
#define PACKET_TAG_GRE 0x0080
#define PACKET_TAG_DLT 0x0100
#define PACKET_TAG_PF_DIVERT 0x0200
#define PACKET_TAG_PF_REASSEMBLED 0x0800
#define PACKET_TAG_SRCROUTE 0x1000
#define PACKET_TAG_TUNNEL 0x2000
#define PACKET_TAG_CARP_BAL_IP 0x4000
#define MTAG_BITS \
("\20\1IPSEC_IN_DONE\2IPSEC_OUT_DONE\3IPSEC_FLOWINFO" \
"\4IPSEC_OUT_CRYPTO_NEEDED\5IPSEC_PENDING_TDB\6BRIDGE\7WG\10GRE\11DLT" \
"\12PF_DIVERT\14PF_REASSEMBLED\15SRCROUTE\16TUNNEL\17CARP_BAL_IP")
#define PACKET_TAG_MAXSIZE 80
#define M_MAXLOOP 128
#define MBUF_LIST_INITIALIZER() { NULL, NULL, 0 }
void ml_init(struct mbuf_list *);
void ml_enqueue(struct mbuf_list *, struct mbuf *);
struct mbuf * ml_dequeue(struct mbuf_list *);
void ml_enlist(struct mbuf_list *, struct mbuf_list *);
struct mbuf * ml_dechain(struct mbuf_list *);
unsigned int ml_purge(struct mbuf_list *);
unsigned int ml_hdatalen(struct mbuf_list *);
#define ml_len(_ml) ((_ml)->ml_len)
#define ml_empty(_ml) ((_ml)->ml_len == 0)
#define MBUF_LIST_FIRST(_ml) ((_ml)->ml_head)
#define MBUF_LIST_NEXT(_m) ((_m)->m_nextpkt)
#define MBUF_LIST_FOREACH(_ml, _m) \
for ((_m) = MBUF_LIST_FIRST(_ml); \
(_m) != NULL; \
(_m) = MBUF_LIST_NEXT(_m))
#include <sys/atomic.h>
#define MBUF_QUEUE_INITIALIZER(_maxlen, _ipl) \
{ MUTEX_INITIALIZER(_ipl), MBUF_LIST_INITIALIZER(), (_maxlen), 0 }
void mq_init(struct mbuf_queue *, u_int, int);
int mq_push(struct mbuf_queue *, struct mbuf *);
int mq_enqueue(struct mbuf_queue *, struct mbuf *);
struct mbuf * mq_dequeue(struct mbuf_queue *);
int mq_enlist(struct mbuf_queue *, struct mbuf_list *);
void mq_delist(struct mbuf_queue *, struct mbuf_list *);
unsigned int mq_purge(struct mbuf_queue *);
unsigned int mq_hdatalen(struct mbuf_queue *);
void mq_set_maxlen(struct mbuf_queue *, u_int);
#define mq_len(_mq) READ_ONCE((_mq)->mq_list.ml_len)
#define mq_empty(_mq) (mq_len(_mq) == 0)
#define mq_full(_mq) (mq_len((_mq)) >= READ_ONCE((_mq)->mq_maxlen))
#define mq_drops(_mq) READ_ONCE((_mq)->mq_drops)
#endif
#endif