pespin has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/38481?usp=email )


Change subject: core/netdev: Add API osmo_netdev_set_mtu()
......................................................................

core/netdev: Add API osmo_netdev_set_mtu()

This API will be used by osmo-ggsn to set the MTU of the tun device
created to handle an APN.

Related: OS#6298
Related: SYS#7122
Change-Id: I705d71813090f8af5a578d2a1f5920fece03bc5f
---
M TODO-RELEASE
M include/osmocom/core/netdev.h
M src/core/libosmocore.map
M src/core/netdev.c
4 files changed, 67 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/81/38481/1

diff --git a/TODO-RELEASE b/TODO-RELEASE
index b3e4742..80387fa 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -9,4 +9,5 @@
 #library       what                    description / commit summary line
 gb             ADD                     add 
bssgp_parse_cell_id2/bssgp_create_cell_id2
 gsm            ADD                     add osmo_rai_to_gprs/gprs_rai_to_osmo
-core  ADD     add API osmo_tundev_get_fd()
\ No newline at end of file
+core  ADD     add API osmo_tundev_get_fd()
+core  ADD     add API osmo_netdev_set_mtu()
\ No newline at end of file
diff --git a/include/osmocom/core/netdev.h b/include/osmocom/core/netdev.h
index 3238ec3..fb0f124 100644
--- a/include/osmocom/core/netdev.h
+++ b/include/osmocom/core/netdev.h
@@ -36,6 +36,8 @@
 int osmo_netdev_set_netns_name(struct osmo_netdev *netdev, const char *netns);
 const char *osmo_netdev_get_netns_name(const struct osmo_netdev *netdev);

+int osmo_netdev_set_mtu(struct osmo_netdev *netdev, uint32_t mtu);
+
 void osmo_netdev_set_ifupdown_ind_cb(struct osmo_netdev *netdev, 
osmo_netdev_ifupdown_ind_cb_t ifupdown_ind_cb);
 void osmo_netdev_set_dev_name_chg_cb(struct osmo_netdev *netdev, 
osmo_netdev_dev_name_chg_cb_t dev_name_chg_cb);
 void osmo_netdev_set_mtu_chg_cb(struct osmo_netdev *netdev, 
osmo_netdev_mtu_chg_cb_t mtu_chg_cb);
diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map
index c48a2b9..cd5dc94 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -321,6 +321,7 @@
 osmo_netdev_set_dev_name_chg_cb;
 osmo_netdev_set_ifindex;
 osmo_netdev_set_ifupdown_ind_cb;
+osmo_netdev_set_mtu;
 osmo_netdev_set_mtu_chg_cb;
 osmo_netdev_set_netns_name;
 osmo_netdev_set_priv_data;
diff --git a/src/core/netdev.c b/src/core/netdev.c
index 7ef7657..8e7fc28 100644
--- a/src/core/netdev.c
+++ b/src/core/netdev.c
@@ -68,7 +68,7 @@

 #include <stdio.h>
 #include <stdlib.h>
-#include <stdio.h>
+#include <inttypes.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
@@ -531,6 +531,35 @@
        return 0;
 }

+static int netdev_mnl_set_mtu(struct osmo_mnl *omnl, unsigned int if_index,
+                             const char *dev_name, uint32_t mtu)
+{
+       char buf[MNL_SOCKET_BUFFER_SIZE];
+       struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
+       struct ifinfomsg *ifm;
+       unsigned int change = 0;
+       unsigned int flags = 0;
+
+       nlh->nlmsg_type = RTM_NEWLINK;
+       nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+       nlh->nlmsg_seq = time(NULL);
+
+       ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
+       ifm->ifi_family = AF_UNSPEC;
+       ifm->ifi_change = change;
+       ifm->ifi_flags = flags;
+       ifm->ifi_index = if_index;
+
+       mnl_attr_put_u32(nlh, IFLA_MTU, mtu);
+
+       if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) {
+               LOGP(DLGLOBAL, LOGL_ERROR, "mnl_socket_sendto\n");
+               return -EIO;
+       }
+
+       return 0;
+}
+
 static int netdev_mnl_add_addr(struct osmo_mnl *omnl, unsigned int if_index, 
const struct osmo_sockaddr *osa, uint8_t prefix)
 {
        char buf[MNL_SOCKET_BUFFER_SIZE];
@@ -857,6 +886,38 @@
        return netdev->dev_name;
 }

+/*! Set the MTU of the network interface
+ *  \param[in] netdev The netdev object where the field is set
+ *  \param[in] mtu The MTU to be set on the network interface
+ *  \returns 0 on success; negative on error
+ *
+ */
+int osmo_netdev_set_mtu(struct osmo_netdev *netdev, uint32_t mtu)
+{
+       struct osmo_netns_switch_state switch_state;
+       int rc;
+
+       if (!netdev->registered)
+               return -ENODEV;
+
+       LOGNETDEV(netdev, LOGL_NOTICE, "Setting dev %s MTU %" PRIu32 "\n",
+                 netdev->dev_name, mtu);
+
+       NETDEV_NETNS_ENTER(netdev, &switch_state, "set_mtu");
+
+#if ENABLE_LIBMNL
+       rc = netdev_mnl_set_mtu(netdev->netns_ctx->omnl, netdev->ifindex,
+                               netdev->dev_name, mtu);
+#else
+       LOGNETDEV(netdev, LOGL_ERROR, "%s: NOT SUPPORTED. Build libosmocore 
with --enable-libmnl.\n", __func__);
+       rc = -ENOTSUP;
+#endif
+
+       NETDEV_NETNS_EXIT(netdev, &switch_state, "set_mtu");
+
+       return rc;
+}
+
 /*! Bring netdev interface UP or DOWN.
  *  \param[in] netdev The netdev object managing the netdev interface
  *  \param[in] ifupdown true to set the interface UP, false to set it DOWN

--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/38481?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I705d71813090f8af5a578d2a1f5920fece03bc5f
Gerrit-Change-Number: 38481
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <[email protected]>

Reply via email to