Skip to content

Commit

Permalink
Support to set a MAC address on an interface (Linux only)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmj committed Sep 17, 2024
1 parent 3d430a6 commit 71ed8ff
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The modules that are currently defined are given below:
- hypervisor : General hypervisor management
- bridge : bridges management
- iol_bridge : IOL (IOS on Linux) bridges management
- docker : Docker veth management
- docker : Docker management
- brctl : Linux bridge management

### Hypervisor module ("hypervisor")
Expand Down Expand Up @@ -414,6 +414,14 @@ docker move_to_ns guestif 6367 eth0
100-guestif moved to namespace 6367
```

- **docker set_mac_addr** *\<interface\>* *\<mac_addr\>*
*\<mac_addr\>*: Set a MAC address on an interface.

``` {.bash}
docker set_mac_addr tap-gns3-e0 12:34:56:78:12:42
100-MAC address 12:34:56:78:12:42" has been successfully set on interface tap-gns3-e0
```

- **docker delete_veth** *\<interface_name\>*: Delete virtual
Ethernet interface.

Expand Down
42 changes: 42 additions & 0 deletions src/hypervisor_docker.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,53 @@ static int cmd_move_ns(hypervisor_conn_t *conn, int argc, char *argv[])
return err;
}

static int cmd_set_mac_addr(hypervisor_conn_t *conn, int argc, char *argv[])
{
int fd;
struct ifreq ifr;
char *interface = argv[0];
char mac_char[17];

if (strlen(argv[1]) != 17) {
hypervisor_send_reply(conn, HSC_ERR_CREATE, 1, "invalid MAC address format");
return (-1);
}

strncpy(mac_char, argv[1], 17);
sscanf(mac_char, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&ifr.ifr_hwaddr.sa_data[0],
&ifr.ifr_hwaddr.sa_data[1],
&ifr.ifr_hwaddr.sa_data[2],
&ifr.ifr_hwaddr.sa_data[3],
&ifr.ifr_hwaddr.sa_data[4],
&ifr.ifr_hwaddr.sa_data[5]
);

if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
hypervisor_send_reply(conn, HSC_ERR_CREATE, 1, "could not create socket: %s", strerror(errno));
return (-1);
}

strncpy(ifr.ifr_name, interface, strlen(interface));
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;

if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
hypervisor_send_reply(conn, HSC_ERR_CREATE, 1, "could not set a MAC address on interface %s", interface);
close(fd);
return (-1);
}

hypervisor_send_reply(conn, HSC_INFO_OK, 1, "MAC address %s has been successfully set on interface %s", mac_char, interface);
close(fd);
return (0);
}

/* Docker commands */
static hypervisor_cmd_t docker_cmd_array[] = {
{ "create_veth", 2, 2, cmd_create_veth_pair, NULL },
{ "delete_veth", 1, 1, cmd_delete_veth, NULL },
{ "move_to_ns", 3, 3, cmd_move_ns, NULL },
{ "set_mac_addr", 2, 2, cmd_set_mac_addr, NULL },
{ NULL, -1, -1, NULL, NULL },
};

Expand Down

0 comments on commit 71ed8ff

Please sign in to comment.