4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_ether.h>
36 #include <rte_ip_frag.h>
37 #include <rte_cycles.h>
40 #include "rte_port_ras.h"
42 #ifndef RTE_PORT_RAS_N_BUCKETS
43 #define RTE_PORT_RAS_N_BUCKETS 4094
46 #ifndef RTE_PORT_RAS_N_ENTRIES_PER_BUCKET
47 #define RTE_PORT_RAS_N_ENTRIES_PER_BUCKET 8
50 #ifndef RTE_PORT_RAS_N_ENTRIES
51 #define RTE_PORT_RAS_N_ENTRIES (RTE_PORT_RAS_N_BUCKETS * RTE_PORT_RAS_N_ENTRIES_PER_BUCKET)
54 #ifdef RTE_PORT_STATS_COLLECT
56 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val) \
57 port->stats.n_pkts_in += val
58 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val) \
59 port->stats.n_pkts_drop += val
63 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(port, val)
64 #define RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(port, val)
68 struct rte_port_ring_writer_ras
;
70 typedef void (*ras_op
)(
71 struct rte_port_ring_writer_ras
*p
,
72 struct rte_mbuf
*pkt
);
75 process_ipv4(struct rte_port_ring_writer_ras
*p
, struct rte_mbuf
*pkt
);
77 process_ipv6(struct rte_port_ring_writer_ras
*p
, struct rte_mbuf
*pkt
);
79 struct rte_port_ring_writer_ras
{
80 struct rte_port_out_stats stats
;
82 struct rte_mbuf
*tx_buf
[RTE_PORT_IN_BURST_SIZE_MAX
];
83 struct rte_ring
*ring
;
85 uint32_t tx_buf_count
;
86 struct rte_ip_frag_tbl
*frag_tbl
;
87 struct rte_ip_frag_death_row death_row
;
93 rte_port_ring_writer_ras_create(void *params
, int socket_id
, int is_ipv4
)
95 struct rte_port_ring_writer_ras_params
*conf
=
96 (struct rte_port_ring_writer_ras_params
*) params
;
97 struct rte_port_ring_writer_ras
*port
;
100 /* Check input parameters */
102 RTE_LOG(ERR
, PORT
, "%s: Parameter conf is NULL\n", __func__
);
105 if (conf
->ring
== NULL
) {
106 RTE_LOG(ERR
, PORT
, "%s: Parameter ring is NULL\n", __func__
);
109 if ((conf
->tx_burst_sz
== 0) ||
110 (conf
->tx_burst_sz
> RTE_PORT_IN_BURST_SIZE_MAX
)) {
111 RTE_LOG(ERR
, PORT
, "%s: Parameter tx_burst_sz is invalid\n",
116 /* Memory allocation */
117 port
= rte_zmalloc_socket("PORT", sizeof(*port
),
118 RTE_CACHE_LINE_SIZE
, socket_id
);
120 RTE_LOG(ERR
, PORT
, "%s: Failed to allocate socket\n", __func__
);
124 /* Create fragmentation table */
125 frag_cycles
= (rte_get_tsc_hz() + MS_PER_S
- 1) / MS_PER_S
* MS_PER_S
;
128 port
->frag_tbl
= rte_ip_frag_table_create(
129 RTE_PORT_RAS_N_BUCKETS
,
130 RTE_PORT_RAS_N_ENTRIES_PER_BUCKET
,
131 RTE_PORT_RAS_N_ENTRIES
,
135 if (port
->frag_tbl
== NULL
) {
136 RTE_LOG(ERR
, PORT
, "%s: rte_ip_frag_table_create failed\n",
143 port
->ring
= conf
->ring
;
144 port
->tx_burst_sz
= conf
->tx_burst_sz
;
145 port
->tx_buf_count
= 0;
147 port
->f_ras
= (is_ipv4
== 1) ? process_ipv4
: process_ipv6
;
153 rte_port_ring_writer_ipv4_ras_create(void *params
, int socket_id
)
155 return rte_port_ring_writer_ras_create(params
, socket_id
, 1);
159 rte_port_ring_writer_ipv6_ras_create(void *params
, int socket_id
)
161 return rte_port_ring_writer_ras_create(params
, socket_id
, 0);
165 send_burst(struct rte_port_ring_writer_ras
*p
)
169 nb_tx
= rte_ring_sp_enqueue_burst(p
->ring
, (void **)p
->tx_buf
,
172 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_DROP_ADD(p
, p
->tx_buf_count
- nb_tx
);
173 for ( ; nb_tx
< p
->tx_buf_count
; nb_tx
++)
174 rte_pktmbuf_free(p
->tx_buf
[nb_tx
]);
180 process_ipv4(struct rte_port_ring_writer_ras
*p
, struct rte_mbuf
*pkt
)
182 /* Assume there is no ethernet header */
183 struct ipv4_hdr
*pkt_hdr
= rte_pktmbuf_mtod(pkt
, struct ipv4_hdr
*);
185 /* Get "More fragments" flag and fragment offset */
186 uint16_t frag_field
= rte_be_to_cpu_16(pkt_hdr
->fragment_offset
);
187 uint16_t frag_offset
= (uint16_t)(frag_field
& IPV4_HDR_OFFSET_MASK
);
188 uint16_t frag_flag
= (uint16_t)(frag_field
& IPV4_HDR_MF_FLAG
);
190 /* If it is a fragmented packet, then try to reassemble */
191 if ((frag_flag
== 0) && (frag_offset
== 0))
192 p
->tx_buf
[p
->tx_buf_count
++] = pkt
;
195 struct rte_ip_frag_tbl
*tbl
= p
->frag_tbl
;
196 struct rte_ip_frag_death_row
*dr
= &p
->death_row
;
198 pkt
->l3_len
= sizeof(*pkt_hdr
);
200 /* Process this fragment */
201 mo
= rte_ipv4_frag_reassemble_packet(tbl
, dr
, pkt
, rte_rdtsc(),
204 p
->tx_buf
[p
->tx_buf_count
++] = mo
;
206 rte_ip_frag_free_death_row(&p
->death_row
, 3);
211 process_ipv6(struct rte_port_ring_writer_ras
*p
, struct rte_mbuf
*pkt
)
213 /* Assume there is no ethernet header */
214 struct ipv6_hdr
*pkt_hdr
= rte_pktmbuf_mtod(pkt
, struct ipv6_hdr
*);
216 struct ipv6_extension_fragment
*frag_hdr
;
217 uint16_t frag_data
= 0;
218 frag_hdr
= rte_ipv6_frag_get_ipv6_fragment_header(pkt_hdr
);
219 if (frag_hdr
!= NULL
)
220 frag_data
= rte_be_to_cpu_16(frag_hdr
->frag_data
);
222 /* If it is a fragmented packet, then try to reassemble */
223 if ((frag_data
& RTE_IPV6_FRAG_USED_MASK
) == 0)
224 p
->tx_buf
[p
->tx_buf_count
++] = pkt
;
227 struct rte_ip_frag_tbl
*tbl
= p
->frag_tbl
;
228 struct rte_ip_frag_death_row
*dr
= &p
->death_row
;
230 pkt
->l3_len
= sizeof(*pkt_hdr
) + sizeof(*frag_hdr
);
232 /* Process this fragment */
233 mo
= rte_ipv6_frag_reassemble_packet(tbl
, dr
, pkt
, rte_rdtsc(), pkt_hdr
,
236 p
->tx_buf
[p
->tx_buf_count
++] = mo
;
238 rte_ip_frag_free_death_row(&p
->death_row
, 3);
243 rte_port_ring_writer_ras_tx(void *port
, struct rte_mbuf
*pkt
)
245 struct rte_port_ring_writer_ras
*p
=
246 (struct rte_port_ring_writer_ras
*) port
;
248 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p
, 1);
250 if (p
->tx_buf_count
>= p
->tx_burst_sz
)
257 rte_port_ring_writer_ras_tx_bulk(void *port
,
258 struct rte_mbuf
**pkts
,
261 struct rte_port_ring_writer_ras
*p
=
262 (struct rte_port_ring_writer_ras
*) port
;
264 if ((pkts_mask
& (pkts_mask
+ 1)) == 0) {
265 uint64_t n_pkts
= __builtin_popcountll(pkts_mask
);
268 for (i
= 0; i
< n_pkts
; i
++) {
269 struct rte_mbuf
*pkt
= pkts
[i
];
271 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p
, 1);
273 if (p
->tx_buf_count
>= p
->tx_burst_sz
)
277 for ( ; pkts_mask
; ) {
278 uint32_t pkt_index
= __builtin_ctzll(pkts_mask
);
279 uint64_t pkt_mask
= 1LLU << pkt_index
;
280 struct rte_mbuf
*pkt
= pkts
[pkt_index
];
282 RTE_PORT_RING_WRITER_RAS_STATS_PKTS_IN_ADD(p
, 1);
284 if (p
->tx_buf_count
>= p
->tx_burst_sz
)
287 pkts_mask
&= ~pkt_mask
;
295 rte_port_ring_writer_ras_flush(void *port
)
297 struct rte_port_ring_writer_ras
*p
=
298 (struct rte_port_ring_writer_ras
*) port
;
300 if (p
->tx_buf_count
> 0)
307 rte_port_ring_writer_ras_free(void *port
)
309 struct rte_port_ring_writer_ras
*p
=
310 (struct rte_port_ring_writer_ras
*) port
;
313 RTE_LOG(ERR
, PORT
, "%s: Parameter port is NULL\n", __func__
);
317 rte_port_ring_writer_ras_flush(port
);
318 rte_ip_frag_table_destroy(p
->frag_tbl
);
325 rte_port_ras_writer_stats_read(void *port
,
326 struct rte_port_out_stats
*stats
, int clear
)
328 struct rte_port_ring_writer_ras
*p
=
329 (struct rte_port_ring_writer_ras
*) port
;
332 memcpy(stats
, &p
->stats
, sizeof(p
->stats
));
335 memset(&p
->stats
, 0, sizeof(p
->stats
));
341 * Summary of port operations
343 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops
= {
344 .f_create
= rte_port_ring_writer_ipv4_ras_create
,
345 .f_free
= rte_port_ring_writer_ras_free
,
346 .f_tx
= rte_port_ring_writer_ras_tx
,
347 .f_tx_bulk
= rte_port_ring_writer_ras_tx_bulk
,
348 .f_flush
= rte_port_ring_writer_ras_flush
,
349 .f_stats
= rte_port_ras_writer_stats_read
,
352 struct rte_port_out_ops rte_port_ring_writer_ipv6_ras_ops
= {
353 .f_create
= rte_port_ring_writer_ipv6_ras_create
,
354 .f_free
= rte_port_ring_writer_ras_free
,
355 .f_tx
= rte_port_ring_writer_ras_tx
,
356 .f_tx_bulk
= rte_port_ring_writer_ras_tx_bulk
,
357 .f_flush
= rte_port_ring_writer_ras_flush
,
358 .f_stats
= rte_port_ras_writer_stats_read
,