forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbuf.c
146 lines (125 loc) · 3.69 KB
/
mbuf.c
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
/*
* 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 functions beyond dpdk mbuf API.
*/
#include <assert.h>
#include "mbuf.h"
#define EMBUF
#define RTE_LOGTYPE_EMBUF RTE_LOGTYPE_USER1
/**
* 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.
*/
int mbuf_may_pull(struct rte_mbuf *mbuf, unsigned int len)
{
int delta, eat;
struct rte_mbuf *seg, *next;
if (likely(len <= mbuf->data_len))
return 0;
if (unlikely(len > mbuf->pkt_len))
return -1;
delta = len - mbuf->data_len;
/* different from skb, there's no way to expand mbuf's tail room,
* because mbuf size is determined when init mbuf pool */
if (rte_pktmbuf_tailroom(mbuf) < delta) {
RTE_LOG(ERR, EMBUF, "%s: no tail room.", __func__);
return -1;
}
/* pull bits needed from segments to tail room of heading mbuf */
if (mbuf_copy_bits(mbuf, mbuf->data_len,
mbuf_tail_point(mbuf), delta) != 0)
return -1;
/* free fully eaten segments and leave left segs attached,
* points need be reload if partial bits was eaten for a seg. */
eat = delta;
mbuf_foreach_seg_safe(mbuf, next, seg) {
if (eat <= 0)
break;
if (seg->data_len <= eat) {
assert(mbuf->next == seg);
eat -= seg->data_len;
rte_pktmbuf_free_seg(seg);
mbuf->next = next;
mbuf->nb_segs--;
} else {
rte_pktmbuf_adj(seg, eat);
eat = 0;
break;
}
}
assert(!eat &&
mbuf->data_off + mbuf->data_len + delta <= mbuf->buf_len);
/* mbuf points must be updated */
mbuf->data_len += delta;
return 0;
}
void mbuf_copy_metadata(struct rte_mbuf *mi, struct rte_mbuf *m)
{
RTE_ASSERT(rte_mbuf_refcnt_read(mi) == 1);
mi->priv_size = m->priv_size;
mi->buf_len = m->buf_len;
mi->data_off = m->data_off;
mi->data_len = m->data_len;
mi->port = m->port;
mi->vlan_tci = m->vlan_tci;
mi->vlan_tci_outer = m->vlan_tci_outer;
mi->tx_offload = m->tx_offload;
mi->hash = m->hash;
mi->next = NULL;
mi->pkt_len = mi->data_len;
mi->nb_segs = 1;
mi->ol_flags = m->ol_flags;
mi->packet_type = m->packet_type;
__rte_mbuf_sanity_check(mi, 1);
__rte_mbuf_sanity_check(m,0);
}
struct rte_mbuf *mbuf_copy(struct rte_mbuf *md, struct rte_mempool *mp)
{
struct rte_mbuf *mc, *mi, **prev;
uint32_t pktlen;
uint8_t nseg;
if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
return NULL;
mi = mc;
pktlen = md->pkt_len;
nseg = 0;
prev = &mi->next;
do {
nseg++;
mbuf_copy_metadata(mi, md);
*prev = mi;
prev = &mi->next;
rte_memcpy(rte_pktmbuf_mtod(mi, void *), rte_pktmbuf_mtod(md, void *)
, md->data_len);
} while ((md = md->next) != NULL && (mi = rte_pktmbuf_alloc(mp)) != NULL);
*prev = NULL;
mc->nb_segs = nseg;
mc->pkt_len = pktlen;
if (unlikely (mi == NULL)) {
rte_pktmbuf_free(mc);
return NULL;
}
__rte_mbuf_sanity_check(mc, 1); //check packet header segment
return mc;
}