On 01/27/2016 07:39 PM, Fan Zhang wrote:
> Originally, sink ports in librte_port releases received mbufs back to
> mempool. This patch adds optional packet dumping to PCAP feature in sink
> port: the packets will be dumped to user defined PCAP file for storage or
> debugging. The user may also choose the sink port's activity: either it
> continuously dump the packets to the file, or stops at certain dumping
>
> This feature shares same CONFIG_RTE_PORT_PCAP compiler option as source
> port PCAP file support feature. Users can enable or disable this feature
> by setting CONFIG_RTE_PORT_PCAP compiler option "y" or "n".
>
> Signed-off-by: Fan Zhang <roy.fan.zhang at intel.com>
> Acked-by: Cristian Dumitrescu <cristian.dumitrescu at intel.com>
> ---
>   lib/librte_port/rte_port_source_sink.c | 268 
> +++++++++++++++++++++++++++++++--
>   lib/librte_port/rte_port_source_sink.h |  11 +-
>   2 files changed, 263 insertions(+), 16 deletions(-)
>
[...]
> +#ifdef RTE_PORT_PCAP
> +
> +/**
> + * Open PCAP file for dumping packets to the file later
> + *
> + * @param port
> + *   Handle to sink port
> + * @param p
> + *   Sink port parameter
> + * @return
> + *   0 on SUCCESS
> + *   error code otherwise
> + */
[...]
> +
> +#else
> +
> +static int
> +pcap_sink_open(struct rte_port_sink *port,
> +             __rte_unused struct rte_port_sink_params *p)
> +{
> +     port->dumper = NULL;
> +     port->max_pkts = 0;
> +     port->pkt_index = 0;
> +     port->dump_finish = 0;
> +
> +     return 0;
> +}

Shouldn't this just return -ENOTSUP instead of success when the pcap 
feature is not built in?

> +
> +static void
> +pcap_sink_dump_pkt(__rte_unused struct rte_port_sink *port,
> +             __rte_unused struct rte_mbuf *mbuf) {}
> +
> +static void
> +pcap_sink_flush_pkt(__rte_unused void *dumper) {}
> +
> +static void
> +pcap_sink_close(__rte_unused void *dumper) {}
> +
> +#endif
> +
>   static void *
>   rte_port_sink_create(__rte_unused void *params, int socket_id)
>   {
>       struct rte_port_sink *port;
> +     struct rte_port_sink_params *p = params;
> +     int status;
>
>       /* Memory allocation */
>       port = rte_zmalloc_socket("PORT", sizeof(*port),
> @@ -360,6 +532,19 @@ rte_port_sink_create(__rte_unused void *params, int 
> socket_id)
>               return NULL;
>       }
>
> +     /* Try to open PCAP file for dumping, if possible */
> +     status = pcap_sink_open(port, p);
> +     if (status < 0) {
> +             RTE_LOG(ERR, PORT, "%s: Failed to enable PCAP support "
> +                             "support\n", __func__);
> +             rte_free(port);
> +             port = NULL;
> +     } else {
> +             if (port->dumper != NULL)
> +                     RTE_LOG(INFO, PORT, "Ready to dump packets to file "
> +                                     "%s\n", p->file_name);
> +     }
> +
>       return port;
>   }
>
> @@ -369,6 +554,8 @@ rte_port_sink_tx(void *port, struct rte_mbuf *pkt)
>       __rte_unused struct rte_port_sink *p = (struct rte_port_sink *) port;
>
>       RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
> +     if (p->dumper != NULL)
> +             pcap_sink_dump_pkt(p, pkt);
>       rte_pktmbuf_free(pkt);
>       RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
>
> @@ -387,21 +574,44 @@ rte_port_sink_tx_bulk(void *port, struct rte_mbuf 
> **pkts,
>
>               RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, n_pkts);
>               RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, n_pkts);
> -             for (i = 0; i < n_pkts; i++) {
> -                     struct rte_mbuf *pkt = pkts[i];
> -
> -                     rte_pktmbuf_free(pkt);
> +             if (p->dumper) {
> +                     for (i = 0; i < n_pkts; i++) {
> +                             struct rte_mbuf *pkt = pkts[i];
> +
> +                             pcap_sink_dump_pkt(p, pkt);
> +                             rte_pktmbuf_free(pkt);
> +                     }
> +             } else {
> +                     for (i = 0; i < n_pkts; i++) {
> +                             struct rte_mbuf *pkt = pkts[i];
> +
> +                             rte_pktmbuf_free(pkt);
> +                     }
>               }
>       } else {
> -             for ( ; pkts_mask; ) {
> -                     uint32_t pkt_index = __builtin_ctzll(pkts_mask);
> -                     uint64_t pkt_mask = 1LLU << pkt_index;
> -                     struct rte_mbuf *pkt = pkts[pkt_index];
> -
> -                     RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
> -                     RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
> -                     rte_pktmbuf_free(pkt);
> -                     pkts_mask &= ~pkt_mask;
> +             if (p->dumper) {
> +                     for ( ; pkts_mask; ) {
> +                             uint32_t pkt_index = __builtin_ctzll(pkts_mask);
> +                             uint64_t pkt_mask = 1LLU << pkt_index;
> +                             struct rte_mbuf *pkt = pkts[pkt_index];
> +
> +                             RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
> +                             RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
> +                             pcap_sink_dump_pkt(p, pkt);
> +                             rte_pktmbuf_free(pkt);
> +                             pkts_mask &= ~pkt_mask;
> +                     }
> +             } else {
> +                     for ( ; pkts_mask; ) {
> +                             uint32_t pkt_index = __builtin_ctzll(pkts_mask);
> +                             uint64_t pkt_mask = 1LLU << pkt_index;
> +                             struct rte_mbuf *pkt = pkts[pkt_index];
> +
> +                             RTE_PORT_SINK_STATS_PKTS_IN_ADD(p, 1);
> +                             RTE_PORT_SINK_STATS_PKTS_DROP_ADD(p, 1);
> +                             rte_pktmbuf_free(pkt);
> +                             pkts_mask &= ~pkt_mask;
> +                     }

These add quite a fair chunk of nearly identical duplicate code, which 
could be easily avoided with an _ops-style function pointer between say, 
null_sink_dump_pkt() which is a no-op function and pcap_sink_dump_pkt().

        - Panu -

Reply via email to