add prefix to cache line macros
[dpdk.git] / lib / librte_port / rte_port_source_sink.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 <stdint.h>
34 #include <string.h>
35
36 #include <rte_mbuf.h>
37 #include <rte_mempool.h>
38 #include <rte_malloc.h>
39
40 #include "rte_port_source_sink.h"
41
42 /*
43  * Port SOURCE
44  */
45 struct rte_port_source {
46         struct rte_mempool *mempool;
47 };
48
49 static void *
50 rte_port_source_create(void *params, int socket_id)
51 {
52         struct rte_port_source_params *p =
53                         (struct rte_port_source_params *) params;
54         struct rte_port_source *port;
55
56         /* Check input arguments*/
57         if ((p == NULL) || (p->mempool == NULL)) {
58                 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
59                 return NULL;
60         }
61
62         /* Memory allocation */
63         port = rte_zmalloc_socket("PORT", sizeof(*port),
64                         RTE_CACHE_LINE_SIZE, socket_id);
65         if (port == NULL) {
66                 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
67                 return NULL;
68         }
69
70         /* Initialization */
71         port->mempool = (struct rte_mempool *) p->mempool;
72
73         return port;
74 }
75
76 static int
77 rte_port_source_free(void *port)
78 {
79         /* Check input parameters */
80         if (port == NULL)
81                 return 0;
82
83         rte_free(port);
84
85         return 0;
86 }
87
88 static int
89 rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
90 {
91         struct rte_port_source *p = (struct rte_port_source *) port;
92
93         if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
94                 return 0;
95
96         return n_pkts;
97 }
98
99 /*
100  * Port SINK
101  */
102 static void *
103 rte_port_sink_create(__rte_unused void *params, __rte_unused int socket_id)
104 {
105         return (void *) 1;
106 }
107
108 static int
109 rte_port_sink_tx(__rte_unused void *port, struct rte_mbuf *pkt)
110 {
111         rte_pktmbuf_free(pkt);
112
113         return 0;
114 }
115
116 static int
117 rte_port_sink_tx_bulk(__rte_unused void *port, struct rte_mbuf **pkts,
118         uint64_t pkts_mask)
119 {
120         if ((pkts_mask & (pkts_mask + 1)) == 0) {
121                 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
122                 uint32_t i;
123
124                 for (i = 0; i < n_pkts; i++) {
125                         struct rte_mbuf *pkt = pkts[i];
126
127                         rte_pktmbuf_free(pkt);
128                 }
129         } else {
130                 for ( ; pkts_mask; ) {
131                         uint32_t pkt_index = __builtin_ctzll(pkts_mask);
132                         uint64_t pkt_mask = 1LLU << pkt_index;
133                         struct rte_mbuf *pkt = pkts[pkt_index];
134
135                         rte_pktmbuf_free(pkt);
136                         pkts_mask &= ~pkt_mask;
137                 }
138         }
139
140         return 0;
141 }
142
143 /*
144  * Summary of port operations
145  */
146 struct rte_port_in_ops rte_port_source_ops = {
147         .f_create = rte_port_source_create,
148         .f_free = rte_port_source_free,
149         .f_rx = rte_port_source_rx,
150 };
151
152 struct rte_port_out_ops rte_port_sink_ops = {
153         .f_create = rte_port_sink_create,
154         .f_free = NULL,
155         .f_tx = rte_port_sink_tx,
156         .f_tx_bulk = rte_port_sink_tx_bulk,
157         .f_flush = NULL,
158 };