MoonGen
 All Files Functions Variables Pages
dpdkc.lua
Go to the documentation of this file.
1 ---------------------------------
2 --- @file dpdkc.lua
3 --- @brief DPDKc ...
4 --- @todo TODO docu
5 ---------------------------------
6 
7 --- low-level dpdk wrapper
8 local ffi = require "ffi"
9 
10 -- structs
11 ffi.cdef[[
12  // core management
13  enum rte_lcore_state_t {
14  WAIT, RUNNING, FINISHED
15  };
16 
17  // packets/mbufs
18  struct rte_pktmbuf {
19  struct rte_mbuf* next;
20  void* data;
21  uint16_t data_len;
22  uint8_t nb_segs;
23  uint8_t in_port;
24  uint32_t pkt_len;
25  //union {
26  uint16_t header_lengths;
27  uint16_t vlan_tci;
28  //uint32_t value;
29  //} offsets;
30  union {
31  uint32_t rss;
32  struct {
33  uint16_t hash;
34  uint16_t id;
35  } fdir;
36  uint32_t sched;
37  } hash;
38  };
39 
40  union rte_ipsec {
41  uint32_t data;
42  //struct {
43  // uint16_t sa_idx:10;
44  // uint16_t esp_len:9;
45  // uint8_t type:1;
46  // uint8_t mode:1;
47  // uint16_t unused:11; /**< These 11 bits are unused. */
48  //} sec;
49  };
50 
51  struct rte_mbuf {
52  void* pool;
53  void* data;
54  uint64_t phy_addr;
55  uint16_t len;
56  uint16_t refcnt;
57  uint8_t type;
58  uint8_t reserved;
59  uint16_t ol_flags;
60  struct rte_pktmbuf pkt;
61  union rte_ipsec ol_ipsec;
62  };
63 
64  struct mempool {
65  }; // dummy struct, only needed to associate it with a metatable
66 
67  // device status/info
68  struct rte_eth_link {
69  uint16_t link_speed;
70  uint16_t link_duplex;
71  uint8_t link_status: 1;
72  } __attribute__((aligned(8)));
73 
74  struct rte_fdir_filter {
75  uint16_t flex_bytes;
76  uint16_t vlan_id;
77  uint16_t port_src;
78  uint16_t port_dst;
79  union {
80  uint32_t ipv4_addr;
81  uint32_t ipv6_addr[4];
82  } ip_src;
83  union {
84  uint32_t ipv4_addr;
85  uint32_t ipv6_addr[4];
86  } ip_dst;
87  int l4type;
88  int iptype;
89  };
90  enum rte_l4type {
91  RTE_FDIR_L4TYPE_NONE = 0, /**< None. */
92  RTE_FDIR_L4TYPE_UDP, /**< UDP. */
93  RTE_FDIR_L4TYPE_TCP, /**< TCP. */
94  RTE_FDIR_L4TYPE_SCTP, /**< SCTP. */
95  };
96 
97 
98  struct rte_fdir_masks {
99  uint8_t only_ip_flow;
100  uint8_t vlan_id;
101  uint8_t vlan_prio;
102  uint8_t flexbytes;
103  uint8_t set_ipv6_mask;
104  uint8_t comp_ipv6_dst;
105  uint32_t dst_ipv4_mask;
106  uint32_t src_ipv4_mask;
107  uint16_t dst_ipv6_mask;
108  uint16_t src_ipv6_mask;
109  uint16_t src_port_mask;
110  uint16_t dst_port_mask;
111  };
112 
113 
114  // statistics
115  struct rte_eth_stats {
116  uint64_t ipackets; /**< Total number of successfully received packets. */
117  uint64_t opackets; /**< Total number of successfully transmitted packets.*/
118  uint64_t ibytes; /**< Total number of successfully received bytes. */
119  uint64_t obytes; /**< Total number of successfully transmitted bytes. */
120  uint64_t imissed; /**< Total of RX missed packets (e.g full FIFO). */
121  uint64_t ibadcrc; /**< Total of RX packets with CRC error. */
122  uint64_t ibadlen; /**< Total of RX packets with bad length. */
123  uint64_t ierrors; /**< Total number of erroneous received packets. */
124  uint64_t oerrors; /**< Total number of failed transmitted packets. */
125  uint64_t imcasts; /**< Total number of multicast received packets. */
126  uint64_t rx_nombuf; /**< Total number of RX mbuf allocation failures. */
127  uint64_t fdirmatch; /**< Total number of RX packets matching a filter. */
128  uint64_t fdirmiss; /**< Total number of RX packets not matching any filter. */
129  uint64_t tx_pause_xon; /**< Total nb. of XON pause frame sent. */
130  uint64_t rx_pause_xon; /**< Total nb. of XON pause frame received. */
131  uint64_t tx_pause_xoff; /**< Total nb. of XOFF pause frame sent. */
132  uint64_t rx_pause_xoff; /**< Total nb. of XOFF pause frame received. */
133  // TODO: 16 is a dpdk compile-time constant which kind of sucks. probably needs a rewrite or something.
134  uint64_t q_ipackets[16];
135  /**< Total number of queue RX packets. */
136  uint64_t q_opackets[16];
137  /**< Total number of queue TX packets. */
138  uint64_t q_ibytes[16];
139  /**< Total number of successfully received queue bytes. */
140  uint64_t q_obytes[16];
141  /**< Total number of successfully transmitted queue bytes. */
142  uint64_t q_errors[16];
143  /**< Total number of queue packets received that are dropped. */
144  uint64_t ilbpackets;
145  /**< Total number of good packets received from loopback,VF Only */
146  uint64_t olbpackets;
147  /**< Total number of good packets transmitted to loopback,VF Only */
148  uint64_t ilbbytes;
149  /**< Total number of good bytes received from loopback,VF Only */
150  uint64_t olbbytes;
151  /**< Total number of good bytes transmitted to loopback,VF Only */
152  };
153 
154  struct mg_rss_hash_mask{
155  uint8_t ipv4 :1;
156  uint8_t tcp_ipv4 :1;
157  uint8_t udp_ipv4 :1;
158  uint8_t ipv6 :1;
159  uint8_t tcp_ipv6 :1;
160  uint8_t udp_ipv6 :1;
161  };
162 ]]
163 
164 -- dpdk functions and wrappers
165 ffi.cdef[[
166  // eal init
167  int rte_eal_init(int argc, const char* argv[]);
168 
169  // cpu core management
170  int rte_eal_get_lcore_state(int core);
171  enum rte_lcore_state_t rte_eal_get_lcore_state(unsigned int slave_id);
172  int rte_eal_wait_lcore(int core);
173  uint32_t get_current_core();
174  uint32_t get_current_socket();
175 
176  // memory
177  struct mempool* init_mem(uint32_t nb_mbuf, uint32_t sock, uint32_t mbuf_size);
178  struct rte_mbuf* alloc_mbuf(struct mempool* mp);
179  void alloc_mbufs(struct mempool* mp, struct rte_mbuf* bufs[], uint32_t len, uint16_t pkt_len);
180  void rte_pktmbuf_free_export(struct rte_mbuf* m);
181  uint16_t rte_mbuf_refcnt_read_export(struct rte_mbuf* m);
182  uint16_t rte_mbuf_refcnt_update_export(struct rte_mbuf* m, int16_t value);
183 
184  // devices
185  void register_pmd_drivers();
186  int rte_eal_pci_probe();
187  int rte_eth_dev_count();
188  uint64_t get_mac_addr(int port, char* buf);
189  void rte_eth_link_get(uint8_t port, struct rte_eth_link* link);
190  void rte_eth_link_get_nowait(uint8_t port, struct rte_eth_link* link);
191  //int configure_device(int port, int rx_queues, int tx_queues, int rx_descs, int tx_descs, uint16_t link_speed, struct mempool* mempool, bool drop_en);
192  int configure_device(int port, int rx_queues, int tx_queues, int rx_descs, int tx_descs, uint16_t link_speed, struct mempool* mempool, bool drop_en, uint8_t rss_enable, struct mg_rss_hash_mask * hash_functions);
193  void get_mac_addr(int port, char* buf);
194  uint32_t get_pci_id(uint8_t port);
195  uint32_t read_reg32(uint8_t port, uint32_t reg);
196  uint64_t read_reg64(uint8_t port, uint32_t reg);
197  void write_reg32(uint8_t port, uint32_t reg, uint32_t val);
198  void write_reg64(uint8_t port, uint32_t reg, uint64_t val);
199  void sync_clocks(uint8_t port1, uint8_t port2, uint32_t timl, uint32_t timh, uint32_t adjl, uint32_t adjh);
200  int32_t get_clock_difference(uint8_t port1, uint8_t port2);
201  uint8_t get_socket(uint8_t port);
202  void rte_eth_promiscuous_enable(uint8_t port);
203  void rte_eth_promiscuous_disable(uint8_t port);
204  void* get_eth_dev(int port);
205  void* get_i40e_dev(int port);
206  int get_i40e_vsi_seid(int port);
207 
208  // rx & tx
209  uint16_t rte_eth_rx_burst_export(uint8_t port_id, uint16_t queue_id, struct rte_mbuf** rx_pkts, uint16_t nb_pkts);
210  uint16_t rte_eth_tx_burst_export(uint8_t port_id, uint16_t queue_id, struct rte_mbuf** tx_pkts, uint16_t nb_pkts);
211  int rte_eth_dev_tx_queue_start(uint8_t port_id, uint16_t rx_queue_id);
212  int rte_eth_dev_tx_queue_stop(uint8_t port_id, uint16_t rx_queue_id);
213  void send_all_packets(uint8_t port_id, uint16_t queue_id, struct rte_mbuf** pkts, uint16_t num_pkts);
214  void send_all_packets_with_delay_invalid_size(uint8_t port_id, uint16_t queue_id, struct rte_mbuf** load_pkts, uint16_t num_pkts, struct mempool* pool);
215  void send_all_packets_with_delay_bad_crc(uint8_t port_id, uint16_t queue_id, struct rte_mbuf** load_pkts, uint16_t num_pkts, struct mempool* pool);
216  uint64_t get_bad_pkts_sent(uint8_t port_id);
217  uint64_t get_bad_bytes_sent(uint8_t port_id);
218 
219  // fdir filter
220  int rte_eth_dev_fdir_add_perfect_filter(uint8_t port_id, struct rte_fdir_filter* fdir_filter, uint16_t soft_id, uint8_t rx_queue, uint8_t drop);
221  int rte_eth_dev_fdir_set_masks(uint8_t port_id, struct rte_fdir_masks* fdir_mask);
222 
223 
224  // checksum offloading
225  void calc_ipv4_pseudo_header_checksum(void* data, int offset);
226  void calc_ipv4_pseudo_header_checksums(struct rte_mbuf** pkts, uint16_t num_pkts, int offset);
227  void calc_ipv6_pseudo_header_checksum(void* data, int offset);
228  void calc_ipv6_pseudo_header_checksums(struct rte_mbuf** pkts, uint16_t num_pkts, int offset);
229 
230  // timers
231  void rte_delay_ms_export(uint32_t ms);
232  void rte_delay_us_export(uint32_t us);
233  uint64_t rte_rdtsc();
234  uint64_t rte_get_tsc_hz();
235 
236  // lifecycle
237  uint8_t is_running(uint32_t extra_time);
238  void set_runtime(uint32_t ms);
239 
240  // timestamping
241  void read_timestamps_software(uint8_t port_id, uint16_t queue_id, uint32_t* data, uint64_t size);
242 
243  // statistics
244  void rte_eth_stats_get(uint8_t port, struct rte_eth_stats* stats);
245 ]]
246 
247 return ffi.C
248 
local ffi
low-level dpdk wrapper
Definition: dpdkc.lua:6
RTE_FDIR_L4TYPE_NONE
None.
Definition: dpdkc.lua:14
local pkt
Module for packets (rte_mbuf)
Definition: packet.lua:20