Skip to content

Commit bd3e8c5

Browse files
author
Octavian Purdila
committed
Merge pull request #45 from stfairy/set-mtu
lkl tools: allow setting the MTU of an interface
2 parents 436c0cf + fcc7f15 commit bd3e8c5

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

tools/lkl/include/lkl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ int lkl_if_up(int ifindex);
170170
*/
171171
int lkl_if_down(int ifindex);
172172

173+
/**
174+
* lkl_if_set_mtu - set MTU on interface
175+
*
176+
* @ifindex - the ifindex of the interface
177+
* @mtu - the requested MTU size
178+
* @returns - return 0 if no error: otherwise negative value returns
179+
*/
180+
int lkl_if_set_mtu(int ifindex, int mtu);
181+
173182
/**
174183
* lkl_if_set_ipv4 - set IPv4 address on interface
175184
*

tools/lkl/lib/hijack/init.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ hijack_init(void)
3535
{
3636
int ret, i, dev_null, nd_id = -1, nd_ifindex = -1;
3737
char *tap = getenv("LKL_HIJACK_NET_TAP");
38+
char *mtu_str = getenv("LKL_HIJACK_NET_MTU");
3839
char *ip = getenv("LKL_HIJACK_NET_IP");
3940
char *netmask_len = getenv("LKL_HIJACK_NET_NETMASK_LEN");
4041
char *gateway = getenv("LKL_HIJACK_NET_GATEWAY");
@@ -105,6 +106,14 @@ hijack_init(void)
105106
nd_id, lkl_strerror(nd_ifindex));
106107
}
107108

109+
if (nd_ifindex >= 0 && mtu_str) {
110+
int mtu = atoi(mtu_str);
111+
112+
ret = lkl_if_set_mtu(nd_ifindex, mtu);
113+
if (ret < 0)
114+
fprintf(stderr, "failed to set MTU: %s\n", lkl_strerror(ret));
115+
}
116+
108117
if (nd_ifindex >= 0 && ip && netmask_len) {
109118
unsigned int addr = inet_addr(ip);
110119
int nmlen = atoi(netmask_len);

tools/lkl/lib/net.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ int lkl_if_down(int ifindex)
6565
return err;
6666
}
6767

68+
int lkl_if_set_mtu(int ifindex, int mtu)
69+
{
70+
struct lkl_ifreq ifr;
71+
int err, sock;
72+
73+
sock = lkl_sys_socket(LKL_AF_INET, LKL_SOCK_DGRAM, 0);
74+
if (sock < 0)
75+
return sock;
76+
77+
err = ifindex_to_name(sock, &ifr, ifindex);
78+
if (err < 0)
79+
return err;
80+
81+
ifr.lkl_ifr_mtu = mtu;
82+
83+
err = lkl_sys_ioctl(sock, LKL_SIOCSIFMTU, (long)&ifr);
84+
85+
lkl_sys_close(sock);
86+
87+
return err;
88+
}
89+
6890
int lkl_if_set_ipv4(int ifindex, unsigned int addr, unsigned int netmask_len)
6991
{
7092
struct lkl_ifreq ifr;

0 commit comments

Comments
 (0)