1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
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>
13 #include <rte_ethdev.h>
15 #include <rte_string_fns.h>
17 #include <rte_malloc.h>
23 #include "rte_vhost.h"
25 #include "vxlan_setup.h"
27 #define IPV4_HEADER_LEN 20
28 #define UDP_HEADER_LEN 8
29 #define VXLAN_HEADER_LEN 8
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)
36 #define IP_DN_FRAGMENT_FLAG 0x0040
38 /* Used to compare MAC addresses. */
39 #define MAC_ADDR_CMP 0xFFFFFFFFFFFFULL
41 /* Configurable number of RX/TX ring descriptors */
42 #define RTE_TEST_RX_DESC_DEFAULT 1024
43 #define RTE_TEST_TX_DESC_DEFAULT 512
45 /* Default inner VLAN ID */
46 #define INNER_VLAN_ID 100
49 struct vxlan_conf vxdev;
51 struct rte_ipv4_hdr app_ip_hdr[VXLAN_N_PORTS];
52 struct rte_ether_hdr app_l2_hdr[VXLAN_N_PORTS];
54 /* local VTEP IP address */
55 uint8_t vxlan_multicast_ips[2][4] = { {239, 1, 1, 1 }, {239, 1, 2, 1 } };
57 /* Remote VTEP IP address */
58 uint8_t vxlan_overlay_ips[2][4] = { {192, 168, 10, 1}, {192, 168, 30, 1} };
60 /* Remote VTEP MAC address */
61 uint8_t peer_mac[6] = {0x00, 0x11, 0x01, 0x00, 0x00, 0x01};
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,};
68 /* Options for configuring ethernet port */
69 static struct rte_eth_conf port_conf = {
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),
87 * The one or two device(s) that belongs to the same tenant ID can
88 * be assigned in a VM.
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,
102 * Initialises a given port using global settings and with the rx buffers
103 * coming from the mbuf_pool passed as parameter
106 vxlan_port_init(uint16_t port, struct rte_mempool *mbuf_pool)
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;
120 pconf->dst_port = udp_port;
122 retval = rte_eth_dev_info_get(port, &dev_info);
124 rte_exit(EXIT_FAILURE,
125 "Error during getting device (port %u) info: %s\n",
126 port, strerror(-retval));
128 if (dev_info.max_rx_queues > MAX_QUEUES) {
129 rte_exit(EXIT_FAILURE,
130 "please define MAX_QUEUES no less than %u in %s\n",
131 dev_info.max_rx_queues, __FILE__);
134 rxconf = &dev_info.default_rxconf;
135 txconf = &dev_info.default_txconf;
137 if (!rte_eth_dev_is_valid_port(port))
140 rx_rings = nb_devices;
141 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
142 local_port_conf.txmode.offloads |=
143 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
144 /* Configure ethernet device. */
145 retval = rte_eth_dev_configure(port, rx_rings, tx_rings,
150 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &rx_ring_size,
155 /* Setup the queues. */
156 rxconf->offloads = local_port_conf.rxmode.offloads;
157 for (q = 0; q < rx_rings; q++) {
158 retval = rte_eth_rx_queue_setup(port, q, rx_ring_size,
159 rte_eth_dev_socket_id(port),
165 txconf->offloads = local_port_conf.txmode.offloads;
166 for (q = 0; q < tx_rings; q++) {
167 retval = rte_eth_tx_queue_setup(port, q, tx_ring_size,
168 rte_eth_dev_socket_id(port),
174 /* Start the device. */
175 retval = rte_eth_dev_start(port);
179 /* Configure UDP port for UDP tunneling */
180 tunnel_udp.udp_port = udp_port;
181 tunnel_udp.prot_type = RTE_TUNNEL_TYPE_VXLAN;
182 retval = rte_eth_dev_udp_tunnel_port_add(port, &tunnel_udp);
185 retval = rte_eth_macaddr_get(port, &ports_eth_addr[port]);
189 RTE_LOG(INFO, PORT, "Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
190 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
192 ports_eth_addr[port].addr_bytes[0],
193 ports_eth_addr[port].addr_bytes[1],
194 ports_eth_addr[port].addr_bytes[2],
195 ports_eth_addr[port].addr_bytes[3],
196 ports_eth_addr[port].addr_bytes[4],
197 ports_eth_addr[port].addr_bytes[5]);
199 if (tso_segsz != 0) {
200 struct rte_eth_dev_info dev_info;
201 rte_eth_dev_info_get(port, &dev_info);
202 if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) == 0)
203 RTE_LOG(WARNING, PORT,
204 "hardware TSO offload is not supported\n");
210 vxlan_rx_process(struct rte_mbuf *pkt)
215 ret = decapsulation(pkt);
221 vxlan_tx_process(uint8_t queue_id, struct rte_mbuf *pkt)
224 encapsulation(pkt, queue_id);
230 * This function learns the MAC address of the device and set init
231 * L2 header and L3 header info.
234 vxlan_link(struct vhost_dev *vdev, struct rte_mbuf *m)
237 struct rte_ether_hdr *pkt_hdr;
238 uint64_t portid = vdev->vid;
239 struct rte_ipv4_hdr *ip;
241 struct rte_eth_tunnel_filter_conf tunnel_filter_conf;
243 if (unlikely(portid >= VXLAN_N_PORTS)) {
244 RTE_LOG(INFO, VHOST_DATA,
245 "(%d) WARNING: Not configuring device,"
246 "as already have %d ports for VXLAN.",
247 vdev->vid, VXLAN_N_PORTS);
251 /* Learn MAC address of guest device from packet */
252 pkt_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
253 if (rte_is_same_ether_addr(&(pkt_hdr->s_addr), &vdev->mac_address)) {
254 RTE_LOG(INFO, VHOST_DATA,
255 "(%d) WARNING: This device is using an existing"
256 " MAC address and has not been registered.\n",
261 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
262 vdev->mac_address.addr_bytes[i] =
263 vxdev.port[portid].vport_mac.addr_bytes[i] =
264 pkt_hdr->s_addr.addr_bytes[i];
265 vxdev.port[portid].peer_mac.addr_bytes[i] = peer_mac[i];
268 memset(&tunnel_filter_conf, 0,
269 sizeof(struct rte_eth_tunnel_filter_conf));
271 rte_ether_addr_copy(&ports_eth_addr[0], &tunnel_filter_conf.outer_mac);
272 tunnel_filter_conf.filter_type = tep_filter_type[filter_idx];
275 rte_ether_addr_copy(&vdev->mac_address, &tunnel_filter_conf.inner_mac);
277 tunnel_filter_conf.queue_id = vdev->rx_q;
278 tunnel_filter_conf.tenant_id = tenant_id_conf[vdev->rx_q];
280 if (tep_filter_type[filter_idx] == RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID)
281 tunnel_filter_conf.inner_vlan = INNER_VLAN_ID;
283 tunnel_filter_conf.tunnel_type = RTE_TUNNEL_TYPE_VXLAN;
285 ret = rte_eth_dev_filter_ctrl(ports[0],
286 RTE_ETH_FILTER_TUNNEL,
288 &tunnel_filter_conf);
290 RTE_LOG(ERR, VHOST_DATA,
291 "%d Failed to add device MAC address to cloud filter\n",
296 /* Print out inner MAC and VNI info. */
297 RTE_LOG(INFO, VHOST_DATA,
298 "(%d) MAC_ADDRESS %02x:%02x:%02x:%02x:%02x:%02x and VNI %d registered\n",
300 vdev->mac_address.addr_bytes[0],
301 vdev->mac_address.addr_bytes[1],
302 vdev->mac_address.addr_bytes[2],
303 vdev->mac_address.addr_bytes[3],
304 vdev->mac_address.addr_bytes[4],
305 vdev->mac_address.addr_bytes[5],
306 tenant_id_conf[vdev->rx_q]);
308 vxdev.port[portid].vport_id = portid;
310 for (i = 0; i < 4; i++) {
312 vxdev.port_ip |= vxlan_multicast_ips[portid][i] << (8 * i);
314 vxdev.port[portid].peer_ip |=
315 vxlan_overlay_ips[portid][i] << (8 * i);
318 vxdev.out_key = tenant_id_conf[vdev->rx_q];
319 rte_ether_addr_copy(&vxdev.port[portid].peer_mac,
320 &app_l2_hdr[portid].d_addr);
321 rte_ether_addr_copy(&ports_eth_addr[0],
322 &app_l2_hdr[portid].s_addr);
323 app_l2_hdr[portid].ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
325 ip = &app_ip_hdr[portid];
326 ip->version_ihl = IP_VHL_DEF;
327 ip->type_of_service = 0;
328 ip->total_length = 0;
330 ip->fragment_offset = IP_DN_FRAGMENT_FLAG;
331 ip->time_to_live = IP_DEFTTL;
332 ip->next_proto_id = IPPROTO_UDP;
333 ip->hdr_checksum = 0;
334 ip->src_addr = vxdev.port_ip;
335 ip->dst_addr = vxdev.port[portid].peer_ip;
337 /* Set device as ready for RX. */
338 vdev->ready = DEVICE_RX;
344 * Removes cloud filter. Ensures that nothing is adding buffers to the RX
345 * queue before disabling RX on the device.
348 vxlan_unlink(struct vhost_dev *vdev)
350 unsigned i = 0, rx_count;
352 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
353 struct rte_eth_tunnel_filter_conf tunnel_filter_conf;
355 if (vdev->ready == DEVICE_RX) {
356 memset(&tunnel_filter_conf, 0,
357 sizeof(struct rte_eth_tunnel_filter_conf));
359 rte_ether_addr_copy(&ports_eth_addr[0],
360 &tunnel_filter_conf.outer_mac);
361 rte_ether_addr_copy(&vdev->mac_address,
362 &tunnel_filter_conf.inner_mac);
363 tunnel_filter_conf.tenant_id = tenant_id_conf[vdev->rx_q];
364 tunnel_filter_conf.filter_type = tep_filter_type[filter_idx];
366 if (tep_filter_type[filter_idx] ==
367 RTE_TUNNEL_FILTER_IMAC_IVLAN_TENID)
368 tunnel_filter_conf.inner_vlan = INNER_VLAN_ID;
370 tunnel_filter_conf.queue_id = vdev->rx_q;
371 tunnel_filter_conf.tunnel_type = RTE_TUNNEL_TYPE_VXLAN;
373 ret = rte_eth_dev_filter_ctrl(ports[0],
374 RTE_ETH_FILTER_TUNNEL,
375 RTE_ETH_FILTER_DELETE,
376 &tunnel_filter_conf);
378 RTE_LOG(ERR, VHOST_DATA,
379 "%d Failed to add device MAC address to cloud filter\n",
383 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
384 vdev->mac_address.addr_bytes[i] = 0;
386 /* Clear out the receive buffers */
387 rx_count = rte_eth_rx_burst(ports[0],
388 (uint16_t)vdev->rx_q,
389 pkts_burst, MAX_PKT_BURST);
392 for (i = 0; i < rx_count; i++)
393 rte_pktmbuf_free(pkts_burst[i]);
395 rx_count = rte_eth_rx_burst(ports[0],
396 (uint16_t)vdev->rx_q,
397 pkts_burst, MAX_PKT_BURST);
399 vdev->ready = DEVICE_MAC_LEARNING;
403 /* Transmit packets after encapsulating */
405 vxlan_tx_pkts(uint16_t port_id, uint16_t queue_id,
406 struct rte_mbuf **tx_pkts, uint16_t nb_pkts) {
410 for (i = 0; i < nb_pkts; i++)
411 vxlan_tx_process(queue_id, tx_pkts[i]);
413 ret = rte_eth_tx_burst(port_id, queue_id, tx_pkts, nb_pkts);
418 /* Check for decapsulation and pass packets directly to VIRTIO device */
420 vxlan_rx_pkts(int vid, struct rte_mbuf **pkts_burst, uint32_t rx_count)
425 struct rte_mbuf *pkts_valid[rx_count];
427 for (i = 0; i < rx_count; i++) {
430 &dev_statistics[vid].rx_bad_ip_csum,
431 (pkts_burst[i]->ol_flags & PKT_RX_IP_CKSUM_BAD)
434 &dev_statistics[vid].rx_bad_ip_csum,
435 (pkts_burst[i]->ol_flags & PKT_RX_L4_CKSUM_BAD)
438 ret = vxlan_rx_process(pkts_burst[i]);
439 if (unlikely(ret < 0))
442 pkts_valid[count] = pkts_burst[i];
446 ret = rte_vhost_enqueue_burst(vid, VIRTIO_RXQ, pkts_valid, count);