forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbuf.h
160 lines (131 loc) · 4.39 KB
/
mbuf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2021 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/*
* mbuf.c of dpvs.
*
* it includes some mbuf related function beyond dpdk librte_mbuf.
*/
#ifndef __DP_VS_MBUF_H__
#define __DP_VS_MBUF_H__
#include <stdlib.h>
#include <string.h>
#include "rte_mbuf.h"
/* for each mbuf including heading mbuf and segments */
#define mbuf_foreach(m, pos) \
for (pos = m; pos != NULL; pos = pos->next)
/* for each segments of mbuf */
#define mbuf_foreach_seg(m, s) \
for (s = m->next; s != NULL; s = s->next)
#define mbuf_foreach_seg_safe(m, n, s) \
for (s = m->next, n = s ? s->next : NULL; \
s != NULL; \
s = n, n = s ? s->next : NULL)
#define MBUF_USERDATA(m, type, field) \
(*((type *)(mbuf_userdata((m), (field)))))
#define MBUF_USERDATA_CONST(m, type, field) \
(*((type *)(mbuf_userdata_const((m), (field)))))
typedef union {
void *hdr;
struct {
uint64_t l2_len:RTE_MBUF_L2_LEN_BITS; /* L2 Header Length */
uint64_t l3_len:RTE_MBUF_L3_LEN_BITS; /* L3 Header Length */
uint64_t l4_len:RTE_MBUF_L4_LEN_BITS; /* L4 Header Length */
uint64_t outer_l2_len:RTE_MBUF_OUTL2_LEN_BITS; /* Outer L2 Header Length */
uint64_t outer_l3_len:RTE_MBUF_OUTL3_LEN_BITS; /* Outer L3 Header Length */
};
} mbuf_userdata_field_proto_t;
typedef void * mbuf_userdata_field_route_t;
typedef enum {
MBUF_FIELD_PROTO = 0,
MBUF_FIELD_ROUTE,
} mbuf_usedata_field_t;
/**
* mbuf_copy_bits - copy bits from mbuf to buffer.
* see skb_copy_bits().
*/
static inline int mbuf_copy_bits(const struct rte_mbuf *mbuf,
int offset, void *to, int len)
{
const struct rte_mbuf *seg;
int start, copy, end;
if (offset + len > (int)mbuf->pkt_len)
return -1;
start = 0;
mbuf_foreach(mbuf, seg) {
end = start + seg->data_len;
if ((copy = end - offset) > 0) {
if (copy > len)
copy = len;
memcpy(to, rte_pktmbuf_mtod_offset(
seg, void *, offset - start),
copy);
if ((len -= copy) == 0)
return 0;
offset += copy;
to += copy;
}
start = end;
}
if (!len)
return 0;
return -1;
}
static inline void *mbuf_tail_point(const struct rte_mbuf *mbuf)
{
return rte_pktmbuf_mtod_offset(mbuf, void *, mbuf->data_len);
}
static inline void *mbuf_header_pointer(const struct rte_mbuf *mbuf,
int offset, int len, void *buffer)
{
if (unlikely(mbuf->data_len < offset + len)) {
if (unlikely(mbuf->pkt_len < offset + len))
return NULL;
if (mbuf_copy_bits(mbuf, offset, buffer, len) != 0)
return NULL;
return buffer;
}
return rte_pktmbuf_mtod_offset(mbuf, void *, offset);
}
/**
* mbuf_may_pull - pull bits from segments to heading mbuf if needed.
* see pskb_may_pull() && __pskb_pull_tail().
*
* it expands heading mbuf, moving it's tail forward and copying necessary
* data from segments part.
*
* return 0 if success and -1 on error.
*/
int mbuf_may_pull(struct rte_mbuf *mbuf, unsigned int len);
/**
* Copy a rte_mbuf including the data area.
*
* return a new rte_mbuf if success and NULL on error.
*/
struct rte_mbuf *mbuf_copy(struct rte_mbuf *md, struct rte_mempool *mp);
void mbuf_copy_metadata(struct rte_mbuf *mi, struct rte_mbuf *m);
#ifdef CONFIG_DPVS_MBUF_DEBUG
inline void dp_vs_mbuf_dump(const char *msg, int af, const struct rte_mbuf *mbuf);
#endif
void *mbuf_userdata(struct rte_mbuf *, mbuf_usedata_field_t);
void *mbuf_userdata_const(const struct rte_mbuf *, mbuf_usedata_field_t);
static inline void mbuf_userdata_reset(struct rte_mbuf *m)
{
memset((void *)m->dynfield1, 0, sizeof(m->dynfield1));
}
int mbuf_init(void);
#endif /* __DP_VS_MBUF_H__ */