Skip to content

Commit 3322623

Browse files
Yuanhan LiuThomas Monjalon
authored andcommitted
vhost: handle request to send RARP
While in former patch we enabled GUEST_ANNOUNCE feature, so that the guest OS will broadcast a GARP message after migration to notify the switch about the new location of migrated VM, the thing is that GUEST_ANNOUNCE is enabled since kernel v3.5 only. For older kernel, VHOST_USER_SEND_RARP request comes to rescue. The payload of this new request is the mac address of the migrated VM, with that, we could construct a RARP message, and then broadcast it to host interfaces. That's how this patch works: - list all interfaces, with the help of SIOCGIFCONF ioctl command - construct an RARP message and broadcast it Cc: Thibaut Collet <thibaut.collet@6wind.com> Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
1 parent d293dac commit 3322623

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

lib/librte_vhost/vhost_user/vhost-net-user.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
100100
[VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
101101
[VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
102102
[VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
103+
[VHOST_USER_SEND_RARP] = "VHOST_USER_SEND_RARP",
103104
};
104105

105106
/**
@@ -437,6 +438,9 @@ vserver_message_handler(int connfd, void *dat, int *remove)
437438
case VHOST_USER_SET_VRING_ENABLE:
438439
user_set_vring_enable(ctx, &msg.payload.state);
439440
break;
441+
case VHOST_USER_SEND_RARP:
442+
user_send_rarp(&msg);
443+
break;
440444

441445
default:
442446
break;

lib/librte_vhost/vhost_user/vhost-net-user.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ typedef enum VhostUserRequest {
6767
VHOST_USER_SET_PROTOCOL_FEATURES = 16,
6868
VHOST_USER_GET_QUEUE_NUM = 17,
6969
VHOST_USER_SET_VRING_ENABLE = 18,
70+
VHOST_USER_SEND_RARP = 19,
7071
VHOST_USER_MAX
7172
} VhostUserRequest;
7273

lib/librte_vhost/vhost_user/virtio-net-user.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@
3434
#include <stdint.h>
3535
#include <stdio.h>
3636
#include <stdlib.h>
37+
#include <string.h>
3738
#include <unistd.h>
3839
#include <sys/mman.h>
3940
#include <sys/types.h>
4041
#include <sys/stat.h>
4142
#include <unistd.h>
43+
#include <sys/ioctl.h>
44+
#include <sys/socket.h>
45+
#include <net/ethernet.h>
46+
#include <netinet/in.h>
47+
#include <netinet/if_ether.h>
48+
#include <linux/if_packet.h>
4249

4350
#include <rte_common.h>
4451
#include <rte_log.h>
@@ -413,3 +420,121 @@ user_set_log_base(struct vhost_device_ctx ctx,
413420

414421
return 0;
415422
}
423+
424+
#define RARP_BUF_SIZE 64
425+
426+
static void
427+
make_rarp_packet(uint8_t *buf, uint8_t *mac)
428+
{
429+
struct ether_header *eth_hdr;
430+
struct ether_arp *rarp;
431+
432+
/* Ethernet header. */
433+
eth_hdr = (struct ether_header *)buf;
434+
memset(&eth_hdr->ether_dhost, 0xff, ETH_ALEN);
435+
memcpy(&eth_hdr->ether_shost, mac, ETH_ALEN);
436+
eth_hdr->ether_type = htons(ETH_P_RARP);
437+
438+
/* RARP header. */
439+
rarp = (struct ether_arp *)(eth_hdr + 1);
440+
rarp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER);
441+
rarp->ea_hdr.ar_pro = htons(ETHERTYPE_IP);
442+
rarp->ea_hdr.ar_hln = ETH_ALEN;
443+
rarp->ea_hdr.ar_pln = 4;
444+
rarp->ea_hdr.ar_op = htons(ARPOP_RREQUEST);
445+
446+
memcpy(&rarp->arp_sha, mac, ETH_ALEN);
447+
memset(&rarp->arp_spa, 0x00, 4);
448+
memcpy(&rarp->arp_tha, mac, 6);
449+
memset(&rarp->arp_tpa, 0x00, 4);
450+
}
451+
452+
453+
static void
454+
send_rarp(const char *ifname, uint8_t *rarp)
455+
{
456+
int fd;
457+
struct ifreq ifr;
458+
struct sockaddr_ll addr;
459+
460+
fd = socket(AF_PACKET, SOCK_RAW, 0);
461+
if (fd < 0) {
462+
perror("socket failed");
463+
return;
464+
}
465+
466+
memset(&ifr, 0, sizeof(struct ifreq));
467+
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
468+
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
469+
perror("failed to get interface index");
470+
close(fd);
471+
return;
472+
}
473+
474+
addr.sll_ifindex = ifr.ifr_ifindex;
475+
addr.sll_halen = ETH_ALEN;
476+
477+
if (sendto(fd, rarp, RARP_BUF_SIZE, 0,
478+
(const struct sockaddr*)&addr, sizeof(addr)) < 0) {
479+
perror("send rarp packet failed");
480+
}
481+
}
482+
483+
484+
/*
485+
* Broadcast a RARP message to all interfaces, to update
486+
* switch's mac table
487+
*/
488+
int
489+
user_send_rarp(struct VhostUserMsg *msg)
490+
{
491+
uint8_t *mac = (uint8_t *)&msg->payload.u64;
492+
uint8_t rarp[RARP_BUF_SIZE];
493+
struct ifconf ifc = {0, };
494+
struct ifreq *ifr;
495+
int nr = 16;
496+
int fd;
497+
uint32_t i;
498+
499+
RTE_LOG(DEBUG, VHOST_CONFIG,
500+
":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
501+
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
502+
503+
make_rarp_packet(rarp, mac);
504+
505+
/*
506+
* Get all interfaces
507+
*/
508+
fd = socket(AF_INET, SOCK_DGRAM, 0);
509+
if (fd < 0) {
510+
perror("failed to create AF_INET socket");
511+
return -1;
512+
}
513+
514+
again:
515+
ifc.ifc_len = sizeof(*ifr) * nr;
516+
ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
517+
518+
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
519+
perror("failed at SIOCGIFCONF");
520+
close(fd);
521+
return -1;
522+
}
523+
524+
if (ifc.ifc_len == (int)sizeof(struct ifreq) * nr) {
525+
/*
526+
* current ifc_buf is not big enough to hold
527+
* all interfaces; double it and try again.
528+
*/
529+
nr *= 2;
530+
goto again;
531+
}
532+
533+
ifr = (struct ifreq *)ifc.ifc_buf;
534+
for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++)
535+
send_rarp(ifr[i].ifr_name, rarp);
536+
537+
close(fd);
538+
539+
return 0;
540+
}

lib/librte_vhost/vhost_user/virtio-net-user.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
#include "vhost-net-user.h"
3939

4040
#define VHOST_USER_PROTOCOL_F_MQ 0
41+
#define VHOST_USER_PROTOCOL_F_RARP 2
4142

42-
#define VHOST_USER_PROTOCOL_FEATURES (1ULL << VHOST_USER_PROTOCOL_F_MQ)
43+
#define VHOST_USER_PROTOCOL_FEATURES ((1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
44+
(1ULL << VHOST_USER_PROTOCOL_F_RARP))
4345

4446
int user_set_mem_table(struct vhost_device_ctx, struct VhostUserMsg *);
4547

@@ -50,6 +52,7 @@ void user_set_vring_kick(struct vhost_device_ctx, struct VhostUserMsg *);
5052
void user_set_protocol_features(struct vhost_device_ctx ctx,
5153
uint64_t protocol_features);
5254
int user_set_log_base(struct vhost_device_ctx ctx, struct VhostUserMsg *);
55+
int user_send_rarp(struct VhostUserMsg *);
5356

5457
int user_get_vring_base(struct vhost_device_ctx, struct vhost_vring_state *);
5558

0 commit comments

Comments
 (0)