net: add rte prefix to ether structures
[dpdk.git] / examples / tep_termination / vxlan_setup.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <getopt.h>
6 #include <linux/if_ether.h>
7 #include <linux/if_vlan.h>
8 #include <linux/virtio_net.h>
9 #include <linux/virtio_ring.h>
10 #include <sys/param.h>
11 #include <unistd.h>
12
13 #include <rte_ethdev.h>
14 #include <rte_log.h>
15 #include <rte_string_fns.h>
16 #include <rte_mbuf.h>
17 #include <rte_malloc.h>
18 #include <rte_ip.h>
19 #include <rte_udp.h>
20 #include <rte_tcp.h>
21
22 #include "main.h"
23 #include "rte_vhost.h"
24 #include "vxlan.h"
25 #include "vxlan_setup.h"
26
27 #define IPV4_HEADER_LEN 20
28 #define UDP_HEADER_LEN  8
29 #define VXLAN_HEADER_LEN 8
30
31 #define IP_VERSION 0x40
32 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
33 #define IP_DEFTTL  64   /* from RFC 1340. */
34 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
35
36 #define IP_DN_FRAGMENT_FLAG 0x0040
37
38 /* Used to compare MAC addresses. */
39 #define MAC_ADDR_CMP 0xFFFFFFFFFFFFULL
40
41 /* Configurable number of RX/TX ring descriptors */
42 #define RTE_TEST_RX_DESC_DEFAULT 1024
43 #define RTE_TEST_TX_DESC_DEFAULT 512
44
45 /* Default inner VLAN ID */
46 #define INNER_VLAN_ID 100
47
48 /* VXLAN device */
49 struct vxlan_conf vxdev;
50
51 struct ipv4_hdr app_ip_hdr[VXLAN_N_PORTS];
52 struct rte_ether_hdr app_l2_hdr[VXLAN_N_PORTS];
53
54 /* local VTEP IP address */
55 uint8_t vxlan_multicast_ips[2][4] = { {239, 1, 1, 1 }, {239, 1, 2, 1 } };
56
57 /* Remote VTEP IP address */
58 uint8_t vxlan_overlay_ips[2][4] = { {192, 168, 10, 1}, {192, 168, 30, 1} };
59
60 /* Remote VTEP MAC address */
61 uint8_t peer_mac[6] = {0x00, 0x11, 0x01, 0x00, 0x00, 0x01};
62
63 /* VXLAN RX filter type */
64 uint8_t tep_filter_type[] = {RTE_TUNNEL_FILTER_IMAC_TENID,
65                         RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID,
66                         RTE_TUNNEL_FILTER_OMAC_TENID_IMAC,};
67
68 /* Options for configuring ethernet port */
69 static struct rte_eth_conf port_conf = {
70         .rxmode = {
71                 .split_hdr_size = 0,
72         },
73         .txmode = {
74                 .mq_mode = ETH_MQ_TX_NONE,
75                 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
76                              DEV_TX_OFFLOAD_UDP_CKSUM |
77                              DEV_TX_OFFLOAD_TCP_CKSUM |
78                              DEV_TX_OFFLOAD_SCTP_CKSUM |
79                              DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
80                              DEV_TX_OFFLOAD_TCP_TSO |
81                              DEV_TX_OFFLOAD_MULTI_SEGS |
82                              DEV_TX_OFFLOAD_VXLAN_TNL_TSO),
83         },
84 };
85
86 /**
87  * The one or two device(s) that belongs to the same tenant ID can
88  * be assigned in a VM.
89  */
90 const uint16_t tenant_id_conf[] = {
91         1000, 1000, 1001, 1001, 1002, 1002, 1003, 1003,
92         1004, 1004, 1005, 1005, 1006, 1006, 1007, 1007,
93         1008, 1008, 1009, 1009, 1010, 1010, 1011, 1011,
94         1012, 1012, 1013, 1013, 1014, 1014, 1015, 1015,
95         1016, 1016, 1017, 1017, 1018, 1018, 1019, 1019,
96         1020, 1020, 1021, 1021, 1022, 1022, 1023, 1023,
97         1024, 1024, 1025, 1025, 1026, 1026, 1027, 1027,
98         1028, 1028, 1029, 1029, 1030, 1030, 1031, 1031,
99 };
100
101 /**
102  * Initialises a given port using global settings and with the rx buffers
103  * coming from the mbuf_pool passed as parameter
104  */
105 int
106 vxlan_port_init(uint16_t port, struct rte_mempool *mbuf_pool)
107 {
108         int retval;
109         uint16_t q;
110         struct rte_eth_dev_info dev_info;
111         uint16_t rx_rings, tx_rings = (uint16_t)rte_lcore_count();
112         uint16_t rx_ring_size = RTE_TEST_RX_DESC_DEFAULT;
113         uint16_t tx_ring_size = RTE_TEST_TX_DESC_DEFAULT;
114         struct rte_eth_udp_tunnel tunnel_udp;
115         struct rte_eth_rxconf *rxconf;
116         struct rte_eth_txconf *txconf;
117         struct vxlan_conf *pconf = &vxdev;
118         struct rte_eth_conf local_port_conf = port_conf;
119
120         pconf->dst_port = udp_port;
121
122         rte_eth_dev_info_get(port, &dev_info);
123
124         if (dev_info.max_rx_queues > MAX_QUEUES) {
125                 rte_exit(EXIT_FAILURE,
126                         "please define MAX_QUEUES no less than %u in %s\n",
127                         dev_info.max_rx_queues, __FILE__);
128         }
129
130         rxconf = &dev_info.default_rxconf;
131         txconf = &dev_info.default_txconf;
132
133         if (!rte_eth_dev_is_valid_port(port))
134                 return -1;
135
136         rx_rings = nb_devices;
137         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
138                 local_port_conf.txmode.offloads |=
139                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
140         /* Configure ethernet device. */
141         retval = rte_eth_dev_configure(port, rx_rings, tx_rings,
142                                        &local_port_conf);
143         if (retval != 0)
144                 return retval;
145
146         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &rx_ring_size,
147                         &tx_ring_size);
148         if (retval != 0)
149                 return retval;
150
151         /* Setup the queues. */
152         rxconf->offloads = local_port_conf.rxmode.offloads;
153         for (q = 0; q < rx_rings; q++) {
154                 retval = rte_eth_rx_queue_setup(port, q, rx_ring_size,
155                                                 rte_eth_dev_socket_id(port),
156                                                 rxconf,
157                                                 mbuf_pool);
158                 if (retval < 0)
159                         return retval;
160         }
161         txconf->offloads = local_port_conf.txmode.offloads;
162         for (q = 0; q < tx_rings; q++) {
163                 retval = rte_eth_tx_queue_setup(port, q, tx_ring_size,
164                                                 rte_eth_dev_socket_id(port),
165                                                 txconf);
166                 if (retval < 0)
167                         return retval;
168         }
169
170         /* Start the device. */
171         retval  = rte_eth_dev_start(port);
172         if (retval < 0)
173                 return retval;
174
175         /* Configure UDP port for UDP tunneling */
176         tunnel_udp.udp_port = udp_port;
177         tunnel_udp.prot_type = RTE_TUNNEL_TYPE_VXLAN;
178         retval = rte_eth_dev_udp_tunnel_port_add(port, &tunnel_udp);
179         if (retval < 0)
180                 return retval;
181         rte_eth_macaddr_get(port, &ports_eth_addr[port]);
182         RTE_LOG(INFO, PORT, "Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
183                         " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
184                         port,
185                         ports_eth_addr[port].addr_bytes[0],
186                         ports_eth_addr[port].addr_bytes[1],
187                         ports_eth_addr[port].addr_bytes[2],
188                         ports_eth_addr[port].addr_bytes[3],
189                         ports_eth_addr[port].addr_bytes[4],
190                         ports_eth_addr[port].addr_bytes[5]);
191
192         if (tso_segsz != 0) {
193                 struct rte_eth_dev_info dev_info;
194                 rte_eth_dev_info_get(port, &dev_info);
195                 if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) == 0)
196                         RTE_LOG(WARNING, PORT,
197                                 "hardware TSO offload is not supported\n");
198         }
199         return 0;
200 }
201
202 static int
203 vxlan_rx_process(struct rte_mbuf *pkt)
204 {
205         int ret = 0;
206
207         if (rx_decap)
208                 ret = decapsulation(pkt);
209
210         return ret;
211 }
212
213 static void
214 vxlan_tx_process(uint8_t queue_id, struct rte_mbuf *pkt)
215 {
216         if (tx_encap)
217                 encapsulation(pkt, queue_id);
218
219         return;
220 }
221
222 /*
223  * This function learns the MAC address of the device and set init
224  * L2 header and L3 header info.
225  */
226 int
227 vxlan_link(struct vhost_dev *vdev, struct rte_mbuf *m)
228 {
229         int i, ret;
230         struct rte_ether_hdr *pkt_hdr;
231         uint64_t portid = vdev->vid;
232         struct ipv4_hdr *ip;
233
234         struct rte_eth_tunnel_filter_conf tunnel_filter_conf;
235
236         if (unlikely(portid >= VXLAN_N_PORTS)) {
237                 RTE_LOG(INFO, VHOST_DATA,
238                         "(%d) WARNING: Not configuring device,"
239                         "as already have %d ports for VXLAN.",
240                         vdev->vid, VXLAN_N_PORTS);
241                 return -1;
242         }
243
244         /* Learn MAC address of guest device from packet */
245         pkt_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
246         if (is_same_ether_addr(&(pkt_hdr->s_addr), &vdev->mac_address)) {
247                 RTE_LOG(INFO, VHOST_DATA,
248                         "(%d) WARNING: This device is using an existing"
249                         " MAC address and has not been registered.\n",
250                         vdev->vid);
251                 return -1;
252         }
253
254         for (i = 0; i < ETHER_ADDR_LEN; i++) {
255                 vdev->mac_address.addr_bytes[i] =
256                         vxdev.port[portid].vport_mac.addr_bytes[i] =
257                         pkt_hdr->s_addr.addr_bytes[i];
258                 vxdev.port[portid].peer_mac.addr_bytes[i] = peer_mac[i];
259         }
260
261         memset(&tunnel_filter_conf, 0,
262                 sizeof(struct rte_eth_tunnel_filter_conf));
263
264         ether_addr_copy(&ports_eth_addr[0], &tunnel_filter_conf.outer_mac);
265         tunnel_filter_conf.filter_type = tep_filter_type[filter_idx];
266
267         /* inner MAC */
268         ether_addr_copy(&vdev->mac_address, &tunnel_filter_conf.inner_mac);
269
270         tunnel_filter_conf.queue_id = vdev->rx_q;
271         tunnel_filter_conf.tenant_id = tenant_id_conf[vdev->rx_q];
272
273         if (tep_filter_type[filter_idx] == RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID)
274                 tunnel_filter_conf.inner_vlan = INNER_VLAN_ID;
275
276         tunnel_filter_conf.tunnel_type = RTE_TUNNEL_TYPE_VXLAN;
277
278         ret = rte_eth_dev_filter_ctrl(ports[0],
279                 RTE_ETH_FILTER_TUNNEL,
280                 RTE_ETH_FILTER_ADD,
281                 &tunnel_filter_conf);
282         if (ret) {
283                 RTE_LOG(ERR, VHOST_DATA,
284                         "%d Failed to add device MAC address to cloud filter\n",
285                 vdev->rx_q);
286                 return -1;
287         }
288
289         /* Print out inner MAC and VNI info. */
290         RTE_LOG(INFO, VHOST_DATA,
291                 "(%d) MAC_ADDRESS %02x:%02x:%02x:%02x:%02x:%02x and VNI %d registered\n",
292                 vdev->rx_q,
293                 vdev->mac_address.addr_bytes[0],
294                 vdev->mac_address.addr_bytes[1],
295                 vdev->mac_address.addr_bytes[2],
296                 vdev->mac_address.addr_bytes[3],
297                 vdev->mac_address.addr_bytes[4],
298                 vdev->mac_address.addr_bytes[5],
299                 tenant_id_conf[vdev->rx_q]);
300
301         vxdev.port[portid].vport_id = portid;
302
303         for (i = 0; i < 4; i++) {
304                 /* Local VTEP IP */
305                 vxdev.port_ip |= vxlan_multicast_ips[portid][i] << (8 * i);
306                 /* Remote VTEP IP */
307                 vxdev.port[portid].peer_ip |=
308                         vxlan_overlay_ips[portid][i] << (8 * i);
309         }
310
311         vxdev.out_key = tenant_id_conf[vdev->rx_q];
312         ether_addr_copy(&vxdev.port[portid].peer_mac,
313                         &app_l2_hdr[portid].d_addr);
314         ether_addr_copy(&ports_eth_addr[0],
315                         &app_l2_hdr[portid].s_addr);
316         app_l2_hdr[portid].ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
317
318         ip = &app_ip_hdr[portid];
319         ip->version_ihl = IP_VHL_DEF;
320         ip->type_of_service = 0;
321         ip->total_length = 0;
322         ip->packet_id = 0;
323         ip->fragment_offset = IP_DN_FRAGMENT_FLAG;
324         ip->time_to_live = IP_DEFTTL;
325         ip->next_proto_id = IPPROTO_UDP;
326         ip->hdr_checksum = 0;
327         ip->src_addr = vxdev.port_ip;
328         ip->dst_addr = vxdev.port[portid].peer_ip;
329
330         /* Set device as ready for RX. */
331         vdev->ready = DEVICE_RX;
332
333         return 0;
334 }
335
336 /**
337  * Removes cloud filter. Ensures that nothing is adding buffers to the RX
338  * queue before disabling RX on the device.
339  */
340 void
341 vxlan_unlink(struct vhost_dev *vdev)
342 {
343         unsigned i = 0, rx_count;
344         int ret;
345         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
346         struct rte_eth_tunnel_filter_conf tunnel_filter_conf;
347
348         if (vdev->ready == DEVICE_RX) {
349                 memset(&tunnel_filter_conf, 0,
350                         sizeof(struct rte_eth_tunnel_filter_conf));
351
352                 ether_addr_copy(&ports_eth_addr[0], &tunnel_filter_conf.outer_mac);
353                 ether_addr_copy(&vdev->mac_address, &tunnel_filter_conf.inner_mac);
354                 tunnel_filter_conf.tenant_id = tenant_id_conf[vdev->rx_q];
355                 tunnel_filter_conf.filter_type = tep_filter_type[filter_idx];
356
357                 if (tep_filter_type[filter_idx] ==
358                         RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID)
359                         tunnel_filter_conf.inner_vlan = INNER_VLAN_ID;
360
361                 tunnel_filter_conf.queue_id = vdev->rx_q;
362                 tunnel_filter_conf.tunnel_type = RTE_TUNNEL_TYPE_VXLAN;
363
364                 ret = rte_eth_dev_filter_ctrl(ports[0],
365                                 RTE_ETH_FILTER_TUNNEL,
366                                 RTE_ETH_FILTER_DELETE,
367                                 &tunnel_filter_conf);
368                 if (ret) {
369                         RTE_LOG(ERR, VHOST_DATA,
370                                 "%d Failed to add device MAC address to cloud filter\n",
371                                 vdev->rx_q);
372                         return;
373                 }
374                 for (i = 0; i < ETHER_ADDR_LEN; i++)
375                         vdev->mac_address.addr_bytes[i] = 0;
376
377                 /* Clear out the receive buffers */
378                 rx_count = rte_eth_rx_burst(ports[0],
379                                 (uint16_t)vdev->rx_q,
380                                 pkts_burst, MAX_PKT_BURST);
381
382                 while (rx_count) {
383                         for (i = 0; i < rx_count; i++)
384                                 rte_pktmbuf_free(pkts_burst[i]);
385
386                         rx_count = rte_eth_rx_burst(ports[0],
387                                         (uint16_t)vdev->rx_q,
388                                         pkts_burst, MAX_PKT_BURST);
389                 }
390                 vdev->ready = DEVICE_MAC_LEARNING;
391         }
392 }
393
394 /* Transmit packets after encapsulating */
395 int
396 vxlan_tx_pkts(uint16_t port_id, uint16_t queue_id,
397                 struct rte_mbuf **tx_pkts, uint16_t nb_pkts) {
398         int ret = 0;
399         uint16_t i;
400
401         for (i = 0; i < nb_pkts; i++)
402                 vxlan_tx_process(queue_id, tx_pkts[i]);
403
404         ret = rte_eth_tx_burst(port_id, queue_id, tx_pkts, nb_pkts);
405
406         return ret;
407 }
408
409 /* Check for decapsulation and pass packets directly to VIRTIO device */
410 int
411 vxlan_rx_pkts(int vid, struct rte_mbuf **pkts_burst, uint32_t rx_count)
412 {
413         uint32_t i = 0;
414         uint32_t count = 0;
415         int ret;
416         struct rte_mbuf *pkts_valid[rx_count];
417
418         for (i = 0; i < rx_count; i++) {
419                 if (enable_stats) {
420                         rte_atomic64_add(
421                                 &dev_statistics[vid].rx_bad_ip_csum,
422                                 (pkts_burst[i]->ol_flags & PKT_RX_IP_CKSUM_BAD)
423                                 != 0);
424                         rte_atomic64_add(
425                                 &dev_statistics[vid].rx_bad_ip_csum,
426                                 (pkts_burst[i]->ol_flags & PKT_RX_L4_CKSUM_BAD)
427                                 != 0);
428                 }
429                 ret = vxlan_rx_process(pkts_burst[i]);
430                 if (unlikely(ret < 0))
431                         continue;
432
433                 pkts_valid[count] = pkts_burst[i];
434                         count++;
435         }
436
437         ret = rte_vhost_enqueue_burst(vid, VIRTIO_RXQ, pkts_valid, count);
438         return ret;
439 }