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