set MTU in C programmatically
Asked Answered
G

4

6

A client requested that the MTU limit should be 1492.

Is there a way to do it in the source code (program in C)?

Is there any other way to do it in general? (ifconfig?)

Why does somebody needs to modify MTU to a certain limit? What is the benefit? And the most important: By changing the MTU is there any risk to break the code?

Gyratory answered 5/5, 2011 at 20:42 Comment(0)
G
3

It's not about speed directly; By increasing MTU you're reducing overhead, which is data that is responsible for the proper delivery of the package but it's not usable by the end user; This can have a increase in speed but only for heavy traffic;

Also, if you increase MTU, you're prone to increase the number of packets that are dropped (since you have a fixed bit error probability and more bits in a packet), eventually causing a decrease in performance with resent packets, etc... So it's a compromise between overhead and data integrity;

I guess that it is more of a interface configuration than something you control with a program; So it's better to stick with 'ifconfig' command or find the equivalent solution for Windows.

Gannet answered 5/5, 2011 at 21:7 Comment(2)
Is it possible to set it through programming? Is there an API to set the MTU of a connection or interface?Polecat
I do believe it's ~possible~, but not really sure how. Problably something very platform specific...Gannet
J
7

Programmaticaly way using C:

int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
struct ifreq ifr;
strcpy(ifr.ifr_name, "eth0");
if(!ioctl(sock, SIOCGIFMTU, &ifr)) {
  ifr.ifr_mtu // Contains current mtu value
}
ifr.ifr_mtu = ... // Change value if it needed
if(!ioctl(sock, SIOCSIFMTU, &ifr)) {
  // Mtu changed successfully
}

It works at least on Ubuntu, see man netdevice.

Jase answered 14/1, 2016 at 14:3 Comment(1)
Cool. ioctl() function is also available in Windows using Ws2_32.lib, Ws2_32.dll and winsock2.hBonine
U
3

The MTU is a number which defines the maximum transmission unit per packet. The bigger it is, the faster your data will be sent. Assuming you can send npackets/s of msize, if m grows, m*n grows too.

I think your client wants that MTU because of its network equipment (maybe ethernet 802.3). Some equipment handel biggest frames size than others.

You can use ifconfig with the option mtuto change its value: ifconfig eth0 mtu 1492.

Unbridled answered 5/5, 2011 at 20:53 Comment(3)
so, is it only a matter of speed and configuration (ethernet,pppoE etc)? Is there a risk to break the existing code?Gyratory
as far as I know, it is more and hardware configuration issue than a software one.Unbridled
On Windows in order to achieve the maximum performance it is recommended to use MTU <= 1024.Dangerfield
G
3

It's not about speed directly; By increasing MTU you're reducing overhead, which is data that is responsible for the proper delivery of the package but it's not usable by the end user; This can have a increase in speed but only for heavy traffic;

Also, if you increase MTU, you're prone to increase the number of packets that are dropped (since you have a fixed bit error probability and more bits in a packet), eventually causing a decrease in performance with resent packets, etc... So it's a compromise between overhead and data integrity;

I guess that it is more of a interface configuration than something you control with a program; So it's better to stick with 'ifconfig' command or find the equivalent solution for Windows.

Gannet answered 5/5, 2011 at 21:7 Comment(2)
Is it possible to set it through programming? Is there an API to set the MTU of a connection or interface?Polecat
I do believe it's ~possible~, but not really sure how. Problably something very platform specific...Gannet
E
1

the modern way to set interface parameters is via sysfs

sudo sh -c 'echo 1492 > /sys/class/net/tun/mtu'

By C, just open and write as files

Enjambment answered 24/5, 2019 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.