2 * Copyright (c) 2018, Microsoft Corporation.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * vmbus.c: Linux user space VMBus bus management.
19 #include <vppinfra/linux/sysfs.h>
21 #include <vlib/vlib.h>
22 #include <vlib/vmbus/vmbus.h>
23 #include <vlib/unix/unix.h>
25 #include <sys/types.h>
29 #include <sys/ioctl.h>
31 #include <linux/ethtool.h>
32 #include <linux/sockios.h>
34 static const char sysfs_vmbus_dev_path[] = "/sys/bus/vmbus/devices";
35 static const char sysfs_vmbus_drv_path[] = "/sys/bus/vmbus/drivers";
36 static const char sysfs_class_net_path[] = "/sys/class/net";
37 static const char uio_drv_name[] = "uio_hv_generic";
38 static const char netvsc_uuid[] = "f8615163-df3e-46c5-913f-f2d2f965ed0e";
45 } linux_vmbus_region_t;
55 vlib_vmbus_dev_handle_t handle;
56 vlib_vmbus_addr_t addr;
58 /* Device File descriptor */
61 /* Minor device for uio device. */
67 } linux_vmbus_device_t;
69 /* Pool of VMBUS devices. */
72 vlib_main_t *vlib_main;
73 linux_vmbus_device_t *linux_vmbus_devices;
77 linux_vmbus_main_t linux_vmbus_main;
79 static linux_vmbus_device_t *
80 linux_vmbus_get_device (vlib_vmbus_dev_handle_t h)
82 linux_vmbus_main_t *lpm = &linux_vmbus_main;
83 return pool_elt_at_index (lpm->linux_vmbus_devices, h);
87 vlib_vmbus_get_private_data (vlib_vmbus_dev_handle_t h)
89 linux_vmbus_device_t *d = linux_vmbus_get_device (h);
90 return d->private_data;
94 vlib_vmbus_set_private_data (vlib_vmbus_dev_handle_t h, uword private_data)
96 linux_vmbus_device_t *d = linux_vmbus_get_device (h);
97 d->private_data = private_data;
101 vlib_vmbus_get_addr (vlib_vmbus_dev_handle_t h)
103 linux_vmbus_device_t *d = linux_vmbus_get_device (h);
107 /* Call to allocate/initialize the vmbus subsystem.
108 This is not an init function so that users can explicitly enable
109 vmbus only when it's needed. */
110 clib_error_t *vmbus_bus_init (vlib_main_t * vm);
112 linux_vmbus_main_t linux_vmbus_main;
115 * Take VMBus address represented in standard form like:
116 * "f2c086b2-ff2e-11e8-88de-7bad0a57de05" and convert
120 unformat_vlib_vmbus_addr (unformat_input_t *input, va_list *args)
122 vlib_vmbus_addr_t *addr = va_arg (*args, vlib_vmbus_addr_t *);
126 if (!unformat (input, "%U", unformat_token, "a-zA-Z0-9-", &s))
129 if (vec_len (s) != 36)
132 if (s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-')
135 clib_memmove (s + 8, s + 9, 4);
136 clib_memmove (s + 12, s + 14, 4);
137 clib_memmove (s + 16, s + 19, 4);
138 clib_memmove (s + 20, s + 24, 12);
140 for (int i = 0; i < 32; i++)
141 if (s[i] >= '0' && s[i] <= '9')
143 else if (s[i] >= 'A' && s[i] <= 'F')
145 else if (s[i] >= 'a' && s[i] <= 'f')
150 for (int i = 0; i < 16; i++)
151 addr->guid[i] = s[2 * i] * 16 + s[2 * i + 1];
160 /* Convert bus address to standard UUID string */
162 format_vlib_vmbus_addr (u8 *s, va_list *va)
164 vlib_vmbus_addr_t *addr = va_arg (*va, vlib_vmbus_addr_t *);
165 u8 *bytes = addr->guid;
167 for (int i = 0; i < 4; i++)
168 s = format (s, "%02x", bytes++[0]);
170 for (int i = 0; i < 2; i++)
171 s = format (s, "%02x", bytes++[0]);
173 for (int i = 0; i < 2; i++)
174 s = format (s, "%02x", bytes++[0]);
176 for (int i = 0; i < 2; i++)
177 s = format (s, "%02x", bytes++[0]);
179 for (int i = 0; i < 6; i++)
180 s = format (s, "%02x", bytes++[0]);
185 /* workaround for mlx bug, bring lower device up before unbind */
186 static clib_error_t *
187 vlib_vmbus_raise_lower (int fd, const char *upper_name)
189 clib_error_t *error = 0;
195 clib_memset (&ifr, 0, sizeof (ifr));
197 dev_net_dir = format (0, "%s/%s%c", sysfs_class_net_path, upper_name, 0);
199 dir = opendir ((char *) dev_net_dir);
203 error = clib_error_return (0, "VMBUS failed to open %s", dev_net_dir);
207 while ((e = readdir (dir)))
209 /* look for lower_enXXXX */
210 if (strncmp (e->d_name, "lower_", 6))
213 strncpy (ifr.ifr_name, e->d_name + 6, IFNAMSIZ - 1);
219 goto done; /* no lower device */
221 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
222 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
224 else if (!(ifr.ifr_flags & IFF_UP))
226 ifr.ifr_flags |= IFF_UP;
228 if (ioctl (fd, SIOCSIFFLAGS, &ifr) < 0)
229 error = clib_error_return_unix (0, "ioctl set intf %s flags",
233 vec_free (dev_net_dir);
238 directory_exists (char *path)
240 struct stat s = { 0 };
241 if (stat (path, &s) == -1)
244 return S_ISDIR (s.st_mode);
248 vlib_vmbus_bind_to_uio (vlib_vmbus_addr_t * addr)
250 clib_error_t *error = 0;
253 static int uio_new_id_needed = 1;
256 u8 *s = 0, *driver_name;
260 dev_dir_name = format (0, "%s/%U", sysfs_vmbus_dev_path,
261 format_vlib_vmbus_addr, addr);
263 driver_name = clib_file_get_resolved_basename ("%v/driver", dev_dir_name);
265 /* skip if not using the Linux kernel netvsc driver */
266 if (!driver_name || strcmp ("hv_netvsc", (char *) driver_name) != 0)
269 /* if uio_hv_generic is not loaded, then can't use native DPDK driver. */
270 if (!directory_exists ("/sys/module/uio_hv_generic"))
273 s = format (s, "%v/net%c", dev_dir_name, 0);
274 dir = opendir ((char *) s);
275 vec_reset_length (s);
278 return clib_error_return (0, "VMBUS failed to open %s", s);
280 while ((e = readdir (dir)))
282 if (e->d_name[0] == '.') /* skip . and .. */
285 ifname = strdup (e->d_name);
292 error = clib_error_return (0,
293 "VMBUS device %U eth not found",
294 format_vlib_vmbus_addr, addr);
299 clib_memset (&ifr, 0, sizeof (ifr));
300 strncpy (ifr.ifr_name, ifname, IFNAMSIZ - 1);
302 /* read up/down flags */
303 fd = socket (PF_INET, SOCK_DGRAM, 0);
306 error = clib_error_return_unix (0, "socket");
310 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
312 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
318 if (ifr.ifr_flags & IFF_UP)
320 error = clib_error_return (
321 0, "Skipping VMBUS device %U as host interface %s is up",
322 format_vlib_vmbus_addr, addr, ifname);
327 /* tell uio_hv_generic about netvsc device type */
328 if (uio_new_id_needed)
330 vec_reset_length (s);
331 s = format (s, "%s/%s/new_id%c", sysfs_vmbus_drv_path, uio_drv_name, 0);
332 error = clib_sysfs_write ((char *) s, "%s", netvsc_uuid);
333 /* If device already exists, we can bind/unbind/override driver */
336 if (error->code == EEXIST)
338 clib_error_free (error);
347 uio_new_id_needed = 0;
350 error = vlib_vmbus_raise_lower (fd, ifname);
356 /* prefer the simplier driver_override model */
357 vec_reset_length (s);
358 s = format (s, "%/driver_override%c", dev_dir_name, 0);
359 if (access ((char *) s, F_OK) == 0)
361 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
365 vec_reset_length (s);
367 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
369 clib_sysfs_write ((char *) s, "%U", format_vlib_vmbus_addr, addr);
374 vec_reset_length (s);
376 s = format (s, "%s/%s/bind%c", sysfs_vmbus_drv_path, uio_drv_name, 0);
378 clib_sysfs_write ((char *) s, "%U", format_vlib_vmbus_addr, addr);
380 vec_reset_length (s);
385 vec_free (dev_dir_name);
386 vec_free (driver_name);
390 static clib_error_t *
391 scan_vmbus_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
393 vlib_vmbus_addr_t addr, **addrv = arg;
394 unformat_input_t input;
395 clib_error_t *err = 0;
397 unformat_init_string (&input, (char *) dev_dir_name,
398 vec_len (dev_dir_name));
400 if (!unformat (&input, "/sys/bus/vmbus/devices/%U",
401 unformat_vlib_vmbus_addr, &addr))
402 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
404 unformat_free (&input);
409 vec_add1 (*addrv, addr);
414 vmbus_addr_cmp (void *v1, void *v2)
416 vlib_vmbus_addr_t *a1 = v1;
417 vlib_vmbus_addr_t *a2 = v2;
419 for (int i = 0; i < ARRAY_LEN (a1->guid); i++)
420 if (a1->guid[i] > a2->guid[i])
422 else if (a1->guid[i] < a2->guid[i])
429 vlib_vmbus_get_all_dev_addrs ()
431 vlib_vmbus_addr_t *addrs = 0;
435 foreach_directory_file ((char *) sysfs_vmbus_dev_path, scan_vmbus_addr,
436 &addrs, /* scan_dirs */ 0);
443 vec_sort_with_function (addrs, vmbus_addr_cmp);
449 linux_vmbus_init (vlib_main_t * vm)
451 linux_vmbus_main_t *pm = &linux_vmbus_main;
459 VLIB_INIT_FUNCTION (linux_vmbus_init) =
461 .runs_before = VLIB_INITS("unix_input_init"),
466 * fd.io coding-style-patch-verification: ON
469 * eval: (c-set-style "gnu")