summaryrefslogtreecommitdiffstats
path: root/kernel/arch/s390/include/asm/atomic.h
blob: 911064aa59b2f25c5c1dc7a7ac846a560ea99b1e (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright IBM Corp. 1999, 2009
 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
 *	      Denis Joseph Barrow,
 *	      Arnd Bergmann <arndb@de.ibm.com>,
 *
 * Atomic operations that C can't guarantee us.
 * Useful for resource counting etc.
 * s390 uses 'Compare And Swap' for atomicity in SMP environment.
 *
 */

#ifndef __ARCH_S390_ATOMIC__
#define __ARCH_S390_ATOMIC__

#include <linux/compiler.h>
#include <linux/types.h>
#include <asm/barrier.h>
#include <asm/cmpxchg.h>

#define ATOMIC_INIT(i)  { (i) }

#define __ATOMIC_NO_BARRIER	"\n"

#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES

#define __ATOMIC_OR	"lao"
#define __ATOMIC_AND	"lan"
#define __ATOMIC_ADD	"laa"
#define __ATOMIC_XOR	"lax"
#define __ATOMIC_BARRIER "bcr	14,0\n"

#define __ATOMIC_LOOP(ptr, op_val, op_string, __barrier)		\
({									\
	int old_val;							\
									\
	typecheck(atomic_t *, ptr);					\
	asm volatile(							\
		op_string "	%0,%2,%1\n"				\
		__barrier						\
		: "=d" (old_val), "+Q" ((ptr)->counter)			\
		: "d" (op_val)						\
		: "cc", "memory");					\
	old_val;							\
})

#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */

#define __ATOMIC_OR	"or"
#define __ATOMIC_AND	"nr"
#define __ATOMIC_ADD	"ar"
#define __ATOMIC_XOR	"xr"
#define __ATOMIC_BARRIER "\n"

#define __ATOMIC_LOOP(ptr, op_val, op_string, __barrier)		\
({									\
	int old_val, new_val;						\
									\
	typecheck(atomic_t *, ptr);					\
	asm volatile(							\
		"	l	%0,%2\n"				\
		"0:	lr	%1,%0\n"				\
		op_string "	%1,%3\n"				\
		"	cs	%0,%1,%2\n"				\
		"	jl	0b"					\
		: "=&d" (old_val), "=&d" (new_val), "+Q" ((ptr)->counter)\
		: "d" (op_val)						\
		: "cc", "memory");					\
	old_val;							\
})

#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */

static inline int atomic_read(const atomic_t *v)
{
	int c;

	asm volatile(
		"	l	%0,%1\n"
		: "=d" (c) : "Q" (v->counter));
	return c;
}

static inline void atomic_set(atomic_t *v, int i)
{
	asm volatile(
		"	st	%1,%0\n"
		: "=Q" (v->counter) : "d" (i));
}

static inline int atomic_add_return(int i, atomic_t *v)
{
	return __ATOMIC_LOOP(v, i, __ATOMIC_ADD, __ATOMIC_BARRIER) + i;
}

static inline void atomic_add(int i, atomic_t *v)
{
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
	if (__builtin_constant_p(i) && (i > -129) && (i < 128)) {
		asm volatile(
			"asi	%0,%1\n"
			: "+Q" (v->counter)
			: "i" (i)
			: "cc", "memory");
		return;
	}
#endif
	__ATOMIC_LOOP(v, i, __ATOMIC_ADD, __ATOMIC_NO_BARRIER);
}

#define atomic_add_negative(_i, _v)	(atomic_add_return(_i, _v) < 0)
#define atomic_inc(_v)			atomic_add(1, _v)
#define atomic_inc_return(_v)		atomic_add_return(1, _v)
#define atomic_inc_and_test(_v)		(atomic_add_return(1, _v) == 0)
#define atomic_sub(_i, _v)		atomic_add(-(int)(_i), _v)
#define atomic_sub_return(_i, _v)	atomic_add_return(-(int)(_i), _v)
#define atomic_sub_and_test(_i, _v)	(atomic_sub_return(_i, _v) == 0)
#define atomic_dec(_v)			atomic_sub(1, _v)
#define atomic_dec_return(_v)		atomic_sub_return(1, _v)
#define atomic_dec_and_test(_v)		(atomic_sub_return(1, _v) == 0)

#define ATOMIC_OP(op, OP)						\
static inline void atomic_##op(int i, atomic_t *v)			\
{									\
	__ATOMIC_LOOP(v, i, __ATOMIC_##OP, __ATOMIC_NO_BARRIER);	\
}

ATOMIC_OP(and, AND)
ATOMIC_OP(or, OR)
ATOMIC_OP(xor, XOR)

#undef ATOMIC_OP

#define atomic_xchg(v, new) (xchg(&((v)->counter), new))

static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
{
	asm volatile(
		"	cs	%0,%2,%1"
		: "+d" (old), "+Q" (v->counter)
		: "d" (new)
		: "cc", "memory");
	return old;
}

static inline int __atomic_add_unless(atomic_t *v, int a, int u)
{
	int c, old;
	c = atomic_read(v);
	for (;;) {
		if (unlikely(c == u))
			break;
		old = atomic_cmpxchg(v, c, c + a);
		if (likely(old == c))
			break;
		c = old;
	}
	return c;
}


#undef __ATOMIC_LOOP

#define ATOMIC64_INIT(i)  { (i) }

#define __ATOMIC64_NO_BARRIER	"\n"

#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES

#define __ATOMIC64_OR	"laog"
#define __ATOMIC64_AND	"lang"
#define __ATOMIC64_ADD	"laag"
#define __ATOMIC64_XOR	"laxg"
#define __ATOMIC64_BARRIER "bcr	14,0\n"

#define __ATOMIC64_LOOP(ptr, op_val, op_string, __barrier)		\
({									\
	long long old_val;						\
									\
	typecheck(atomic64_t *, ptr);					\
	asm volatile(							\
		op_string "	%0,%2,%1\n"				\
		__barrier						\
		: "=d" (old_val), "+Q" ((ptr)->counter)			\
		: "d" (op_val)						\
		: "cc", "memory");					\
	old_val;							\
})

#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */

#define __ATOMIC64_OR	"ogr"
#define __ATOMIC64_AND	"ngr"
#define __ATOMIC64_ADD	"agr"
#define __ATOMIC64_XOR	"xgr"
#define __ATOMIC64_BARRIER "\n"

#define __ATOMIC64_LOOP(ptr, op_val, op_string, __barrier)		\
({									\
	long long old_val, new_val;					\
									\
	typecheck(atomic64_t *, ptr);					\
	asm volatile(							\
		"	lg	%0,%2\n"				\
		"0:	lgr	%1,%0\n"				\
		op_string "	%1,%3\n"				\
		"	csg	%0,%1,%2\n"				\
		"	jl	0b"					\
		: "=&d" (old_val), "=&d" (new_val), "+Q" ((ptr)->counter)\
		: "d" (op_val)						\
		: "cc", "memory");					\
	old_val;							\
})

#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */

static inline long long atomic64_read(const atomic64_t *v)
{
	long long c;

	asm volatile(
		"	lg	%0,%1\n"
		: "=d" (c) : "Q" (v->counter));
	return c;
}

static inline void atomic64_set(atomic64_t *v, long long i)
{
	asm volatile(
		"	stg	%1,%0\n"
		: "=Q" (v->counter) : "d" (i));
}

static inline long long atomic64_add_return(long long i, atomic64_t *v)
{
	return __ATOMIC64_LOOP(v, i, __ATOMIC64_ADD, __ATOMIC64_BARRIER) + i;
}

static inline void atomic64_add(long long i, atomic64_t *v)
{
#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
	if (__builtin_constant_p(i) && (i > -129) && (i < 128)) {
		asm volatile(
			"agsi	%0,%1\n"
			: "+Q" (v->counter)
			: "i" (i)
			: "cc", "memory");
		return;
	}
#endif
	__ATOMIC64_LOOP(v, i, __ATOMIC64_ADD, __ATOMIC64_NO_BARRIER);
}

#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))

static inline long long atomic64_cmpxchg(atomic64_t *v,
					     long long old, long long new)
{
	asm volatile(
		"	csg	%0,%2,%1"
		: "+d" (old), "+Q" (v->counter)
		: "d" (new)
		: "cc", "memory");
	return old;
}

#define ATOMIC64_OP(op, OP)						\
static inline void atomic64_##op(long i, atomic64_t *v)			\
{									\
	__ATOMIC64_LOOP(v, i, __ATOMIC64_##OP, __ATOMIC64_NO_BARRIER);	\
}

ATOMIC64_OP(and, AND)
ATOMIC64_OP(or, OR)
ATOMIC64_OP(xor, XOR)

#undef ATOMIC64_OP
#undef __ATOMIC64_LOOP

static inline int atomic64_add_unless(atomic64_t *v, long long i, long long u)
{
	long long c, old;

	c = atomic64_read(v);
	for (;;) {
		if (unlikely(c == u))
			break;
		old = atomic64_cmpxchg(v, c, c + i);
		if (likely(old == c))
			break;
		c = old;
	}
	return c != u;
}

static inline long long atomic64_dec_if_positive(atomic64_t *v)
{
	long long c, old, dec;

	c = atomic64_read(v);
	for (;;) {
		dec = c - 1;
		if (unlikely(dec < 0))
			break;
		old = atomic64_cmpxchg((v), c, dec);
		if (likely(old == c))
			break;
		c = old;
	}
	return dec;
}

#define atomic64_add_negative(_i, _v)	(atomic64_add_return(_i, _v) < 0)
#define atomic64_inc(_v)		atomic64_add(1, _v)
#define atomic64_inc_return(_v)		atomic64_add_return(1, _v)
#define atomic64_inc_and_test(_v)	(atomic64_add_return(1, _v) == 0)
#define atomic64_sub_return(_i, _v)	atomic64_add_return(-(long long)(_i), _v)
#define atomic64_sub(_i, _v)		atomic64_add(-(long long)(_i), _v)
#define atomic64_sub_and_test(_i, _v)	(atomic64_sub_return(_i, _v) == 0)
#define atomic64_dec(_v)		atomic64_sub(1, _v)
#define atomic64_dec_return(_v)		atomic64_sub_return(1, _v)
#define atomic64_dec_and_test(_v)	(atomic64_sub_return(1, _v) == 0)
#define atomic64_inc_not_zero(v)	atomic64_add_unless((v), 1, 0)

#endif /* __ARCH_S390_ATOMIC__  */
reachable_port(targ); if (port) { tun_base->offload_crc = port->capabilities.tx_offload_cksum; } } static void init_task_ipv6_decap(struct task_base* tbase, struct task_args* targ) { struct task_ipv6_decap* tun_task = (struct task_ipv6_decap*)tbase; struct task_ipv6_tun_base* tun_base = (struct task_ipv6_tun_base*)tun_task; init_task_ipv6_tun_base(tun_base, targ); tun_base->runtime_flags = targ->runtime_flags; memcpy(&tun_task->dst_mac, &targ->edaddr, sizeof(tun_task->dst_mac)); } static void init_task_ipv6_encap(struct task_base* tbase, struct task_args* targ) { struct task_ipv6_encap* tun_task = (struct task_ipv6_encap*)tbase; struct task_ipv6_tun_base *tun_base = (struct task_ipv6_tun_base*)tun_task; init_task_ipv6_tun_base(tun_base, targ); rte_memcpy(&tun_task->local_endpoint_addr, &targ->local_ipv6, sizeof(tun_task->local_endpoint_addr)); tun_task->tunnel_hop_limit = targ->tunnel_hop_limit; tun_base->runtime_flags = targ->runtime_flags; } static struct task_init task_init_ipv6_decap = { .mode_str = "ipv6_decap", .init = init_task_ipv6_decap, .handle = handle_ipv6_decap_bulk, .size = sizeof(struct task_ipv6_decap) }; static struct task_init task_init_ipv6_encap = { .mode_str = "ipv6_encap", .init = init_task_ipv6_encap, .handle = handle_ipv6_encap_bulk, .size = sizeof(struct task_ipv6_encap) }; __attribute__((constructor)) static void reg_task_ipv6_decap(void) { reg_task(&task_init_ipv6_decap); } __attribute__((constructor)) static void reg_task_ipv6_encap(void) { reg_task(&task_init_ipv6_encap); } static inline uint8_t handle_ipv6_decap(struct task_ipv6_decap* ptask, struct rte_mbuf* rx_mbuf, struct ipv6_tun_dest* tun_dest); static inline uint8_t handle_ipv6_encap(struct task_ipv6_encap* ptask, struct rte_mbuf* rx_mbuf, struct ipv6_tun_dest* tun_dest); static inline int extract_key_fields( __attribute__((unused)) struct task_ipv6_tun_base* ptask, struct ipv4_hdr* pip4, ipv6_tun_dir_t dir, uint32_t* pAddr, uint16_t* pPort) { *pAddr = (dir == TUNNEL_DIR_DECAP) ? pip4->src_addr : pip4->dst_addr; if (pip4->next_proto_id == IPPROTO_UDP) { struct udp_hdr* pudp = (struct udp_hdr *)(pip4 + 1); *pPort = rte_be_to_cpu_16((dir == TUNNEL_DIR_DECAP) ? pudp->src_port : pudp->dst_port); } else if (pip4->next_proto_id == IPPROTO_TCP) { struct tcp_hdr* ptcp = (struct tcp_hdr *)(pip4 + 1); *pPort = rte_be_to_cpu_16((dir == TUNNEL_DIR_DECAP) ? ptcp->src_port : ptcp->dst_port); } else { plog_warn("IPv6 Tunnel: IPv4 packet of unexpected type proto_id=0x%x\n", pip4->next_proto_id); *pPort = 0xffff; return -1; } return 0; } static inline void extract_key(struct task_ipv6_tun_base* ptask, struct ipv4_hdr* pip4, ipv6_tun_dir_t dir, uint64_t* pkey) { uint32_t lookup_addr; uint16_t lookup_port; if (unlikely( extract_key_fields(ptask, pip4, dir, &lookup_addr, &lookup_port))) { plog_warn("IPv6 Tunnel: Unable to extract fields from packet\n"); *pkey = 0xffffffffL; return; } *pkey = MAKE_KEY_FROM_FIELDS(lookup_addr, lookup_port, ptask->lookup_port_mask); } static inline struct ipv4_hdr* get_ipv4_decap(struct rte_mbuf *mbuf) { struct ether_hdr* peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *); struct ipv6_hdr* pip6 = (struct ipv6_hdr *)(peth + 1); struct ipv4_hdr* pip4 = (struct ipv4_hdr*) (pip6 + 1); // TODO - Skip Option headers return pip4; } static inline struct ipv4_hdr* get_ipv4_encap(struct rte_mbuf *mbuf) { struct ether_hdr* peth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *); struct ipv4_hdr* pip4 = (struct ipv4_hdr *)(peth + 1); return pip4; } static inline void extract_key_decap(struct task_ipv6_tun_base* ptask, struct rte_mbuf *mbuf, uint64_t* pkey) { extract_key(ptask, get_ipv4_decap(mbuf), TUNNEL_DIR_DECAP, pkey); } static inline void extract_key_decap_bulk(struct task_ipv6_tun_base* ptask, struct rte_mbuf **mbufs, uint16_t n_pkts) { for (uint16_t j = 0; j < n_pkts; ++j) { extract_key_decap(ptask, mbufs[j], &ptask->keys[j]); } } static inline void extract_key_encap(struct task_ipv6_tun_base* ptask, struct rte_mbuf *mbuf, uint64_t* pkey) { extract_key(ptask, get_ipv4_encap(mbuf), TUNNEL_DIR_ENCAP, pkey); } static inline void extract_key_encap_bulk(struct task_ipv6_tun_base* ptask, struct rte_mbuf **mbufs, uint16_t n_pkts) { for (uint16_t j = 0; j < n_pkts; ++j) { extract_key_encap(ptask, mbufs[j], &ptask->keys[j]); } } __attribute__((cold)) static void handle_error(struct task_ipv6_tun_base* ptask, struct rte_mbuf* mbuf, ipv6_tun_dir_t dir) { uint32_t lookup_addr; uint16_t lookup_port; uint64_t key; struct ipv4_hdr* pip4 = (dir == TUNNEL_DIR_DECAP) ? get_ipv4_decap(mbuf) : get_ipv4_encap(mbuf); extract_key_fields(ptask, pip4, dir, &lookup_addr, &lookup_port); extract_key(ptask, pip4, dir, &key); plog_warn("IPv6 Tunnel (%s) lookup failed for "IPv4_BYTES_FMT":%d [key=0x%"PRIx64"]\n", (dir == TUNNEL_DIR_DECAP) ? "decap" : "encap", IPv4_BYTES(((unsigned char*)&lookup_addr)), lookup_port, key); } static int handle_ipv6_decap_bulk(struct task_base* tbase, struct rte_mbuf** mbufs, const uint16_t n_pkts) { struct task_ipv6_decap* task = (struct task_ipv6_decap *)tbase; uint64_t pkts_mask = RTE_LEN2MASK(n_pkts, uint64_t); struct ipv6_tun_dest* entries[64]; uint8_t out[MAX_PKT_BURST]; uint64_t lookup_hit_mask; uint16_t n_kept = 0; prefetch_pkts(mbufs, n_pkts); // Lookup to verify packets are valid for their respective tunnels (their sending lwB4) extract_key_decap_bulk(&task->base, mbufs, n_pkts); rte_table_hash_key8_ext_dosig_ops.f_lookup(task->base.lookup_table, task->base.fake_packets, pkts_mask, &lookup_hit_mask, (void**)entries); if (likely(lookup_hit_mask == pkts_mask)) { for (uint16_t j = 0; j < n_pkts; ++j) { out[j] = handle_ipv6_decap(task, mbufs[j], entries[j]); } } else { for (uint16_t j = 0; j < n_pkts; ++j) { if (unlikely(!((lookup_hit_mask >> j) & 0x1))) { handle_error(&task->base, mbufs[j], TUNNEL_DIR_DECAP); out[j] = OUT_DISCARD; continue; } out[j] = handle_ipv6_decap(task, mbufs[j], entries[j]); } } return task->base.base.tx_pkt(tbase, mbufs, n_pkts, out); } static int handle_ipv6_encap_bulk(struct task_base* tbase, struct rte_mbuf** mbufs, const uint16_t n_pkts) { struct task_ipv6_encap* task = (struct task_ipv6_encap *)tbase; uint64_t pkts_mask = RTE_LEN2MASK(n_pkts, uint64_t); struct ipv6_tun_dest* entries[64]; uint64_t lookup_hit_mask; uint8_t out[MAX_PKT_BURST]; uint16_t n_kept = 0; prefetch_first(mbufs, n_pkts); extract_key_encap_bulk(&task->base, mbufs, n_pkts); rte_table_hash_key8_ext_dosig_ops.f_lookup(task->base.lookup_table, task->base.fake_packets, pkts_mask, &lookup_hit_mask, (void**)entries); if (likely(lookup_hit_mask == pkts_mask)) { for (uint16_t j = 0; j < n_pkts; ++j) { out[j] = handle_ipv6_encap(task, mbufs[j], entries[j]); } } else { for (uint16_t j = 0; j < n_pkts; ++j) { if (unlikely(!((lookup_hit_mask >> j) & 0x1))) { handle_error(&task->base, mbufs[j], TUNNEL_DIR_ENCAP); out[j] = OUT_DISCARD; continue; } out[j] = handle_ipv6_encap(task, mbufs[j], entries[j]); } } return task->base.base.tx_pkt(tbase, mbufs, n_pkts, out); } static inline uint8_t handle_ipv6_decap(struct task_ipv6_decap* ptask, struct rte_mbuf* rx_mbuf, __attribute__((unused)) struct ipv6_tun_dest* tun_dest) { struct ether_hdr* peth = rte_pktmbuf_mtod(rx_mbuf, struct ether_hdr *); if (unlikely(peth->ether_type != ETYPE_IPv6)) { plog_warn("Received non IPv6 packet on ipv6 tunnel port\n"); // Drop packet return OUT_DISCARD; } struct ipv6_hdr* pip6 = (struct ipv6_hdr *)(peth + 1); int ipv6_hdr_len = sizeof(struct ipv6_hdr); // TODO - Skip over any IPv6 Extension Header: // If pip6->next_header is in (0, 43, 44, 50, 51, 60, 135), skip ahead pip->hdr_ext_len // bytes and repeat. Increase ipv6_hdr_len with as much, each time. if (unlikely(pip6->proto != IPPROTO_IPIP)) { plog_warn("Received non IPv4 content within IPv6 tunnel packet\n"); // Drop packet return OUT_DISCARD; } // Discard IPv6 encapsulation rte_pktmbuf_adj(rx_mbuf, ipv6_hdr_len); peth = rte_pktmbuf_mtod(rx_mbuf, struct ether_hdr *); // Restore Ethernet header ether_addr_copy(&ptask->base.src_mac, &peth->s_addr); ether_addr_copy(&ptask->dst_mac, &peth->d_addr); peth->ether_type = ETYPE_IPv4; return 0; } static inline uint8_t handle_ipv6_encap(struct task_ipv6_encap* ptask, struct rte_mbuf* rx_mbuf, __attribute__((unused)) struct ipv6_tun_dest* tun_dest) { //plog_info("Found tunnel endpoint:"IPv6_BYTES_FMT" ("MAC_BYTES_FMT")\n", IPv6_BYTES(tun_dest->dst_addr), MAC_BYTES(tun_dest->dst_mac.addr_bytes)); struct ether_hdr* peth = (struct ether_hdr *)(rte_pktmbuf_mtod(rx_mbuf, struct ether_hdr *)); struct ipv4_hdr* pip4 = (struct ipv4_hdr *)(peth + 1); uint16_t ipv4_length = rte_be_to_cpu_16(pip4->total_length); struct task_ipv6_tun_base* tun_base = (struct task_ipv6_tun_base*)ptask; if (unlikely((pip4->version_ihl >> 4) != 4)) { plog_warn("Received non IPv4 packet at ipv6 tunnel input\n"); // Drop packet return OUT_DISCARD; } if (pip4->time_to_live) { pip4->time_to_live--; } else { plog_info("TTL = 0 => Dropping\n"); return OUT_DISCARD; } pip4->hdr_checksum = 0; // Remove padding if any (we don't want to encapsulate garbage at end of IPv4 packet) int padding = rte_pktmbuf_pkt_len(rx_mbuf) - (ipv4_length + sizeof(struct ether_hdr)); if (unlikely(padding > 0)) { rte_pktmbuf_trim(rx_mbuf, padding); } // Encapsulate const int extra_space = sizeof(struct ipv6_hdr); peth = (struct ether_hdr *)rte_pktmbuf_prepend(rx_mbuf, extra_space); // Ethernet Header ether_addr_copy(&ptask->base.src_mac, &peth->s_addr); ether_addr_copy(&tun_dest->dst_mac, &peth->d_addr); peth->ether_type = ETYPE_IPv6; // Set up IPv6 Header struct ipv6_hdr* pip6 = (struct ipv6_hdr *)(peth + 1); pip6->vtc_flow = rte_cpu_to_be_32(IPv6_VERSION << 28); pip6->proto = IPPROTO_IPIP; pip6->payload_len = rte_cpu_to_be_16(ipv4_length); pip6->hop_limits = ptask->tunnel_hop_limit; rte_memcpy(pip6->dst_addr, &tun_dest->dst_addr, sizeof(pip6->dst_addr)); rte_memcpy(pip6->src_addr, &ptask->local_endpoint_addr, sizeof(pip6->src_addr)); if (tun_base->runtime_flags & TASK_TX_CRC) { // We modified the TTL in the IPv4 header, hence have to recompute the IPv4 checksum #define TUNNEL_L2_LEN (sizeof(struct ether_hdr) + sizeof(struct ipv6_hdr)) prox_ip_cksum(rx_mbuf, pip4, TUNNEL_L2_LEN, sizeof(struct ipv4_hdr), ptask->base.offload_crc); } return 0; }