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