15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#include <rte_eal.h>
355129044dSC.J. Collier
365129044dSC.J. Collier#include <rte_common.h>
375129044dSC.J. Collier#include <rte_debug.h>
385129044dSC.J. Collier#include <rte_errno.h>
395129044dSC.J. Collier#include <rte_ethdev.h>
405129044dSC.J. Collier#include <rte_launch.h>
415129044dSC.J. Collier#include <rte_lcore.h>
425129044dSC.J. Collier#include <rte_log.h>
435129044dSC.J. Collier#include <rte_mbuf.h>
445129044dSC.J. Collier#include <rte_ring.h>
455129044dSC.J. Collier
465129044dSC.J. Collier#include <rte_byteorder.h>
475129044dSC.J. Collier
485129044dSC.J. Collier#include "args.h"
495129044dSC.J. Collier#include "main.h"
505129044dSC.J. Collier#include "init.h"
515129044dSC.J. Collier#include "../include/conf.h"
525129044dSC.J. Collier
535129044dSC.J. Collier
545129044dSC.J. Collier#ifdef QW_SOFTWARE_FC
555129044dSC.J. Collier#define SEND_PAUSE_FRAME(port_id, duration) send_pause_frame(port_id, duration)
565129044dSC.J. Collier#else
575129044dSC.J. Collier#define SEND_PAUSE_FRAME(port_id, duration) do { } while(0)
585129044dSC.J. Collier#endif
595129044dSC.J. Collier
605129044dSC.J. Collier#define ETHER_TYPE_FLOW_CONTROL 0x8808
615129044dSC.J. Collier
625129044dSC.J. Collierstruct ether_fc_frame {
635129044dSC.J. Collier    uint16_t opcode;
645129044dSC.J. Collier    uint16_t param;
655129044dSC.J. Collier} __attribute__((__packed__));
665129044dSC.J. Collier
675129044dSC.J. Collier
685129044dSC.J. Collierint *quota;
695129044dSC.J. Collierunsigned int *low_watermark;
705129044dSC.J. Collier
715129044dSC.J. Collieruint8_t port_pairs[RTE_MAX_ETHPORTS];
725129044dSC.J. Collier
735129044dSC.J. Collierstruct rte_ring *rings[RTE_MAX_LCORE][RTE_MAX_ETHPORTS];
745129044dSC.J. Collierstruct rte_mempool *mbuf_pool;
755129044dSC.J. Collier
765129044dSC.J. Collier
775129044dSC.J. Collierstatic void send_pause_frame(uint8_t port_id, uint16_t duration)
785129044dSC.J. Collier{
795129044dSC.J. Collier    struct rte_mbuf *mbuf;
805129044dSC.J. Collier    struct ether_fc_frame *pause_frame;
815129044dSC.J. Collier    struct ether_hdr *hdr;
825129044dSC.J. Collier    struct ether_addr mac_addr;
835129044dSC.J. Collier
845129044dSC.J. Collier    RTE_LOG(DEBUG, USER1, "Sending PAUSE frame (duration=%d) on port %d\n",
855129044dSC.J. Collier            duration, port_id);
865129044dSC.J. Collier
875129044dSC.J. Collier    /* Get a mbuf from the pool */
885129044dSC.J. Collier    mbuf = rte_pktmbuf_alloc(mbuf_pool);
895129044dSC.J. Collier    if (unlikely(mbuf == NULL))
905129044dSC.J. Collier        return;
915129044dSC.J. Collier
925129044dSC.J. Collier    /* Prepare a PAUSE frame */
935129044dSC.J. Collier    hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
945129044dSC.J. Collier    pause_frame = (struct ether_fc_frame *) &hdr[1];
955129044dSC.J. Collier
965129044dSC.J. Collier    rte_eth_macaddr_get(port_id, &mac_addr);
975129044dSC.J. Collier    ether_addr_copy(&mac_addr, &hdr->s_addr);
985129044dSC.J. Collier
995129044dSC.J. Collier    void *tmp = &hdr->d_addr.addr_bytes[0];
1005129044dSC.J. Collier    *((uint64_t *)tmp) = 0x010000C28001ULL;
1015129044dSC.J. Collier
1025129044dSC.J. Collier    hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_FLOW_CONTROL);
1035129044dSC.J. Collier
1045129044dSC.J. Collier    pause_frame->opcode = rte_cpu_to_be_16(0x0001);
1055129044dSC.J. Collier    pause_frame->param  = rte_cpu_to_be_16(duration);
1065129044dSC.J. Collier
1075129044dSC.J. Collier    mbuf->pkt_len  = 60;
1085129044dSC.J. Collier    mbuf->data_len = 60;
1095129044dSC.J. Collier
1105129044dSC.J. Collier    rte_eth_tx_burst(port_id, 0, &mbuf, 1);
1115129044dSC.J. Collier}
1125129044dSC.J. Collier
1135129044dSC.J. Collier/**
1145129044dSC.J. Collier * Get the previous enabled lcore ID
1155129044dSC.J. Collier *
1165129044dSC.J. Collier * @param lcore_id
1175129044dSC.J. Collier *   The current lcore ID.
1185129044dSC.J. Collier * @return
1195129044dSC.J. Collier *   The previous enabled lcore_id or -1 if not found.
1205129044dSC.J. Collier */
1215129044dSC.J. Collierstatic unsigned int
1225129044dSC.J. Collierget_previous_lcore_id(unsigned int lcore_id)
1235129044dSC.J. Collier{
1245129044dSC.J. Collier    int i;
1255129044dSC.J. Collier
1265129044dSC.J. Collier    for (i = lcore_id - 1; i >= 0; i--)
1275129044dSC.J. Collier        if (rte_lcore_is_enabled(i))
1285129044dSC.J. Collier            return i;
1295129044dSC.J. Collier
1305129044dSC.J. Collier    return -1;
1315129044dSC.J. Collier}
1325129044dSC.J. Collier
1335129044dSC.J. Collier/**
1345129044dSC.J. Collier * Get the last enabled lcore ID
1355129044dSC.J. Collier *
1365129044dSC.J. Collier * @return
1375129044dSC.J. Collier *   The last enabled lcore_id.
1385129044dSC.J. Collier */
1395129044dSC.J. Collierstatic unsigned int
1405129044dSC.J. Collierget_last_lcore_id(void)
1415129044dSC.J. Collier{
1425129044dSC.J. Collier    int i;
1435129044dSC.J. Collier
1445129044dSC.J. Collier    for (i = RTE_MAX_LCORE; i >= 0; i--)
1455129044dSC.J. Collier        if (rte_lcore_is_enabled(i))
1465129044dSC.J. Collier            return i;
1475129044dSC.J. Collier
1485129044dSC.J. Collier    return 0;
1495129044dSC.J. Collier}
1505129044dSC.J. Collier
1515129044dSC.J. Collierstatic void
1525129044dSC.J. Collierreceive_stage(__attribute__((unused)) void *args)
1535129044dSC.J. Collier{
1545129044dSC.J. Collier    int i, ret;
1555129044dSC.J. Collier
1565129044dSC.J. Collier    uint8_t port_id;
1575129044dSC.J. Collier    uint16_t nb_rx_pkts;
1585129044dSC.J. Collier
1595129044dSC.J. Collier    unsigned int lcore_id;
1605129044dSC.J. Collier
1615129044dSC.J. Collier    struct rte_mbuf *pkts[MAX_PKT_QUOTA];
1625129044dSC.J. Collier    struct rte_ring *ring;
1635129044dSC.J. Collier    enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
1645129044dSC.J. Collier
1655129044dSC.J. Collier    lcore_id = rte_lcore_id();
1665129044dSC.J. Collier
1675129044dSC.J. Collier    RTE_LOG(INFO, USER1,
1685129044dSC.J. Collier            "%s() started on core %u\n", __func__, lcore_id);
1695129044dSC.J. Collier
1705129044dSC.J. Collier    while (1) {
1715129044dSC.J. Collier
1725129044dSC.J. Collier        /* Process each port round robin style */
1735129044dSC.J. Collier        for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
1745129044dSC.J. Collier
1755129044dSC.J. Collier            if (!is_bit_set(port_id, portmask))
1765129044dSC.J. Collier                continue;
1775129044dSC.J. Collier
1785129044dSC.J. Collier            ring = rings[lcore_id][port_id];
1795129044dSC.J. Collier
1805129044dSC.J. Collier            if (ring_state[port_id] != RING_READY) {
1815129044dSC.J. Collier                if (rte_ring_count(ring) > *low_watermark)
1825129044dSC.J. Collier                    continue;
1835129044dSC.J. Collier                else
1845129044dSC.J. Collier                    ring_state[port_id] = RING_READY;
1855129044dSC.J. Collier            }
1865129044dSC.J. Collier
1875129044dSC.J. Collier            /* Enqueue received packets on the RX ring */
1885129044dSC.J. Collier            nb_rx_pkts = rte_eth_rx_burst(port_id, 0, pkts, (uint16_t) *quota);
1895129044dSC.J. Collier            ret = rte_ring_enqueue_bulk(ring, (void *) pkts, nb_rx_pkts);
1905129044dSC.J. Collier            if (ret == -EDQUOT) {
1915129044dSC.J. Collier                ring_state[port_id] = RING_OVERLOADED;
1925129044dSC.J. Collier                send_pause_frame(port_id, 1337);
1935129044dSC.J. Collier            }
1945129044dSC.J. Collier
1955129044dSC.J. Collier            else if (ret == -ENOBUFS) {
1965129044dSC.J. Collier
1975129044dSC.J. Collier                /* Return  mbufs to the pool, effectively dropping packets */
1985129044dSC.J. Collier                for (i = 0; i < nb_rx_pkts; i++)
1995129044dSC.J. Collier                    rte_pktmbuf_free(pkts[i]);
2005129044dSC.J. Collier            }
2015129044dSC.J. Collier        }
2025129044dSC.J. Collier    }
2035129044dSC.J. Collier}
2045129044dSC.J. Collier
2055129044dSC.J. Collierstatic void
2065129044dSC.J. Collierpipeline_stage(__attribute__((unused)) void *args)
2075129044dSC.J. Collier{
2085129044dSC.J. Collier    int i, ret;
2095129044dSC.J. Collier    int nb_dq_pkts;
2105129044dSC.J. Collier
2115129044dSC.J. Collier    uint8_t port_id;
2125129044dSC.J. Collier
2135129044dSC.J. Collier    unsigned int lcore_id, previous_lcore_id;
2145129044dSC.J. Collier
2155129044dSC.J. Collier    void *pkts[MAX_PKT_QUOTA];
2165129044dSC.J. Collier    struct rte_ring *rx, *tx;
2175129044dSC.J. Collier    enum ring_state ring_state[RTE_MAX_ETHPORTS] = { RING_READY };
2185129044dSC.J. Collier
2195129044dSC.J. Collier    lcore_id = rte_lcore_id();
2205129044dSC.J. Collier    previous_lcore_id = get_previous_lcore_id(lcore_id);
2215129044dSC.J. Collier
2225129044dSC.J. Collier    RTE_LOG(INFO, USER1,
2235129044dSC.J. Collier            "%s() started on core %u - processing packets from core %u\n",
2245129044dSC.J. Collier            __func__, lcore_id, previous_lcore_id);
2255129044dSC.J. Collier
2265129044dSC.J. Collier    while (1) {
2275129044dSC.J. Collier
2285129044dSC.J. Collier        for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
2295129044dSC.J. Collier
2305129044dSC.J. Collier            if (!is_bit_set(port_id, portmask))
2315129044dSC.J. Collier                continue;
2325129044dSC.J. Collier
2335129044dSC.J. Collier            tx = rings[lcore_id][port_id];
2345129044dSC.J. Collier            rx = rings[previous_lcore_id][port_id];
2355129044dSC.J. Collier
2365129044dSC.J. Collier            if (ring_state[port_id] != RING_READY) {
2375129044dSC.J. Collier                if (rte_ring_count(tx) > *low_watermark)
2385129044dSC.J. Collier                    continue;
2395129044dSC.J. Collier                else
2405129044dSC.J. Collier                    ring_state[port_id] = RING_READY;
2415129044dSC.J. Collier            }
2425129044dSC.J. Collier
2435129044dSC.J. Collier            /* Dequeue up to quota mbuf from rx */
2445129044dSC.J. Collier            nb_dq_pkts = rte_ring_dequeue_burst(rx, pkts, *quota);
2455129044dSC.J. Collier            if (unlikely(nb_dq_pkts < 0))
2465129044dSC.J. Collier                continue;
2475129044dSC.J. Collier
2485129044dSC.J. Collier            /* Enqueue them on tx */
2495129044dSC.J. Collier            ret = rte_ring_enqueue_bulk(tx, pkts, nb_dq_pkts);
2505129044dSC.J. Collier            if (ret == -EDQUOT)
2515129044dSC.J. Collier                ring_state[port_id] = RING_OVERLOADED;
2525129044dSC.J. Collier
2535129044dSC.J. Collier            else if (ret == -ENOBUFS) {
2545129044dSC.J. Collier
2555129044dSC.J. Collier                /* Return  mbufs to the pool, effectively dropping packets */
2565129044dSC.J. Collier                for (i = 0; i < nb_dq_pkts; i++)
2575129044dSC.J. Collier                    rte_pktmbuf_free(pkts[i]);
2585129044dSC.J. Collier            }
2595129044dSC.J. Collier        }
2605129044dSC.J. Collier    }
2615129044dSC.J. Collier}
2625129044dSC.J. Collier
2635129044dSC.J. Collierstatic void
2645129044dSC.J. Colliersend_stage(__attribute__((unused)) void *args)
2655129044dSC.J. Collier{
2665129044dSC.J. Collier	uint16_t nb_dq_pkts;
2675129044dSC.J. Collier
2685129044dSC.J. Collier    uint8_t port_id;
2695129044dSC.J. Collier    uint8_t dest_port_id;
2705129044dSC.J. Collier
2715129044dSC.J. Collier    unsigned int lcore_id, previous_lcore_id;
2725129044dSC.J. Collier
2735129044dSC.J. Collier    struct rte_ring *tx;
2745129044dSC.J. Collier    struct rte_mbuf *tx_pkts[MAX_PKT_QUOTA];
2755129044dSC.J. Collier
2765129044dSC.J. Collier    lcore_id = rte_lcore_id();
2775129044dSC.J. Collier    previous_lcore_id = get_previous_lcore_id(lcore_id);
2785129044dSC.J. Collier
2795129044dSC.J. Collier    RTE_LOG(INFO, USER1,
2805129044dSC.J. Collier            "%s() started on core %u - processing packets from core %u\n",
2815129044dSC.J. Collier            __func__, lcore_id, previous_lcore_id);
2825129044dSC.J. Collier
2835129044dSC.J. Collier    while (1) {
2845129044dSC.J. Collier
2855129044dSC.J. Collier        /* Process each ring round robin style */
2865129044dSC.J. Collier        for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
2875129044dSC.J. Collier
2885129044dSC.J. Collier            if (!is_bit_set(port_id, portmask))
2895129044dSC.J. Collier                continue;
2905129044dSC.J. Collier
2915129044dSC.J. Collier            dest_port_id = port_pairs[port_id];
2925129044dSC.J. Collier            tx = rings[previous_lcore_id][port_id];
2935129044dSC.J. Collier
2945129044dSC.J. Collier            if (rte_ring_empty(tx))
2955129044dSC.J. Collier                continue;
2965129044dSC.J. Collier
2975129044dSC.J. Collier            /* Dequeue packets from tx and send them */
2985129044dSC.J. Collier            nb_dq_pkts = (uint16_t) rte_ring_dequeue_burst(tx, (void *) tx_pkts, *quota);
2995129044dSC.J. Collier            rte_eth_tx_burst(dest_port_id, 0, tx_pkts, nb_dq_pkts);
3005129044dSC.J. Collier
3015129044dSC.J. Collier            /* TODO: Check if nb_dq_pkts == nb_tx_pkts? */
3025129044dSC.J. Collier        }
3035129044dSC.J. Collier    }
3045129044dSC.J. Collier}
3055129044dSC.J. Collier
3065129044dSC.J. Collierint
3075129044dSC.J. Colliermain(int argc, char **argv)
3085129044dSC.J. Collier{
3095129044dSC.J. Collier    int ret;
3105129044dSC.J. Collier    unsigned int lcore_id, master_lcore_id, last_lcore_id;
3115129044dSC.J. Collier
3125129044dSC.J. Collier    uint8_t port_id;
3135129044dSC.J. Collier
3145129044dSC.J. Collier    rte_set_log_level(RTE_LOG_INFO);
3155129044dSC.J. Collier
3165129044dSC.J. Collier    ret = rte_eal_init(argc, argv);
3175129044dSC.J. Collier    if (ret < 0)
3185129044dSC.J. Collier        rte_exit(EXIT_FAILURE, "Cannot initialize EAL\n");
3195129044dSC.J. Collier
3205129044dSC.J. Collier    argc -= ret;
3215129044dSC.J. Collier    argv += ret;
3225129044dSC.J. Collier
3235129044dSC.J. Collier    init_dpdk();
3245129044dSC.J. Collier    setup_shared_variables();
3255129044dSC.J. Collier
3265129044dSC.J. Collier    *quota = 32;
3275129044dSC.J. Collier    *low_watermark = 60 * RING_SIZE / 100;
3285129044dSC.J. Collier
3295129044dSC.J. Collier    last_lcore_id   = get_last_lcore_id();
3305129044dSC.J. Collier    master_lcore_id = rte_get_master_lcore();
3315129044dSC.J. Collier
3325129044dSC.J. Collier    /* Parse the application's arguments */
3335129044dSC.J. Collier    ret = parse_qw_args(argc, argv);
3345129044dSC.J. Collier    if (ret < 0)
3355129044dSC.J. Collier        rte_exit(EXIT_FAILURE, "Invalid quota/watermark argument(s)\n");
3365129044dSC.J. Collier
3375129044dSC.J. Collier    /* Create a pool of mbuf to store packets */
3385129044dSC.J. Collier    mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, 32, 0,
3395129044dSC.J. Collier	    MBUF_DATA_SIZE, rte_socket_id());
3405129044dSC.J. Collier    if (mbuf_pool == NULL)
3415129044dSC.J. Collier        rte_panic("%s\n", rte_strerror(rte_errno));
3425129044dSC.J. Collier
3435129044dSC.J. Collier    for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
3445129044dSC.J. Collier        if (is_bit_set(port_id, portmask)) {
3455129044dSC.J. Collier            configure_eth_port(port_id);
3465129044dSC.J. Collier            init_ring(master_lcore_id, port_id);
3475129044dSC.J. Collier        }
3485129044dSC.J. Collier
3495129044dSC.J. Collier    pair_ports();
3505129044dSC.J. Collier
3515129044dSC.J. Collier    /* Start pipeline_connect() on all the available slave lcore but the last */
3525129044dSC.J. Collier    for (lcore_id = 0 ; lcore_id < last_lcore_id; lcore_id++) {
3535129044dSC.J. Collier        if (rte_lcore_is_enabled(lcore_id) && lcore_id != master_lcore_id) {
3545129044dSC.J. Collier
3555129044dSC.J. Collier            for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
3565129044dSC.J. Collier                if (is_bit_set(port_id, portmask))
3575129044dSC.J. Collier                    init_ring(lcore_id, port_id);
3585129044dSC.J. Collier
3595129044dSC.J. Collier            /* typecast is a workaround for GCC 4.3 bug */
3605129044dSC.J. Collier            rte_eal_remote_launch((int (*)(void *))pipeline_stage, NULL, lcore_id);
3615129044dSC.J. Collier        }
3625129044dSC.J. Collier    }
3635129044dSC.J. Collier
3645129044dSC.J. Collier    /* Start send_stage() on the last slave core */
3655129044dSC.J. Collier    /* typecast is a workaround for GCC 4.3 bug */
3665129044dSC.J. Collier    rte_eal_remote_launch((int (*)(void *))send_stage, NULL, last_lcore_id);
3675129044dSC.J. Collier
3685129044dSC.J. Collier    /* Start receive_stage() on the master core */
3695129044dSC.J. Collier    receive_stage(NULL);
3705129044dSC.J. Collier
3715129044dSC.J. Collier    return 0;
3725129044dSC.J. Collier}
373