forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbuf.h
114 lines (95 loc) · 2.71 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
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 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)
/**
* 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);
#endif /* __DP_VS_MBUF_H__ */