add prefix to cache line macros
[dpdk.git] / lib / librte_port / rte_port_ras.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 #include <string.h>
34
35 #include <rte_ether.h>
36 #include <rte_ip_frag.h>
37 #include <rte_cycles.h>
38 #include <rte_log.h>
39
40 #include "rte_port_ras.h"
41
42 #ifndef IPV4_RAS_N_BUCKETS
43 #define IPV4_RAS_N_BUCKETS                                 4094
44 #endif
45
46 #ifndef IPV4_RAS_N_ENTRIES_PER_BUCKET
47 #define IPV4_RAS_N_ENTRIES_PER_BUCKET                      8
48 #endif
49
50 #ifndef IPV4_RAS_N_ENTRIES
51 #define IPV4_RAS_N_ENTRIES (IPV4_RAS_N_BUCKETS * IPV4_RAS_N_ENTRIES_PER_BUCKET)
52 #endif
53
54 struct rte_port_ring_writer_ipv4_ras {
55         struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
56         struct rte_ring *ring;
57         uint32_t tx_burst_sz;
58         uint32_t tx_buf_count;
59         struct rte_ip_frag_tbl *frag_tbl;
60         struct rte_ip_frag_death_row death_row;
61 };
62
63 static void *
64 rte_port_ring_writer_ipv4_ras_create(void *params, int socket_id)
65 {
66         struct rte_port_ring_writer_ipv4_ras_params *conf =
67                         (struct rte_port_ring_writer_ipv4_ras_params *) params;
68         struct rte_port_ring_writer_ipv4_ras *port;
69         uint64_t frag_cycles;
70
71         /* Check input parameters */
72         if (conf == NULL) {
73                 RTE_LOG(ERR, PORT, "%s: Parameter conf is NULL\n", __func__);
74                 return NULL;
75         }
76         if (conf->ring == NULL) {
77                 RTE_LOG(ERR, PORT, "%s: Parameter ring is NULL\n", __func__);
78                 return NULL;
79         }
80         if ((conf->tx_burst_sz == 0) ||
81             (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
82                 RTE_LOG(ERR, PORT, "%s: Parameter tx_burst_sz is invalid\n",
83                         __func__);
84                 return NULL;
85         }
86
87         /* Memory allocation */
88         port = rte_zmalloc_socket("PORT", sizeof(*port),
89                         RTE_CACHE_LINE_SIZE, socket_id);
90         if (port == NULL) {
91                 RTE_LOG(ERR, PORT, "%s: Failed to allocate socket\n", __func__);
92                 return NULL;
93         }
94
95         /* Create fragmentation table */
96         frag_cycles = (rte_get_tsc_hz() + MS_PER_S - 1) / MS_PER_S * MS_PER_S;
97         frag_cycles *= 100;
98
99         port->frag_tbl = rte_ip_frag_table_create(
100                 IPV4_RAS_N_BUCKETS,
101                 IPV4_RAS_N_ENTRIES_PER_BUCKET,
102                 IPV4_RAS_N_ENTRIES,
103                 frag_cycles,
104                 socket_id);
105
106         if (port->frag_tbl == NULL) {
107                 RTE_LOG(ERR, PORT, "%s: rte_ip_frag_table_create failed\n",
108                         __func__);
109                 rte_free(port);
110                 return NULL;
111         }
112
113         /* Initialization */
114         port->ring = conf->ring;
115         port->tx_burst_sz = conf->tx_burst_sz;
116         port->tx_buf_count = 0;
117
118         return port;
119 }
120
121 static inline void
122 send_burst(struct rte_port_ring_writer_ipv4_ras *p)
123 {
124         uint32_t nb_tx;
125
126         nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
127                         p->tx_buf_count);
128
129         for ( ; nb_tx < p->tx_buf_count; nb_tx++)
130                 rte_pktmbuf_free(p->tx_buf[nb_tx]);
131
132         p->tx_buf_count = 0;
133 }
134
135 static inline void
136 process_one(struct rte_port_ring_writer_ipv4_ras *p, struct rte_mbuf *pkt)
137 {
138         /* Assume there is no ethernet header */
139         struct ipv4_hdr *pkt_hdr = (struct ipv4_hdr *)
140                         (rte_pktmbuf_mtod(pkt, unsigned char *));
141
142         /* Get "Do not fragment" flag and fragment offset */
143         uint16_t frag_field = rte_be_to_cpu_16(pkt_hdr->fragment_offset);
144         uint16_t frag_offset = (uint16_t)(frag_field & IPV4_HDR_OFFSET_MASK);
145         uint16_t frag_flag = (uint16_t)(frag_field & IPV4_HDR_MF_FLAG);
146
147         /* If it is a fragmented packet, then try to reassemble */
148         if ((frag_flag == 0) && (frag_offset == 0))
149                 p->tx_buf[p->tx_buf_count++] = pkt;
150         else {
151                 struct rte_mbuf *mo;
152                 struct rte_ip_frag_tbl *tbl = p->frag_tbl;
153                 struct rte_ip_frag_death_row *dr = &p->death_row;
154
155                 /* Process this fragment */
156                 mo = rte_ipv4_frag_reassemble_packet(tbl, dr, pkt, rte_rdtsc(), pkt_hdr);
157                 if (mo != NULL)
158                         p->tx_buf[p->tx_buf_count++] = mo;
159
160                 rte_ip_frag_free_death_row(&p->death_row, 3);
161         }
162 }
163
164 static int
165 rte_port_ring_writer_ipv4_ras_tx(void *port, struct rte_mbuf *pkt)
166 {
167         struct rte_port_ring_writer_ipv4_ras *p =
168                         (struct rte_port_ring_writer_ipv4_ras *) port;
169
170         process_one(p, pkt);
171         if (p->tx_buf_count >= p->tx_burst_sz)
172                 send_burst(p);
173
174         return 0;
175 }
176
177 static int
178 rte_port_ring_writer_ipv4_ras_tx_bulk(void *port,
179                 struct rte_mbuf **pkts,
180                 uint64_t pkts_mask)
181 {
182         struct rte_port_ring_writer_ipv4_ras *p =
183                         (struct rte_port_ring_writer_ipv4_ras *) port;
184
185         if ((pkts_mask & (pkts_mask + 1)) == 0) {
186                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
187                 uint32_t i;
188
189                 for (i = 0; i < n_pkts; i++) {
190                         struct rte_mbuf *pkt = pkts[i];
191
192                         process_one(p, pkt);
193                         if (p->tx_buf_count >= p->tx_burst_sz)
194                                 send_burst(p);
195                 }
196         } else {
197                 for ( ; pkts_mask; ) {
198                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
199                         uint64_t pkt_mask = 1LLU << pkt_index;
200                         struct rte_mbuf *pkt = pkts[pkt_index];
201
202                         process_one(p, pkt);
203                         if (p->tx_buf_count >= p->tx_burst_sz)
204                                 send_burst(p);
205
206                         pkts_mask &= ~pkt_mask;
207                 }
208         }
209
210         return 0;
211 }
212
213 static int
214 rte_port_ring_writer_ipv4_ras_flush(void *port)
215 {
216         struct rte_port_ring_writer_ipv4_ras *p =
217                         (struct rte_port_ring_writer_ipv4_ras *) port;
218
219         if (p->tx_buf_count > 0)
220                 send_burst(p);
221
222         return 0;
223 }
224
225 static int
226 rte_port_ring_writer_ipv4_ras_free(void *port)
227 {
228         struct rte_port_ring_writer_ipv4_ras *p =
229                         (struct rte_port_ring_writer_ipv4_ras *) port;
230
231         if (port == NULL) {
232                 RTE_LOG(ERR, PORT, "%s: Parameter port is NULL\n", __func__);
233                 return -1;
234         }
235
236         rte_port_ring_writer_ipv4_ras_flush(port);
237         rte_ip_frag_table_destroy(p->frag_tbl);
238         rte_free(port);
239
240         return 0;
241 }
242
243 /*
244  * Summary of port operations
245  */
246 struct rte_port_out_ops rte_port_ring_writer_ipv4_ras_ops = {
247         .f_create = rte_port_ring_writer_ipv4_ras_create,
248         .f_free = rte_port_ring_writer_ipv4_ras_free,
249         .f_tx = rte_port_ring_writer_ipv4_ras_tx,
250         .f_tx_bulk = rte_port_ring_writer_ipv4_ras_tx_bulk,
251         .f_flush = rte_port_ring_writer_ipv4_ras_flush,
252 };