I've created a virtual interface that used to get all outgoing packets, encapsulate with additional header, before resend them to their final destination through the physical adapter.
In order to choose the optimal MTU size that won't trigger fragmentation, I'd like to calculate the PMTU between the physical interface and the destination, subtracted by the encapsulation overhead. Then I'll set the virtual adapter's MTU with this result.
In order to do so, I poll the overall MTU cache using sysctl on macOS.
Here's the relevant code :
size_t sz_needed = 0;
std::vector<char> route_table;
std::array<int, kSysctlMibLength> mib;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = 0;
mib[4] = NET_RT_DUMP;
mib[5] = 0;
// first check the size
if (sysctl(mib.data(), kSysctlMibLength, nullptr, &sz_needed, nullptr, 0) < 0) {
return {};
}
// allocate needed size for pba
route_table.reserve(sz_needed);
// than I get the route table contents
if (sysctl(mib.data(), kSysctlMibLength, route_table.data(), &sz_needed, nullptr, 0) < 0) {
return {};
}
// now we need to get the rt_msghdr.rt_metrics.rmx_mtu /* MTU for this path */
Should I expect that this mtu cache gets updated automatically if the following flag is on net.inet.tcp.path_mtu_discovery ?