2087ea75a9fa6f23328fbc2756b5a62333a12d96
[o-du/phy.git] / fhi_lib / lib / ethernet / ethdi.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @brief This file has all definitions for the Ethernet Data Interface Layer
21  * @file ethdi.c
22  * @ingroup group_lte_source_auxlib
23  * @author Intel Corporation
24  **/
25
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <errno.h>
31 #include <sys/queue.h>
32 #include <err.h>
33 #include <assert.h>
34 #include <linux/limits.h>
35 #include <sys/types.h>
36 #include <stdlib.h>
37 #include <sys/time.h>
38 #include <time.h>
39 #include <unistd.h>
40
41 #include <rte_config.h>
42 #include <rte_common.h>
43 #include <rte_log.h>
44 #include <rte_memory.h>
45 #include <rte_memcpy.h>
46 #include <rte_memzone.h>
47 #include <rte_eal.h>
48 #include <rte_per_lcore.h>
49 #include <rte_launch.h>
50 #include <rte_atomic.h>
51 #include <rte_cycles.h>
52 #include <rte_prefetch.h>
53 #include <rte_lcore.h>
54 #include <rte_per_lcore.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_interrupts.h>
57 #include <rte_pci.h>
58 #include <rte_debug.h>
59 #include <rte_ethdev.h>
60 #include <rte_ring.h>
61 #include <rte_mbuf.h>
62 #include <rte_timer.h>
63
64 #include "ethernet.h"
65 #include "ethdi.h"
66 #include "xran_fh_o_du.h"
67 #include "xran_mlog_lnx.h"
68 #include "xran_printf.h"
69
70 #include "../src/xran_lib_mlog_tasks_id.h"
71
72 #define BURST_RX_IO_SIZE 48
73
74 struct xran_ethdi_ctx g_ethdi_ctx = { 0 };
75 enum xran_if_state xran_if_current_state = XRAN_STOPPED;
76
77 struct rte_mbuf *xran_ethdi_mbuf_alloc(void)
78 {
79     return rte_pktmbuf_alloc(_eth_mbuf_pool);
80 }
81
82 int xran_ethdi_mbuf_send(struct rte_mbuf *mb, uint16_t ethertype)
83 {
84     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
85     int res = 0;
86
87     mb->port = ctx->io_cfg.port[ETHDI_UP_VF];
88     xran_add_eth_hdr_vlan(&ctx->entities[ID_RU], ethertype, mb, ctx->up_vtag);
89
90     res = xran_enqueue_mbuf(mb, ctx->tx_ring[ETHDI_UP_VF]);
91     return res;
92 }
93
94 int xran_ethdi_mbuf_send_cp(struct rte_mbuf *mb, uint16_t ethertype)
95 {
96     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
97     int res = 0;
98
99     mb->port = ctx->io_cfg.port[ETHDI_CP_VF];
100     xran_add_eth_hdr_vlan(&ctx->entities[ID_RU], ethertype, mb, ctx->cp_vtag);
101
102     res = xran_enqueue_mbuf(mb, ctx->tx_ring[ETHDI_CP_VF]);
103     return res;
104 }
105 #if 0
106 void xran_ethdi_stop_tx()
107 {
108     struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
109     rte_timer_stop_sync(&ctx->timer_tx);
110 }
111 #endif
112
113 struct {
114     uint16_t ethertype;
115     ethertype_handler fn;
116 } xran_ethertype_handlers[] = {
117     { ETHER_TYPE_ETHDI, NULL },
118     { ETHER_TYPE_ECPRI, NULL },
119     { ETHER_TYPE_START_TX, NULL }
120 };
121
122
123
124 int xran_register_ethertype_handler(uint16_t ethertype, ethertype_handler callback)
125 {
126     int i;
127
128     for (i = 0; i < RTE_DIM(xran_ethertype_handlers); ++i)
129         if (xran_ethertype_handlers[i].ethertype == ethertype) {
130             xran_ethertype_handlers[i].fn = callback;
131
132             return 1;
133         }
134
135     elog("support for ethertype %u not found", ethertype);
136
137     return 0;
138 }
139
140 int xran_handle_ether(uint16_t ethertype, struct rte_mbuf *pkt, uint64_t rx_time)
141 {
142     int i;
143
144     for (i = 0; i < RTE_DIM(xran_ethertype_handlers); ++i)
145         if (xran_ethertype_handlers[i].ethertype == ethertype)
146             if (xran_ethertype_handlers[i].fn)
147                 return xran_ethertype_handlers[i].fn(pkt, rx_time);
148
149     wlog("Packet with unrecognized ethertype '%.4X' dropped", ethertype);
150
151     return 0;
152 };
153
154
155 /* Process vlan tag. Cut the ethernet header. Call the etherype handlers. */
156 int xran_ethdi_filter_packet(struct rte_mbuf *pkt, uint64_t rx_time)
157 {
158     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
159
160 #ifdef VLAN_SUPPORT
161     if (rte_vlan_strip(pkt) == 0) {
162         if (pkt->vlan_tci == ctx->cp_vtag) {
163             dlog("VLAN tci matches %d", pkt->vlan_tci);
164         } else {
165             wlog("packet with wrong VLAN tag %d, dropping",
166                     pkt->vlan_tci);
167             return 0;
168         }
169     } else
170         dlog("Packet not vlan tagged");
171 #endif
172
173     const struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(pkt, void *);
174
175 #if defined(DPDKIO_DEBUG) && DPDKIO_DEBUG > 1
176     nlog("*** processing RX'ed packet of size %d ***",
177             rte_pktmbuf_data_len(pkt));
178     /* TODO: just dump ethernet header in readable format? */
179 #endif
180
181 #if defined(DPDKIO_DEBUG) && DPDKIO_DEBUG > 1
182     {
183         char dst[ETHER_ADDR_FMT_SIZE] = "(empty)";
184         char src[ETHER_ADDR_FMT_SIZE] = "(empty)";
185
186         ether_format_addr(dst, sizeof(dst), &eth_hdr->d_addr);
187         ether_format_addr(src, sizeof(src), &eth_hdr->s_addr);
188         nlog("src: %s dst: %s ethertype: %.4X", dst, src,
189                 rte_be_to_cpu_16(eth_hdr->ether_type));
190     }
191 #endif
192
193     /* Cut out the ethernet header. It's not needed anymore. */
194     if (rte_pktmbuf_adj(pkt, sizeof(*eth_hdr)) == NULL) {
195         wlog("Packet too short, dropping");
196         return 0;
197     }
198
199
200     return xran_handle_ether(rte_be_to_cpu_16(eth_hdr->ether_type), pkt, rx_time);
201 }
202
203 #if 0
204 //-------------------------------------------------------------------------------------------
205 /** @ingroup xran
206  *
207  *  @param[in]  port - DPDK ETH port id
208  *
209  *  @return  void
210  *
211  *  @description
212  *  Prints statistics of usage of DPDK port
213  *
214 **/
215 //-------------------------------------------------------------------------------------------
216 void xran_ethdi_ports_stats(void)
217 {
218     struct rte_eth_stats stats;
219     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
220     int32_t i = 0;
221
222     for(i = 0; i < ETHDI_VF_MAX; i++){
223         /* Get stats (extended stats includes common stats) */
224         rte_eth_stats_get(ctx->io_cfg.port[i], &stats);
225         printf("DPDK stats:\n");
226         printf("** Port %hhu **\n", ctx->io_cfg.port[i]);
227         printf("ierrors:\t%lu\n",   stats.ierrors);
228         printf("oerrors:\t%lu\n",   stats.oerrors);
229         printf("ipackets:\t%lu\n",  stats.ipackets);
230         printf("opackets:\t%lu\n",  stats.opackets);
231         printf("imissed:\t%lu\n",   stats.imissed);
232         printf("rx_nombuf:\t%lu\n", stats.rx_nombuf);
233     }
234     return ;
235 }
236 #endif
237 /* Check the link status of all ports in up to 9s, and print them finally */
238 static void check_port_link_status(uint8_t portid)
239 {
240 #define CHECK_INTERVAL 100 /* 100ms */
241 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
242     uint8_t count, all_ports_up, print_flag = 0;
243     struct rte_eth_link link;
244
245     printf("\nChecking link status");
246     fflush(stdout);
247     for (count = 0; count <= MAX_CHECK_TIME; count++) {
248         all_ports_up = 1;
249         memset(&link, 0, sizeof(link));
250         rte_eth_link_get_nowait(portid, &link);
251
252         /* print link status if flag set */
253         if (print_flag == 1) {
254             if (link.link_status)
255                 printf("Port %d Link Up - speed %u "
256                         "Mbps - %s\n", (uint8_t)portid,
257                         (unsigned)link.link_speed,
258                         (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
259                         ("full-duplex") : ("half-duplex\n"));
260             else
261                 printf("Port %d Link Down\n",
262                         (uint8_t)portid);
263         }
264         /* clear all_ports_up flag if any link down */
265         if (link.link_status == ETH_LINK_DOWN) {
266             all_ports_up = 0;
267             break;
268         }
269         /* after finally printing all link status, get out */
270         if (print_flag == 1)
271             break;
272
273         if (all_ports_up == 0) {
274             printf(".");
275             fflush(stdout);
276             rte_delay_ms(CHECK_INTERVAL);
277         }
278
279         /* set the print_flag if all ports up or timeout */
280         if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
281             print_flag = 1;
282             printf(" ... done\n");
283         }
284     }
285 }
286
287
288 int xran_ethdi_init_dpdk_io(char *name, const struct xran_io_loop_cfg *io_cfg,
289     int *lcore_id, struct ether_addr *p_lls_cu_addr, struct ether_addr *p_ru_addr,
290     uint16_t cp_vlan, uint16_t up_vlan)
291 {
292     uint16_t port[2] = {0xffff, 0xffff};
293     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
294     int i;
295     char core_mask[64];
296     long c_mask=0;
297     char bbdev_wdev[32] = "";
298     char bbdev_vdev[32] = "";
299
300     char *argv[] = { name, /*"-c 0xFFFFF00000FFFFF"*/core_mask, "-n2", "--socket-mem=8192", "--proc-type=auto",
301         "--file-prefix", name, "-w", "0000:00:00.0", bbdev_wdev, bbdev_vdev};
302
303     if (io_cfg == NULL)
304         return 0;
305     if(io_cfg->bbdev_mode != XRAN_BBDEV_NOT_USED){
306         printf("BBDEV_FEC_ACCL_NR5G\n");
307         if (io_cfg->bbdev_mode == XRAN_BBDEV_MODE_HW_ON){
308             // hw-accelerated bbdev
309             printf("hw-accelerated bbdev %s\n", io_cfg->bbdev_dev[0]);
310             snprintf(bbdev_wdev, sizeof(bbdev_wdev), "-w %s", io_cfg->bbdev_dev[0]);
311         } else if (io_cfg->bbdev_mode == XRAN_BBDEV_MODE_HW_OFF){
312             // hw-accelerated bbdev disable
313             if(io_cfg->bbdev_dev[0]){
314                 printf("hw-accelerated bbdev disable %s\n", io_cfg->bbdev_dev[0]);
315                 snprintf(bbdev_wdev, sizeof(bbdev_wdev), "-b %s", io_cfg->bbdev_dev[0]);
316             }
317             snprintf(bbdev_wdev, sizeof(bbdev_wdev), "%s", "--vdev=baseband_turbo_sw");
318         } else {
319             rte_panic("Cannot init DPDK incorrect [bbdev_mode %d]\n", io_cfg->bbdev_mode);
320         }
321     }
322
323     c_mask = (long)(1L << io_cfg->core) |
324             (long)(1L << io_cfg->system_core) |
325             (long)(1L << io_cfg->pkt_proc_core) |
326             (long)(1L << io_cfg->pkt_aux_core) |
327             (long)(1L << io_cfg->timing_core);
328
329     printf("c_mask 0x%lx core %d system_core %d pkt_proc_core %d pkt_aux_core %d timing_core %d\n",
330         c_mask, io_cfg->core, io_cfg->system_core, io_cfg->pkt_proc_core, io_cfg->pkt_aux_core, io_cfg->timing_core);
331
332     snprintf(core_mask, sizeof(core_mask), "-c 0x%lx", c_mask);
333
334     ctx->io_cfg = *io_cfg;
335     ctx->ping_state           = PING_IDLE;
336     ctx->known_peers          = 1;
337     ctx->busy_poll_till = rte_rdtsc();
338     ctx->cp_vtag = cp_vlan;
339     ctx->up_vtag = up_vlan;
340
341     for (i = 0; i <= ID_BROADCAST; i++)     /* Initialize all as broadcast */
342         memset(&ctx->entities[i], 0xFF, sizeof(ctx->entities[0]));
343
344     printf("%s: Calling rte_eal_init:", __FUNCTION__);
345     for (i = 0; i < RTE_DIM(argv); i++)
346     {
347         printf("%s ", argv[i]);
348     }
349     printf("\n");
350
351
352     /* This will return on system_core, which is not necessarily the
353      * one we're on right now. */
354     if (rte_eal_init(RTE_DIM(argv), argv) < 0)
355         rte_panic("Cannot init EAL: %s\n", rte_strerror(rte_errno));
356
357     xran_init_mbuf_pool();
358
359 #ifdef RTE_LIBRTE_PDUMP
360     /* initialize packet capture framework */
361     rte_pdump_init(NULL);
362 #endif
363
364     /* Timers. */
365     rte_timer_subsystem_init();
366     rte_timer_init(&ctx->timer_ping);
367     rte_timer_init(&ctx->timer_sync);
368     rte_timer_init(&ctx->timer_tx);
369
370     *lcore_id = rte_get_next_lcore(rte_lcore_id(), 0, 0);
371
372     PANIC_ON(*lcore_id == RTE_MAX_LCORE, "out of lcores for io_loop()");
373
374     if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
375         for (i = 0; i < ETHDI_VF_MAX; i ++){
376             if(io_cfg->dpdk_dev[i]){
377                 if (rte_eth_dev_attach(io_cfg->dpdk_dev[i], &port[i]) != 0 ||
378                     rte_eth_dev_count_avail() == 0)
379                     errx(1, "Network port doesn't exist.");
380                 xran_init_port(port[i], p_lls_cu_addr);
381             } else {
382                 printf("no DPDK port provided\n");
383             }
384             if(i==0){
385                 ctx->tx_ring[i] = rte_ring_create("tx_ring_up", NUM_MBUFS,
386                     rte_lcore_to_socket_id(*lcore_id), RING_F_SC_DEQ);
387                 ctx->rx_ring[i] = rte_ring_create("rx_ring_up", NUM_MBUFS,
388                     rte_lcore_to_socket_id(*lcore_id), RING_F_SC_DEQ);
389                 ctx->pkt_dump_ring[i] = rte_ring_create("pkt_dump_ring_up", NUM_MBUFS,
390                     rte_lcore_to_socket_id(*lcore_id), RING_F_SC_DEQ);
391             }else {
392                 ctx->tx_ring[i] = rte_ring_create("tx_ring_cp", NUM_MBUFS,
393                     rte_lcore_to_socket_id(*lcore_id), RING_F_SC_DEQ);
394                 ctx->rx_ring[i] = rte_ring_create("rx_ring_cp", NUM_MBUFS,
395                     rte_lcore_to_socket_id(*lcore_id), RING_F_SC_DEQ);
396                 ctx->pkt_dump_ring[i] = rte_ring_create("pkt_dump_ring_cp", NUM_MBUFS,
397                     rte_lcore_to_socket_id(*lcore_id), RING_F_SC_DEQ);
398             }
399             if(io_cfg->dpdk_dev[i])
400                 check_port_link_status(port[i]);
401         }
402     } else {
403         rte_panic("ethdi_dpdk_io_loop() failed to start  with RTE_PROC_SECONDARY\n");
404     }
405     PANIC_ON(ctx->tx_ring == NULL, "failed to allocate tx ring");
406     PANIC_ON(ctx->rx_ring == NULL, "failed to allocate rx ring");
407     PANIC_ON(ctx->pkt_dump_ring == NULL, "failed to allocate pkt dumping ring");
408     for (i = 0; i < ETHDI_VF_MAX; i++){
409         ctx->io_cfg.port[i] = port[i];
410         print_dbg("port_id 0x%04x\n", ctx->io_cfg.port[i]);
411     }
412
413     if(io_cfg->dpdk_dev[ETHDI_UP_VF]){
414         rte_eth_macaddr_get(port[ETHDI_UP_VF], &ctx->entities[io_cfg->id]);
415         ether_addr_copy(p_ru_addr,  &ctx->entities[ID_RU]);
416     }
417
418     return 1;
419 }
420
421 static inline uint16_t xran_tx_from_ring(int port, struct rte_ring *r)
422 {
423     struct rte_mbuf *mbufs[BURST_SIZE];
424     uint16_t dequeued, sent = 0;
425     uint32_t remaining;
426     int i;
427     long t1 = MLogTick();
428
429     dequeued = rte_ring_dequeue_burst(r, (void **)mbufs, BURST_SIZE,
430             &remaining);
431     if (!dequeued)
432         return 0;   /* Nothing to send. */
433
434     while (1) {     /* When tx queue is full it is trying again till succeed */
435         t1 = MLogTick();
436         sent += rte_eth_tx_burst(port, 0, &mbufs[sent], dequeued - sent);
437
438         MLogTask(PID_RADIO_ETH_TX_BURST, t1, MLogTick());
439
440         if (sent == dequeued)
441             return remaining;
442     }
443 }
444
445 int32_t process_dpdk_io(void)
446 {
447     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
448     const struct xran_io_loop_cfg *const cfg = &(xran_ethdi_get_ctx()->io_cfg);
449     const int port[ETHDI_VF_MAX] = {cfg->port[ETHDI_UP_VF], cfg->port[ETHDI_CP_VF]};
450     int port_id = 0;
451
452     for (port_id = 0; port_id < ETHDI_VF_MAX; port_id++){
453         struct rte_mbuf *mbufs[BURST_RX_IO_SIZE];
454         if(port[port_id] == 0xFF)
455             return 0;
456         /* RX */
457         const uint16_t rxed = rte_eth_rx_burst(port[port_id], 0, mbufs, BURST_RX_IO_SIZE);
458         if (rxed != 0){
459             unsigned enq_n = 0;
460             long t1 = MLogTick();
461             enq_n =  rte_ring_enqueue_burst(ctx->rx_ring[port_id], (void*)mbufs, rxed, NULL);
462             if(rxed - enq_n)
463                 rte_panic("error enq\n");
464             MLogTask(PID_RADIO_RX_VALIDATE, t1, MLogTick());
465         }
466
467         /* TX */
468         const uint16_t sent = xran_tx_from_ring(port[port_id], ctx->tx_ring[port_id]);
469
470         if (XRAN_STOPPED == xran_if_current_state)
471             return -1;
472     }
473
474     if (XRAN_STOPPED == xran_if_current_state)
475             return -1;
476
477     return 0;
478 }
479
480 #if 0
481 static inline void xran_process_rx_burst(struct rte_mbuf *mbufs[], uint16_t n_mbufs,
482         uint64_t rx_time)
483 {
484         int i;
485
486         if (!n_mbufs)
487             return;
488
489         for (i = 0; i < n_mbufs; ++i)
490         {
491             if (xran_ethdi_filter_packet(mbufs[i], rx_time) == MBUF_FREE)
492                 rte_pktmbuf_free(mbufs[i]);
493         }
494
495 #ifdef DPDKIO_LATENCY_DEBUG
496        struct timeval tv_now, tv_diff;
497
498        gettimeofday(&tv_now, NULL);
499        if (n_mbufs > 1)
500            nlog("Warning - received %d mbufs in a row", n_mbufs);
501
502        timersub(&tv_now, &rx_time, &tv_diff);
503        nlog("rx processing took %d usec", tv_diff.tv_usec);
504 #endif
505 }
506
507 /*
508  * This is the main DPDK-IO loop.
509  * This will sleep if there's no packets incoming and there's
510  * no work enqueued, sleep lenth is defined in IDLE_SLEEP_MICROSECS
511  */
512 int xran_ethdi_dpdk_io_loop(void *io_loop_cfg)
513 {
514     struct sched_param sched_param;
515     int res = 0;
516     struct xran_ethdi_ctx *ctx = xran_ethdi_get_ctx();
517     const struct xran_io_loop_cfg *const cfg = &(xran_ethdi_get_ctx()->io_cfg);
518     const int port[ETHDI_VF_MAX] = {cfg->port[ETHDI_UP_VF], cfg->port[ETHDI_CP_VF]};
519
520     printf("%s [PORT: %d %d] [CPU %2d] [PID: %6d]\n", __FUNCTION__, port[ETHDI_UP_VF], port[ETHDI_CP_VF] , rte_lcore_id(), getpid());
521
522     printf("%s [CPU %2d] [PID: %6d]\n", __FUNCTION__,  rte_lcore_id(), getpid());
523     sched_param.sched_priority = XRAN_THREAD_DEFAULT_PRIO;
524     if ((res = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched_param))) {
525         printf("priority is not changed: coreId = %d, result1 = %d\n",rte_lcore_id(), res);
526     }
527
528     for (;;){
529         if(process_dpdk_io()!=0)
530             break;
531     }
532
533     fflush(stderr);
534     fflush(stdout);
535     puts("IO loop finished");
536
537     return 0;
538 }
539 #endif
540