mempool: introduce helpers for populate and required size
[dpdk.git] / lib / librte_pdump / rte_pdump.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Intel Corporation
3  */
4
5 #include <rte_memcpy.h>
6 #include <rte_mbuf.h>
7 #include <rte_ethdev.h>
8 #include <rte_lcore.h>
9 #include <rte_log.h>
10 #include <rte_errno.h>
11 #include <rte_string_fns.h>
12
13 #include "rte_pdump.h"
14
15 #define DEVICE_ID_SIZE 64
16 /* Macros for printing using RTE_LOG */
17 #define RTE_LOGTYPE_PDUMP RTE_LOGTYPE_USER1
18
19 /* Used for the multi-process communication */
20 #define PDUMP_MP        "mp_pdump"
21
22 enum pdump_operation {
23         DISABLE = 1,
24         ENABLE = 2
25 };
26
27 enum pdump_version {
28         V1 = 1
29 };
30
31 struct pdump_request {
32         uint16_t ver;
33         uint16_t op;
34         uint32_t flags;
35         union pdump_data {
36                 struct enable_v1 {
37                         char device[DEVICE_ID_SIZE];
38                         uint16_t queue;
39                         struct rte_ring *ring;
40                         struct rte_mempool *mp;
41                         void *filter;
42                 } en_v1;
43                 struct disable_v1 {
44                         char device[DEVICE_ID_SIZE];
45                         uint16_t queue;
46                         struct rte_ring *ring;
47                         struct rte_mempool *mp;
48                         void *filter;
49                 } dis_v1;
50         } data;
51 };
52
53 struct pdump_response {
54         uint16_t ver;
55         uint16_t res_op;
56         int32_t err_value;
57 };
58
59 static struct pdump_rxtx_cbs {
60         struct rte_ring *ring;
61         struct rte_mempool *mp;
62         const struct rte_eth_rxtx_callback *cb;
63         void *filter;
64 } rx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT],
65 tx_cbs[RTE_MAX_ETHPORTS][RTE_MAX_QUEUES_PER_PORT];
66
67 static inline int
68 pdump_pktmbuf_copy_data(struct rte_mbuf *seg, const struct rte_mbuf *m)
69 {
70         if (rte_pktmbuf_tailroom(seg) < m->data_len) {
71                 RTE_LOG(ERR, PDUMP,
72                         "User mempool: insufficient data_len of mbuf\n");
73                 return -EINVAL;
74         }
75
76         seg->port = m->port;
77         seg->vlan_tci = m->vlan_tci;
78         seg->hash = m->hash;
79         seg->tx_offload = m->tx_offload;
80         seg->ol_flags = m->ol_flags;
81         seg->packet_type = m->packet_type;
82         seg->vlan_tci_outer = m->vlan_tci_outer;
83         seg->data_len = m->data_len;
84         seg->pkt_len = seg->data_len;
85         rte_memcpy(rte_pktmbuf_mtod(seg, void *),
86                         rte_pktmbuf_mtod(m, void *),
87                         rte_pktmbuf_data_len(seg));
88
89         return 0;
90 }
91
92 static inline struct rte_mbuf *
93 pdump_pktmbuf_copy(struct rte_mbuf *m, struct rte_mempool *mp)
94 {
95         struct rte_mbuf *m_dup, *seg, **prev;
96         uint32_t pktlen;
97         uint16_t nseg;
98
99         m_dup = rte_pktmbuf_alloc(mp);
100         if (unlikely(m_dup == NULL))
101                 return NULL;
102
103         seg = m_dup;
104         prev = &seg->next;
105         pktlen = m->pkt_len;
106         nseg = 0;
107
108         do {
109                 nseg++;
110                 if (pdump_pktmbuf_copy_data(seg, m) < 0) {
111                         if (seg != m_dup)
112                                 rte_pktmbuf_free_seg(seg);
113                         rte_pktmbuf_free(m_dup);
114                         return NULL;
115                 }
116                 *prev = seg;
117                 prev = &seg->next;
118         } while ((m = m->next) != NULL &&
119                         (seg = rte_pktmbuf_alloc(mp)) != NULL);
120
121         *prev = NULL;
122         m_dup->nb_segs = nseg;
123         m_dup->pkt_len = pktlen;
124
125         /* Allocation of new indirect segment failed */
126         if (unlikely(seg == NULL)) {
127                 rte_pktmbuf_free(m_dup);
128                 return NULL;
129         }
130
131         __rte_mbuf_sanity_check(m_dup, 1);
132         return m_dup;
133 }
134
135 static inline void
136 pdump_copy(struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
137 {
138         unsigned i;
139         int ring_enq;
140         uint16_t d_pkts = 0;
141         struct rte_mbuf *dup_bufs[nb_pkts];
142         struct pdump_rxtx_cbs *cbs;
143         struct rte_ring *ring;
144         struct rte_mempool *mp;
145         struct rte_mbuf *p;
146
147         cbs  = user_params;
148         ring = cbs->ring;
149         mp = cbs->mp;
150         for (i = 0; i < nb_pkts; i++) {
151                 p = pdump_pktmbuf_copy(pkts[i], mp);
152                 if (p)
153                         dup_bufs[d_pkts++] = p;
154         }
155
156         ring_enq = rte_ring_enqueue_burst(ring, (void *)dup_bufs, d_pkts, NULL);
157         if (unlikely(ring_enq < d_pkts)) {
158                 RTE_LOG(DEBUG, PDUMP,
159                         "only %d of packets enqueued to ring\n", ring_enq);
160                 do {
161                         rte_pktmbuf_free(dup_bufs[ring_enq]);
162                 } while (++ring_enq < d_pkts);
163         }
164 }
165
166 static uint16_t
167 pdump_rx(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
168         struct rte_mbuf **pkts, uint16_t nb_pkts,
169         uint16_t max_pkts __rte_unused,
170         void *user_params)
171 {
172         pdump_copy(pkts, nb_pkts, user_params);
173         return nb_pkts;
174 }
175
176 static uint16_t
177 pdump_tx(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
178                 struct rte_mbuf **pkts, uint16_t nb_pkts, void *user_params)
179 {
180         pdump_copy(pkts, nb_pkts, user_params);
181         return nb_pkts;
182 }
183
184 static int
185 pdump_register_rx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
186                                 struct rte_ring *ring, struct rte_mempool *mp,
187                                 uint16_t operation)
188 {
189         uint16_t qid;
190         struct pdump_rxtx_cbs *cbs = NULL;
191
192         qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
193         for (; qid < end_q; qid++) {
194                 cbs = &rx_cbs[port][qid];
195                 if (cbs && operation == ENABLE) {
196                         if (cbs->cb) {
197                                 RTE_LOG(ERR, PDUMP,
198                                         "failed to add rx callback for port=%d "
199                                         "and queue=%d, callback already exists\n",
200                                         port, qid);
201                                 return -EEXIST;
202                         }
203                         cbs->ring = ring;
204                         cbs->mp = mp;
205                         cbs->cb = rte_eth_add_first_rx_callback(port, qid,
206                                                                 pdump_rx, cbs);
207                         if (cbs->cb == NULL) {
208                                 RTE_LOG(ERR, PDUMP,
209                                         "failed to add rx callback, errno=%d\n",
210                                         rte_errno);
211                                 return rte_errno;
212                         }
213                 }
214                 if (cbs && operation == DISABLE) {
215                         int ret;
216
217                         if (cbs->cb == NULL) {
218                                 RTE_LOG(ERR, PDUMP,
219                                         "failed to delete non existing rx "
220                                         "callback for port=%d and queue=%d\n",
221                                         port, qid);
222                                 return -EINVAL;
223                         }
224                         ret = rte_eth_remove_rx_callback(port, qid, cbs->cb);
225                         if (ret < 0) {
226                                 RTE_LOG(ERR, PDUMP,
227                                         "failed to remove rx callback, errno=%d\n",
228                                         -ret);
229                                 return ret;
230                         }
231                         cbs->cb = NULL;
232                 }
233         }
234
235         return 0;
236 }
237
238 static int
239 pdump_register_tx_callbacks(uint16_t end_q, uint16_t port, uint16_t queue,
240                                 struct rte_ring *ring, struct rte_mempool *mp,
241                                 uint16_t operation)
242 {
243
244         uint16_t qid;
245         struct pdump_rxtx_cbs *cbs = NULL;
246
247         qid = (queue == RTE_PDUMP_ALL_QUEUES) ? 0 : queue;
248         for (; qid < end_q; qid++) {
249                 cbs = &tx_cbs[port][qid];
250                 if (cbs && operation == ENABLE) {
251                         if (cbs->cb) {
252                                 RTE_LOG(ERR, PDUMP,
253                                         "failed to add tx callback for port=%d "
254                                         "and queue=%d, callback already exists\n",
255                                         port, qid);
256                                 return -EEXIST;
257                         }
258                         cbs->ring = ring;
259                         cbs->mp = mp;
260                         cbs->cb = rte_eth_add_tx_callback(port, qid, pdump_tx,
261                                                                 cbs);
262                         if (cbs->cb == NULL) {
263                                 RTE_LOG(ERR, PDUMP,
264                                         "failed to add tx callback, errno=%d\n",
265                                         rte_errno);
266                                 return rte_errno;
267                         }
268                 }
269                 if (cbs && operation == DISABLE) {
270                         int ret;
271
272                         if (cbs->cb == NULL) {
273                                 RTE_LOG(ERR, PDUMP,
274                                         "failed to delete non existing tx "
275                                         "callback for port=%d and queue=%d\n",
276                                         port, qid);
277                                 return -EINVAL;
278                         }
279                         ret = rte_eth_remove_tx_callback(port, qid, cbs->cb);
280                         if (ret < 0) {
281                                 RTE_LOG(ERR, PDUMP,
282                                         "failed to remove tx callback, errno=%d\n",
283                                         -ret);
284                                 return ret;
285                         }
286                         cbs->cb = NULL;
287                 }
288         }
289
290         return 0;
291 }
292
293 static int
294 set_pdump_rxtx_cbs(const struct pdump_request *p)
295 {
296         uint16_t nb_rx_q = 0, nb_tx_q = 0, end_q, queue;
297         uint16_t port;
298         int ret = 0;
299         uint32_t flags;
300         uint16_t operation;
301         struct rte_ring *ring;
302         struct rte_mempool *mp;
303
304         flags = p->flags;
305         operation = p->op;
306         if (operation == ENABLE) {
307                 ret = rte_eth_dev_get_port_by_name(p->data.en_v1.device,
308                                 &port);
309                 if (ret < 0) {
310                         RTE_LOG(ERR, PDUMP,
311                                 "failed to get port id for device id=%s\n",
312                                 p->data.en_v1.device);
313                         return -EINVAL;
314                 }
315                 queue = p->data.en_v1.queue;
316                 ring = p->data.en_v1.ring;
317                 mp = p->data.en_v1.mp;
318         } else {
319                 ret = rte_eth_dev_get_port_by_name(p->data.dis_v1.device,
320                                 &port);
321                 if (ret < 0) {
322                         RTE_LOG(ERR, PDUMP,
323                                 "failed to get port id for device id=%s\n",
324                                 p->data.dis_v1.device);
325                         return -EINVAL;
326                 }
327                 queue = p->data.dis_v1.queue;
328                 ring = p->data.dis_v1.ring;
329                 mp = p->data.dis_v1.mp;
330         }
331
332         /* validation if packet capture is for all queues */
333         if (queue == RTE_PDUMP_ALL_QUEUES) {
334                 struct rte_eth_dev_info dev_info;
335
336                 ret = rte_eth_dev_info_get(port, &dev_info);
337                 if (ret != 0) {
338                         RTE_LOG(ERR, PDUMP,
339                                 "Error during getting device (port %u) info: %s\n",
340                                 port, strerror(-ret));
341                         return ret;
342                 }
343
344                 nb_rx_q = dev_info.nb_rx_queues;
345                 nb_tx_q = dev_info.nb_tx_queues;
346                 if (nb_rx_q == 0 && flags & RTE_PDUMP_FLAG_RX) {
347                         RTE_LOG(ERR, PDUMP,
348                                 "number of rx queues cannot be 0\n");
349                         return -EINVAL;
350                 }
351                 if (nb_tx_q == 0 && flags & RTE_PDUMP_FLAG_TX) {
352                         RTE_LOG(ERR, PDUMP,
353                                 "number of tx queues cannot be 0\n");
354                         return -EINVAL;
355                 }
356                 if ((nb_tx_q == 0 || nb_rx_q == 0) &&
357                         flags == RTE_PDUMP_FLAG_RXTX) {
358                         RTE_LOG(ERR, PDUMP,
359                                 "both tx&rx queues must be non zero\n");
360                         return -EINVAL;
361                 }
362         }
363
364         /* register RX callback */
365         if (flags & RTE_PDUMP_FLAG_RX) {
366                 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_rx_q : queue + 1;
367                 ret = pdump_register_rx_callbacks(end_q, port, queue, ring, mp,
368                                                         operation);
369                 if (ret < 0)
370                         return ret;
371         }
372
373         /* register TX callback */
374         if (flags & RTE_PDUMP_FLAG_TX) {
375                 end_q = (queue == RTE_PDUMP_ALL_QUEUES) ? nb_tx_q : queue + 1;
376                 ret = pdump_register_tx_callbacks(end_q, port, queue, ring, mp,
377                                                         operation);
378                 if (ret < 0)
379                         return ret;
380         }
381
382         return ret;
383 }
384
385 static int
386 pdump_server(const struct rte_mp_msg *mp_msg, const void *peer)
387 {
388         struct rte_mp_msg mp_resp;
389         const struct pdump_request *cli_req;
390         struct pdump_response *resp = (struct pdump_response *)&mp_resp.param;
391
392         /* recv client requests */
393         if (mp_msg->len_param != sizeof(*cli_req)) {
394                 RTE_LOG(ERR, PDUMP, "failed to recv from client\n");
395                 resp->err_value = -EINVAL;
396         } else {
397                 cli_req = (const struct pdump_request *)mp_msg->param;
398                 resp->ver = cli_req->ver;
399                 resp->res_op = cli_req->op;
400                 resp->err_value = set_pdump_rxtx_cbs(cli_req);
401         }
402
403         strlcpy(mp_resp.name, PDUMP_MP, RTE_MP_MAX_NAME_LEN);
404         mp_resp.len_param = sizeof(*resp);
405         mp_resp.num_fds = 0;
406         if (rte_mp_reply(&mp_resp, peer) < 0) {
407                 RTE_LOG(ERR, PDUMP, "failed to send to client:%s, %s:%d\n",
408                         strerror(rte_errno), __func__, __LINE__);
409                 return -1;
410         }
411
412         return 0;
413 }
414
415 int
416 rte_pdump_init(void)
417 {
418         int ret = rte_mp_action_register(PDUMP_MP, pdump_server);
419         if (ret && rte_errno != ENOTSUP)
420                 return -1;
421         return 0;
422 }
423
424 int
425 rte_pdump_uninit(void)
426 {
427         rte_mp_action_unregister(PDUMP_MP);
428
429         return 0;
430 }
431
432 static int
433 pdump_validate_ring_mp(struct rte_ring *ring, struct rte_mempool *mp)
434 {
435         if (ring == NULL || mp == NULL) {
436                 RTE_LOG(ERR, PDUMP, "NULL ring or mempool are passed %s:%d\n",
437                         __func__, __LINE__);
438                 rte_errno = EINVAL;
439                 return -1;
440         }
441         if (mp->flags & MEMPOOL_F_SP_PUT || mp->flags & MEMPOOL_F_SC_GET) {
442                 RTE_LOG(ERR, PDUMP, "mempool with either SP or SC settings"
443                 " is not valid for pdump, should have MP and MC settings\n");
444                 rte_errno = EINVAL;
445                 return -1;
446         }
447         if (ring->prod.single || ring->cons.single) {
448                 RTE_LOG(ERR, PDUMP, "ring with either SP or SC settings"
449                 " is not valid for pdump, should have MP and MC settings\n");
450                 rte_errno = EINVAL;
451                 return -1;
452         }
453
454         return 0;
455 }
456
457 static int
458 pdump_validate_flags(uint32_t flags)
459 {
460         if (flags != RTE_PDUMP_FLAG_RX && flags != RTE_PDUMP_FLAG_TX &&
461                 flags != RTE_PDUMP_FLAG_RXTX) {
462                 RTE_LOG(ERR, PDUMP,
463                         "invalid flags, should be either rx/tx/rxtx\n");
464                 rte_errno = EINVAL;
465                 return -1;
466         }
467
468         return 0;
469 }
470
471 static int
472 pdump_validate_port(uint16_t port, char *name)
473 {
474         int ret = 0;
475
476         if (port >= RTE_MAX_ETHPORTS) {
477                 RTE_LOG(ERR, PDUMP, "Invalid port id %u, %s:%d\n", port,
478                         __func__, __LINE__);
479                 rte_errno = EINVAL;
480                 return -1;
481         }
482
483         ret = rte_eth_dev_get_name_by_port(port, name);
484         if (ret < 0) {
485                 RTE_LOG(ERR, PDUMP,
486                         "port id to name mapping failed for port id=%u, %s:%d\n",
487                         port, __func__, __LINE__);
488                 rte_errno = EINVAL;
489                 return -1;
490         }
491
492         return 0;
493 }
494
495 static int
496 pdump_prepare_client_request(char *device, uint16_t queue,
497                                 uint32_t flags,
498                                 uint16_t operation,
499                                 struct rte_ring *ring,
500                                 struct rte_mempool *mp,
501                                 void *filter)
502 {
503         int ret = -1;
504         struct rte_mp_msg mp_req, *mp_rep;
505         struct rte_mp_reply mp_reply;
506         struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
507         struct pdump_request *req = (struct pdump_request *)mp_req.param;
508         struct pdump_response *resp;
509
510         req->ver = 1;
511         req->flags = flags;
512         req->op = operation;
513         if ((operation & ENABLE) != 0) {
514                 strlcpy(req->data.en_v1.device, device,
515                         sizeof(req->data.en_v1.device));
516                 req->data.en_v1.queue = queue;
517                 req->data.en_v1.ring = ring;
518                 req->data.en_v1.mp = mp;
519                 req->data.en_v1.filter = filter;
520         } else {
521                 strlcpy(req->data.dis_v1.device, device,
522                         sizeof(req->data.dis_v1.device));
523                 req->data.dis_v1.queue = queue;
524                 req->data.dis_v1.ring = NULL;
525                 req->data.dis_v1.mp = NULL;
526                 req->data.dis_v1.filter = NULL;
527         }
528
529         strlcpy(mp_req.name, PDUMP_MP, RTE_MP_MAX_NAME_LEN);
530         mp_req.len_param = sizeof(*req);
531         mp_req.num_fds = 0;
532         if (rte_mp_request_sync(&mp_req, &mp_reply, &ts) == 0) {
533                 mp_rep = &mp_reply.msgs[0];
534                 resp = (struct pdump_response *)mp_rep->param;
535                 rte_errno = resp->err_value;
536                 if (!resp->err_value)
537                         ret = 0;
538                 free(mp_reply.msgs);
539         }
540
541         if (ret < 0)
542                 RTE_LOG(ERR, PDUMP,
543                         "client request for pdump enable/disable failed\n");
544         return ret;
545 }
546
547 int
548 rte_pdump_enable(uint16_t port, uint16_t queue, uint32_t flags,
549                         struct rte_ring *ring,
550                         struct rte_mempool *mp,
551                         void *filter)
552 {
553
554         int ret = 0;
555         char name[DEVICE_ID_SIZE];
556
557         ret = pdump_validate_port(port, name);
558         if (ret < 0)
559                 return ret;
560         ret = pdump_validate_ring_mp(ring, mp);
561         if (ret < 0)
562                 return ret;
563         ret = pdump_validate_flags(flags);
564         if (ret < 0)
565                 return ret;
566
567         ret = pdump_prepare_client_request(name, queue, flags,
568                                                 ENABLE, ring, mp, filter);
569
570         return ret;
571 }
572
573 int
574 rte_pdump_enable_by_deviceid(char *device_id, uint16_t queue,
575                                 uint32_t flags,
576                                 struct rte_ring *ring,
577                                 struct rte_mempool *mp,
578                                 void *filter)
579 {
580         int ret = 0;
581
582         ret = pdump_validate_ring_mp(ring, mp);
583         if (ret < 0)
584                 return ret;
585         ret = pdump_validate_flags(flags);
586         if (ret < 0)
587                 return ret;
588
589         ret = pdump_prepare_client_request(device_id, queue, flags,
590                                                 ENABLE, ring, mp, filter);
591
592         return ret;
593 }
594
595 int
596 rte_pdump_disable(uint16_t port, uint16_t queue, uint32_t flags)
597 {
598         int ret = 0;
599         char name[DEVICE_ID_SIZE];
600
601         ret = pdump_validate_port(port, name);
602         if (ret < 0)
603                 return ret;
604         ret = pdump_validate_flags(flags);
605         if (ret < 0)
606                 return ret;
607
608         ret = pdump_prepare_client_request(name, queue, flags,
609                                                 DISABLE, NULL, NULL, NULL);
610
611         return ret;
612 }
613
614 int
615 rte_pdump_disable_by_deviceid(char *device_id, uint16_t queue,
616                                 uint32_t flags)
617 {
618         int ret = 0;
619
620         ret = pdump_validate_flags(flags);
621         if (ret < 0)
622                 return ret;
623
624         ret = pdump_prepare_client_request(device_id, queue, flags,
625                                                 DISABLE, NULL, NULL, NULL);
626
627         return ret;
628 }