lists.osmocom.org
Sign In Sign Up
Manage this list Sign In Sign Up

Keyboard Shortcuts

Thread View

  • j: Next unread message
  • k: Previous unread message
  • j a: Jump to all threads
  • j l: Jump to MailingList overview

gerrit-log

Thread Start a new thread
Download
Threads by month
  • ----- 2026 -----
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2025 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2024 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2023 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
  • ----- 2022 -----
  • December
  • November
  • October
  • September
  • August
  • July
  • June
  • May
  • April
  • March
  • February
  • January
gerrit-log@lists.osmocom.org

January 2023

  • 1 participants
  • 2205 discussions
Change in libosmocore[master]: Introduce netns API
by pespin 19 Jan '23

19 Jan '23
pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/31024 ) Change subject: Introduce netns API ...................................................................... Introduce netns API Write a new API and implementation to manage network namespace related operations. This will be used by the upcoming tundev module. Change-Id: I0f2fba2fa42250a07211a7b7f479498f27c529da --- M TODO-RELEASE M include/osmocom/core/Makefile.am A include/osmocom/core/netns.h M src/core/Makefile.am A src/core/netns.c 5 files changed, 237 insertions(+), 1 deletion(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/24/31024/1 diff --git a/TODO-RELEASE b/TODO-RELEASE index 0cf5c80..535b726 100644 --- a/TODO-RELEASE +++ b/TODO-RELEASE @@ -9,4 +9,5 @@ #library what description / commit summary line libosmocore new API osmo_sockaddr_is_any(), osmo_sockaddr_netmask_to_prefixlen() libosmocore ABI breakage OSMO_NUM_DLIB change affecting internal_cat[] -libosmocore new API osmo_netdev_*() \ No newline at end of file +libosmocore new API osmo_netdev_*() +libosmocore new API osmo_netns_*() diff --git a/include/osmocom/core/Makefile.am b/include/osmocom/core/Makefile.am index dc97c25..3b2ef30 100644 --- a/include/osmocom/core/Makefile.am +++ b/include/osmocom/core/Makefile.am @@ -36,6 +36,7 @@ macaddr.h \ msgb.h \ netdev.h \ + netns.h \ panic.h \ prbs.h \ prim.h \ diff --git a/include/osmocom/core/netns.h b/include/osmocom/core/netns.h new file mode 100644 index 0000000..5bbf224 --- /dev/null +++ b/include/osmocom/core/netns.h @@ -0,0 +1,24 @@ +/*! \file netns.h + * Network namespace convenience functions. */ + +#pragma once +#if (!EMBEDDED) + +#if defined(__linux__) + +#include <signal.h> + +struct osmo_netns_switch_state { + sigset_t prev_sigmask; + int prev_nsfd; +}; + +int osmo_netns_open_fd(const char *name); +int osmo_netns_switch_enter(int nsfd, struct osmo_netns_switch_state *state); +int osmo_netns_switch_exit(struct osmo_netns_switch_state *state); + + +#endif /* defined(__linux__) */ + +#endif /* (!EMBEDDED) */ +/*! @} */ diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 3907447..04201a8 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -49,6 +49,7 @@ macaddr.c \ msgb.c \ netdev.c \ + netns.c \ panic.c \ prbs.c \ prim.c \ diff --git a/src/core/netns.c b/src/core/netns.c new file mode 100644 index 0000000..8cf48d4 --- /dev/null +++ b/src/core/netns.c @@ -0,0 +1,209 @@ + +/* Network namespace convenience functions + * (C) 2023 by sysmocom - s.m.f.c. GmbH <info(a)sysmocom.de> + * + * All Rights Reserved + * + * SPDX-License-Identifier: GPL-2.0+ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include "config.h" +#include <asm-generic/errno.h> + +/*! \addtogroup netns + * @{ + * Network namespace convenience functions + * + * \file netns.c */ + +#if defined(__linux__) + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sched.h> +#include <signal.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <sys/mount.h> +#include <sys/param.h> +#include <fcntl.h> +#include <errno.h> + +#include <osmocom/core/utils.h> +#include <osmocom/core/netns.h> + +#define NETNS_PREFIX_PATH "/var/run/netns" +#define NETNS_CURRENT_PATH "/proc/self/ns/net" + +/*! Open a file descriptor for the current network namespace. + * \returns fd of the current network namespace on success; negative in case of error + */ +static int netns_open_current_fd(void) +{ + int fd; + /* store the default namespace for later reference */ + if ((fd = open(NETNS_CURRENT_PATH, O_RDONLY)) < 0) + return -errno; + return fd; +} + +/*! switch to a (non-default) namespace, store existing signal mask in oldmask. + * \param[in] nsfd file descriptor representing the namespace to whch we shall switch + * \param[out] state caller-provided memory location to which state of previous netns is stored + * \returns 0 on success; negative on error */ +int osmo_netns_switch_enter(int nsfd, struct osmo_netns_switch_state *state) +{ + sigset_t intmask; + int rc; + + state->prev_nsfd = -1; + + if (sigfillset(&intmask) < 0) + return -errno; + if ((rc = sigprocmask(SIG_BLOCK, &intmask, &state->prev_sigmask)) != 0) + return -rc; + state->prev_nsfd = netns_open_current_fd(); + + if (setns(nsfd, CLONE_NEWNET) < 0) { + /* restore old mask if we couldn't switch the netns */ + sigprocmask(SIG_SETMASK, &state->prev_sigmask, NULL); + close(state->prev_nsfd); + state->prev_nsfd = -1; + return -errno; + } + return 0; +} + +/*! switch back to the previous namespace, restoring signal mask. + * \param[in] state information about previous netns, filled by osmo_netns_switch_enter() + * \returns 0 on successs; negative on error */ +int osmo_netns_switch_exit(struct osmo_netns_switch_state *state) +{ + if (state->prev_nsfd < 0) + return -EINVAL; + + int rc; + if (setns(state->prev_nsfd, CLONE_NEWNET) < 0) + return -errno; + + close(state->prev_nsfd); + state->prev_nsfd = -1; + + if ((rc = sigprocmask(SIG_SETMASK, &state->prev_sigmask, NULL)) != 0) + return -rc; + return 0; +} + +static int create_netns(const char *name) +{ + char path[MAXPATHLEN]; + sigset_t intmask, oldmask; + int fd, prev_nsfd; + int rc; + + /* create /var/run/netns, if it doesn't exist already */ + rc = mkdir(NETNS_PREFIX_PATH, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + if (rc < 0 && errno != EEXIST) + return rc; + + /* create /var/run/netns/[name], if it doesn't exist already */ + rc = snprintf(path, sizeof(path), "%s/%s", NETNS_PREFIX_PATH, name); + if (rc >= sizeof(path)) + return -ENAMETOOLONG; + fd = open(path, O_RDONLY|O_CREAT|O_EXCL, 0); + if (fd < 0) + return -errno; + if (close(fd) < 0) + return -errno; + + /* mask off all signals, store old signal mask */ + if (sigfillset(&intmask) < 0) + return -errno; + if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0) + return -rc; + + prev_nsfd = netns_open_current_fd(); + if (prev_nsfd < 0) + return prev_nsfd; + + /* create a new network namespace */ + if (unshare(CLONE_NEWNET) < 0) { + rc = -errno; + goto restore_sigmask; + } + if (mount(NETNS_CURRENT_PATH, path, "none", MS_BIND, NULL) < 0) { + rc = -errno; + goto restore_sigmask; + } + + /* switch back to previous namespace */ + if (setns(prev_nsfd, CLONE_NEWNET) < 0) { + rc = -errno; + goto restore_sigmask; + } + +restore_sigmask: + close(prev_nsfd); + + /* restore process mask */ + if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) + return -rc; + + /* might have been set above in case mount fails */ + if (rc < 0) + return rc; + + /* finally, open the created namespace file descriptor from previous ns */ + if ((fd = open(path, O_RDONLY)) < 0) + return -errno; + + return fd; +} + +/*! Open a file descriptor for the network namespace with provided name. + * Creates /var/run/netns/ directory if it doesn't exist already. + * \param[in] name Name of the network namespace (in /var/run/netns/) + * \returns File descriptor of network namespace; negative in case of error + */ +int osmo_netns_open_fd(const char *name) +{ + int rc; + int fd; + char path[MAXPATHLEN]; + + /* path = /var/run/netns/[name] */ + rc = snprintf(path, sizeof(path), "%s/%s", NETNS_PREFIX_PATH, name); + if (rc >= sizeof(path)) + return -ENAMETOOLONG; + + /* If netns already exists, simply open it: */ + fd = open(path, O_RDONLY); + if (fd >= 0) + return fd; + + /* The netns doesn't exist yet, let's create it: */ + fd = create_netns(name); + return fd; +} + +#endif /* defined(__linux__) */ + +/*! @} */ -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/31024 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I0f2fba2fa42250a07211a7b7f479498f27c529da Gerrit-Change-Number: 31024 Gerrit-PatchSet: 1 Gerrit-Owner: pespin <pespin(a)sysmocom.de> Gerrit-MessageType: newchange
1 0
0 0
Change in libosmocore[master]: Introduce tundev API
by pespin 19 Jan '23

19 Jan '23
pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/31025 ) Change subject: Introduce tundev API ...................................................................... Introduce tundev API The data structre is held private so that it can be easily extended in the future. Change-Id: I3463271666df1e85746fb7b06ec45a17024b8c53 --- M TODO-RELEASE M include/osmocom/core/Makefile.am A include/osmocom/core/tun.h M src/core/Makefile.am A src/core/tun.c 5 files changed, 475 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/25/31025/1 diff --git a/TODO-RELEASE b/TODO-RELEASE index 535b726..572cba3 100644 --- a/TODO-RELEASE +++ b/TODO-RELEASE @@ -11,3 +11,4 @@ libosmocore ABI breakage OSMO_NUM_DLIB change affecting internal_cat[] libosmocore new API osmo_netdev_*() libosmocore new API osmo_netns_*() +libosmocore new API osmo_tundev_*() diff --git a/include/osmocom/core/Makefile.am b/include/osmocom/core/Makefile.am index 3b2ef30..e1cd92a 100644 --- a/include/osmocom/core/Makefile.am +++ b/include/osmocom/core/Makefile.am @@ -55,6 +55,7 @@ thread.h \ timer.h \ timer_compat.h \ + tun.h \ utils.h \ write_queue.h \ sockaddr_str.h \ diff --git a/include/osmocom/core/tun.h b/include/osmocom/core/tun.h new file mode 100644 index 0000000..8f233c4 --- /dev/null +++ b/include/osmocom/core/tun.h @@ -0,0 +1,38 @@ +/*! \file tun.h + * tunnel network device convenience functions. */ + +#pragma once +#if (!EMBEDDED) + +#include <stddef.h> +#include <stdint.h> + +#include <osmocom/core/msgb.h> + +struct osmo_tundev; + +/* callback user gets ownership of the msgb and is expected to free it. */ +typedef int (*osmo_tundev_data_ind_cb_t)(struct osmo_tundev *tun, struct msgb *msg); + +struct osmo_tundev *osmo_tundev_alloc(void *ctx, const char *name); +void osmo_tundev_free(struct osmo_tundev *tundev); +int osmo_tundev_open(struct osmo_tundev *tundev); +int osmo_tundev_close(struct osmo_tundev *tundev); +bool osmo_tundev_is_open(struct osmo_tundev *tundev); + +void osmo_tundev_set_priv_data(struct osmo_tundev *tundev, void *priv_data); +void *osmo_tundev_get_priv_data(struct osmo_tundev *tundev); + +void osmo_tundev_set_data_ind_cb(struct osmo_tundev *tundev, osmo_tundev_data_ind_cb_t data_ind_cb); + +const char *osmo_tundev_get_name(const struct osmo_tundev *tundev); +int osmo_tundev_set_dev_name(struct osmo_tundev *tundev, const char *dev_name); +const char *osmo_tundev_get_dev_name(const struct osmo_tundev *tundev); + +int osmo_tundev_set_netns_name(struct osmo_tundev *tundev, const char *netns); +const char *osmo_tundev_get_netns_name(const struct osmo_tundev *tundev); + +int osmo_tundev_encaps(struct osmo_tundev *tundev, struct msgb *msg); + +#endif /* (!EMBEDDED) */ +/*! @} */ diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 04201a8..e942a49 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -71,6 +71,7 @@ timer.c \ timer_gettimeofday.c \ timer_clockgettime.c \ + tun.c \ use_count.c \ utils.c \ write_queue.c \ diff --git a/src/core/tun.c b/src/core/tun.c new file mode 100644 index 0000000..23055ed --- /dev/null +++ b/src/core/tun.c @@ -0,0 +1,434 @@ + +/* TUN interface functions. + * (C) 2023 by sysmocom - s.m.f.c. GmbH <info(a)sysmocom.de> + * + * All Rights Reserved + * + * SPDX-License-Identifier: GPL-2.0+ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#include "config.h" + +/*! \addtogroup tun + * @{ + * tun network device (interface) convenience functions + * + * \file tundev.c */ + +#include <asm-generic/errno-base.h> +#include <asm-generic/errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <errno.h> +#include <ifaddrs.h> +#include <sys/types.h> +#include <sys/time.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/time.h> +#include <net/if.h> +#include <net/route.h> + +#if defined(__linux__) +#include <linux/if_tun.h> +#else +#error "Unknown platform!" +#endif + +#include <osmocom/core/utils.h> +#include <osmocom/core/select.h> +#include <osmocom/core/linuxlist.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/write_queue.h> +#include <osmocom/core/socket.h> +#include <osmocom/core/netns.h> +#include <osmocom/core/tun.h> + +#define TUN_DEV_PATH "/dev/net/tun" +#define TUN_PACKET_MAX 8196 + +#define LOGTUN(tun, lvl, fmt, args ...) \ + LOGP(DLGLOBAL, lvl, "TUN(%s): " fmt, (tun)->name, ## args) + +struct osmo_tundev { + /* Name used to identify the osmo_tundev */ + char *name; + + /* Network interface name to use when setting up the tun device. + *NULL = let the system pick one. */ + char *dev_name; + + /* Write queue used since tun fd is set non-blocking */ + struct osmo_wqueue wqueue; + + /* netns name where the tun interface is created (NULL = default netns) */ + char *netns_name; + /* FD to the netns with name "netns_name" above */ + int netns_fd; + + /* API user private data */ + void *priv_data; + + /* Called bu tundev each time a new packet is received on the tun interface. Can be NULL. */ + osmo_tundev_data_ind_cb_t data_ind_cb; + + /* Whether the tundev is in opened state (managing the tun interface) */ + bool opened; +}; + +/* A new pkt arrived from the tun device, dispatch it to the API user */ +static int tundev_decaps(struct osmo_tundev *tundev) +{ + struct msgb *msg; + int status; + + msg = msgb_alloc(TUN_PACKET_MAX, "tundev_rx"); + + if ((status = read(tundev->wqueue.bfd.fd, msgb_data(msg), TUN_PACKET_MAX)) <= 0) { + LOGTUN(tundev, LOGL_ERROR, "read() failed: %s (%d)\n", strerror(errno), errno); + msgb_free(msg); + return -1; + } + msgb_put(msg, status); + + if (tundev->data_ind_cb) + return tundev->data_ind_cb(tundev, msg); + else + msgb_free(msg); + + return 0; +} + +/* callback for tun device osmocom select loop integration */ +static int tundev_read_cb(struct osmo_fd *fd) +{ + struct osmo_tundev *tundev = fd->data; + return tundev_decaps(tundev); +} + +/* callback for tun device osmocom select loop integration */ +static int tundev_write_cb(struct osmo_fd *fd, struct msgb *msg) +{ + struct osmo_tundev *tundev = fd->data; + size_t pkt_len = msgb_length(msg); + + int rc; + rc = write(tundev->wqueue.bfd.fd, msgb_data(msg), pkt_len); + if (rc < 0) { + LOGTUN(tundev, LOGL_ERROR, "write() failed: %s (%d)\n", strerror(errno), errno); + } else if (rc < pkt_len) { + LOGTUN(tundev, LOGL_ERROR, "short write() %d < %zu\n", rc, pkt_len); + } + return rc; +} + +/*! Allocate a new tundev object. + * \param[in] ctx talloc context to use as a parent when allocating the tundev object + * \param[in] name A name providen to identify the tundev object + * \returns newly allocated tundev object on success; NULL on error + */ +struct osmo_tundev *osmo_tundev_alloc(void *ctx, const char *name) +{ + struct osmo_tundev *tundev; + tundev = talloc_zero(ctx, struct osmo_tundev); + if (!tundev) + return NULL; + + tundev->name = talloc_strdup(tundev, name); + tundev->netns_fd = -1; + osmo_wqueue_init(&tundev->wqueue, 1000); + osmo_fd_setup(&tundev->wqueue.bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, tundev, 0); + tundev->wqueue.read_cb = tundev_read_cb; + tundev->wqueue.write_cb = tundev_write_cb; + return tundev; +} + +/*! Free an allocated tundev object. + * \param[in] tundev The tundev object to free + */ +void osmo_tundev_free(struct osmo_tundev *tundev) +{ + if (!tundev) + return; + osmo_tundev_close(tundev); + talloc_free(tundev); +} + +/*! Open and configure fd of the tunnel device. + * \param[in] tundev The tundev object to free + * \param[in] flags internal linux flags to pass when creating the device (not used yet) + * \returns 0 on success; negative on error + */ +static int tundev_open_fd(struct osmo_tundev *tundev, int flags) +{ + struct ifreq ifr; + int fd, rc; + + fd = open(TUN_DEV_PATH, O_RDWR); + if (fd < 0) { + LOGTUN(tundev, LOGL_ERROR, "Cannot open " TUN_DEV_PATH ": %s\n", strerror(errno)); + return fd; + } + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TUN | IFF_NO_PI | flags; + if (tundev->dev_name) { + /* if a TUN interface name was specified, put it in the structure; otherwise, + the kernel will try to allocate the "next" device of the specified type */ + osmo_strlcpy(ifr.ifr_name, tundev->dev_name, IFNAMSIZ); + } + + /* try to create the device */ + rc = ioctl(fd, TUNSETIFF, (void *) &ifr); + if (rc < 0) { + close(fd); + return rc; + } + + /* Read name back from device */ + if (!tundev->dev_name) { + ifr.ifr_name[IFNAMSIZ - 1] = '\0'; + tundev->dev_name = talloc_strdup(tundev, ifr.ifr_name); + } + + /* set non-blocking: */ + rc = fcntl(fd, F_GETFL); + if (rc < 0) { + LOGTUN(tundev, LOGL_ERROR, "fcntl(F_GETFL) failed: %s (%d)\n", + strerror(errno), errno); + return rc; + } + rc = fcntl(fd, F_SETFL, rc | O_NONBLOCK); + if (rc < 0) { + LOGTUN(tundev, LOGL_ERROR, "fcntl(F_SETFL, O_NONBLOCK) failed: %s (%d)\n", + strerror(errno), errno); + return rc; + } + + /* FIXME: SIOCSIFTXQLEN / SIOCSIFFLAGS */ + + return fd; +} + +/*! Open the tunnel device owned by the tundev object. + * \param[in] tundev The tundev object to open + * \returns 0 on success; negative on error + */ +int osmo_tundev_open(struct osmo_tundev *tundev) +{ + struct osmo_netns_switch_state switch_state; + int rc; + + if (tundev->netns_name) { + tundev->netns_fd = osmo_netns_open_fd(tundev->netns_name); + if (tundev->netns_fd < 0) { + LOGTUN(tundev, LOGL_ERROR, "Cannot obtain netns file descriptor: %s (%d)\n", + strerror(errno), errno); + return tundev->netns_fd; + } + } + + /* temporarily switch to specified namespace to create tun device */ + if (tundev->netns_name) { + LOGTUN(tundev, LOGL_INFO, "Switch to netns '%s'\n", + tundev->netns_name); + rc = osmo_netns_switch_enter(tundev->netns_fd, &switch_state); + if (rc < 0) { + LOGTUN(tundev, LOGL_ERROR, "Cannot switch to netns '%s': %s (%d)\n", + tundev->netns_name, strerror(errno), errno); + goto err_close_ns; + } + } + + tundev->wqueue.bfd.fd = tundev_open_fd(tundev, 0); + if (tundev->wqueue.bfd.fd < 0) { + LOGTUN(tundev, LOGL_ERROR, "Cannot open TUN device: %s\n", strerror(errno)); + rc = -ENODEV; + goto err_restore_ns; + } + + /* switch back to default namespace */ + if (tundev->netns_name) { + rc = osmo_netns_switch_exit(&switch_state); + if (rc < 0) { + LOGTUN(tundev, LOGL_ERROR, "Cannot switch back from netns '%s': %s\n", + tundev->netns_name, strerror(errno)); + goto err_close_tun; + } + LOGTUN(tundev, LOGL_INFO, "Back from netns '%s'\n", + tundev->netns_name); + } + + osmo_fd_register(&tundev->wqueue.bfd); + tundev->opened = true; + return 0; + +err_close_tun: + close(tundev->wqueue.bfd.fd); + tundev->wqueue.bfd.fd = -1; +err_restore_ns: + osmo_netns_switch_exit(&switch_state); +err_close_ns: + if (tundev->netns_name) { + close(tundev->netns_fd); + tundev->netns_fd = -1; + } + return rc; +} + +/*! Close the tunnel device owned by the tundev object. + * \param[in] tundev The tundev object to close + * \returns 0 on success; negative on error + */ +int osmo_tundev_close(struct osmo_tundev *tundev) +{ + if (!tundev->opened) + return -EALREADY; + + osmo_wqueue_clear(&tundev->wqueue); + + if (tundev->netns_fd != -1) { + close(tundev->netns_fd); + tundev->netns_fd = -1; + } + if (tundev->wqueue.bfd.fd != -1) { + osmo_fd_unregister(&tundev->wqueue.bfd); + close(tundev->wqueue.bfd.fd); + tundev->wqueue.bfd.fd = -1; + } + tundev->opened = false; + return 0; +} + +/*! Retrieve whether the tundev object is in "opened" state. + * \param[in] tundev The tundev object to check + * \returns true if in state "opened"; false otherwise + */ +bool osmo_tundev_is_open(struct osmo_tundev *tundev) +{ + return tundev->opened; +} + +/*! Set private user data pointer on the tundev object. + * \param[in] tundev The tundev object where the field is set + */ +void osmo_tundev_set_priv_data(struct osmo_tundev *tundev, void *priv_data) +{ + tundev->priv_data = priv_data; +} + +/*! Get private user data pointer from the tundev object. + * \param[in] tundev The tundev object from where to retrieve the field + * \returns The current value of the priv_data field. + */ +void *osmo_tundev_get_priv_data(struct osmo_tundev *tundev) +{ + return tundev->priv_data; +} + +/*! Set data_ind_cb callback, called when a new packet is received on the tun interface. + * \param[in] tundev The tundev object where the field is set + * \param[in] data_ind_cb the user provided function to be called when a new packet is received + */ +void osmo_tundev_set_data_ind_cb(struct osmo_tundev *tundev, osmo_tundev_data_ind_cb_t data_ind_cb) +{ + tundev->data_ind_cb = data_ind_cb; +} + +/*! Get name used to identify the tundev object. + * \param[in] tundev The tundev object from where to retrieve the field + * \returns The current value of the name used to identify the tundev object + */ +const char *osmo_tundev_get_name(const struct osmo_tundev *tundev) +{ + return tundev->name; +} + +/*! Set name used to name the tunnel interface created by the tundev object. + * \param[in] tundev The tundev object where the field is set + * \param[in] dev_name The tunnel interface name to use + * \returns 0 on success; negative on error + * + * This is used during osmo_tundev_open() time, and hence shouldn't be changed + * when the tundev object is in "opened" state. + * If left as NULL (default), the system will pick a suitable name during + * osmo_tundev_open(), and the field will be updated to the system-selected + * name, which can be retrieved later with osmo_tundev_get_dev_name(). + */ +int osmo_tundev_set_dev_name(struct osmo_tundev *tundev, const char *dev_name) +{ + if (tundev->opened) + return -EALREADY; + osmo_talloc_replace_string(tundev, &tundev->dev_name, dev_name); + return 0; +} + +/*! Get name used to name the tunnel interface created by the tundev object + * \param[in] tundev The tundev object from where to retrieve the field + * \returns The current value of the configured tunnel interface name to use + */ +const char *osmo_tundev_get_dev_name(const struct osmo_tundev *tundev) +{ + return tundev->dev_name; +} + +/*! Set name of the network namespace to use when opening the tunnel interface + * \param[in] tundev The tundev object where the field is set + * \param[in] netns_name The network namespace to use during tunnel interface creation + * \returns 0 on success; negative on error + * + * This is used during osmo_tundev_open() time, and hence shouldn't be changed + * when the tundev object is in "opened" state. + * If left as NULL (default), the system will stay in the current network namespace. + */ +int osmo_tundev_set_netns_name(struct osmo_tundev *tundev, const char *netns_name) +{ + if (tundev->opened) + return -EALREADY; + osmo_talloc_replace_string(tundev, &tundev->netns_name, netns_name); + return 0; +} + +/*! Get name of network namespace used whien opening the tunnel interface + * \param[in] tundev The tundev object from where to retrieve the field + * \returns The current value of the configured network namespace + */ +const char *osmo_tundev_get_netns_name(const struct osmo_tundev *tundev) +{ + return tundev->netns_name; +} + +/*! Submit a packet to the tunnel device managed by the tundev object + * \param[in] tundev The tundev object owning the tunnel device where to inject the packet + * \param[in] msg The msgb containg the packet to transfer + * \returns The current value of the configured network namespace + * + * This function takes the ownership of msg in all cases. + */ +int osmo_tundev_encaps(struct osmo_tundev *tundev, struct msgb *msg) +{ + int rc = osmo_wqueue_enqueue(&tundev->wqueue, msg); + if (rc < 0) { + LOGTUN(tundev, LOGL_ERROR, "Failed to enqueue the packet\n"); + msgb_free(msg); + return rc; + } + return rc; +} + +/*! @} */ -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/31025 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I3463271666df1e85746fb7b06ec45a17024b8c53 Gerrit-Change-Number: 31025 Gerrit-PatchSet: 1 Gerrit-Owner: pespin <pespin(a)sysmocom.de> Gerrit-MessageType: newchange
1 0
0 0
Change in libosmo-abis[master]: Send raw data with mISDN as it arrives from application
by laforge 19 Jan '23

19 Jan '23
laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30450 ) Change subject: Send raw data with mISDN as it arrives from application ...................................................................... Send raw data with mISDN as it arrives from application The application is responsible to send raw data in sync with the raw data it receives. It also cares about sending data in advance, to prevent underruns, if required Change-Id: Ib311331fbbeff3661f6745e3474918987a8aa460 --- M src/input/misdn.c 1 file changed, 17 insertions(+), 14 deletions(-) Approvals: Jenkins Builder: Verified laforge: Looks good to me, approved; Verified pespin: Looks good to me, but someone else must approve diff --git a/src/input/misdn.c b/src/input/misdn.c index 0925cbf..454c8e8 100644 --- a/src/input/misdn.c +++ b/src/input/misdn.c @@ -392,27 +392,26 @@ } /* write to a raw channel TS */ -static int handle_ts_raw_write(struct osmo_fd *bfd, unsigned int len) +static int handle_ts_raw_write(struct osmo_fd *bfd) { struct e1inp_line *line = bfd->data; + struct misdn_line *mline = line->driver_data; unsigned int ts_nr = bfd->priv_nr; struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; struct msgb *msg; struct mISDNhead *hh; int ret; + osmo_fd_write_disable(bfd); + + if (mline->unconfirmed_ts[ts_nr-1]) + return 0; + /* get the next msg for this timeslot */ msg = e1inp_tx_ts(e1i_ts, NULL); if (!msg) return 0; - if (msg->len != len) { - /* This might lead to a transmit underrun, as we call tx - * from the rx path, as there's no select/poll on dahdi - * */ - LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unexpected msg->len = %u, expected %u\n", msg->len, len); - } - LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN TX: %s\n", osmo_hexdump(msg->data, msg->len)); hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); @@ -421,15 +420,19 @@ ret = write(bfd->fd, msg->data, msg->len); if (ret < msg->len) - LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "send returns %d instead of %d\n", ret, msg->len); + LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "send returns %d instead of %d\n", ret, msg->len); + else + mline->unconfirmed_ts[ts_nr-1] = 1; msgb_free(msg); + return ret; } static int handle_ts_raw_read(struct osmo_fd *bfd) { struct e1inp_line *line = bfd->data; + struct misdn_line *mline = line->driver_data; unsigned int ts_nr = bfd->priv_nr; struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE, "mISDN TS RAW"); @@ -459,11 +462,12 @@ msg->l2h = msgb_pull(msg, MISDN_HEADER_LEN); LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN RX: %s\n", osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); - /* the number of bytes received indicates that data to send */ - handle_ts_raw_write(bfd, msgb_l2len(msg)); return e1inp_rx_ts(e1i_ts, msg, 0, 0); case PH_ACTIVATE_IND: + break; case PH_DATA_CNF: + mline->unconfirmed_ts[ts_nr-1] = 0; + osmo_fd_write_enable(bfd); break; default: break; @@ -588,9 +592,8 @@ case E1INP_TS_TYPE_RAW: if (what & OSMO_FD_READ) rc = handle_ts_raw_read(bfd); - /* We never include the mISDN B-Channel FD into the - * writeset, since it doesn't support poll() based - * write flow control */ + if (what & OSMO_FD_WRITE) + rc = handle_ts_raw_write(bfd); break; case E1INP_TS_TYPE_HDLC: if (what & OSMO_FD_READ) -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30450 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: Ib311331fbbeff3661f6745e3474918987a8aa460 Gerrit-Change-Number: 30450 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-MessageType: merged
1 0
0 0
Change in libosmo-abis[master]: Send raw data with mISDN as it arrives from application
by laforge 19 Jan '23

19 Jan '23
Attention is currently required from: jolly, pespin. laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30450 ) Change subject: Send raw data with mISDN as it arrives from application ...................................................................... Patch Set 2: Verified+1 -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30450 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: Ib311331fbbeff3661f6745e3474918987a8aa460 Gerrit-Change-Number: 30450 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: jolly <andreas(a)eversberg.eu> Gerrit-Attention: pespin <pespin(a)sysmocom.de> Gerrit-Comment-Date: Thu, 19 Jan 2023 19:17:38 +0000 Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
1 0
0 0
Change in libosmo-abis[master]: Initialize devinfo at misdn.c
by laforge 19 Jan '23

19 Jan '23
laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30449 ) Change subject: Initialize devinfo at misdn.c ...................................................................... Initialize devinfo at misdn.c Valgrind did complain about use of uninitialized data. Change-Id: I3a60eee96366561acac9ccfc92eb0d6ca828b7b9 --- M src/input/misdn.c 1 file changed, 1 insertion(+), 0 deletions(-) Approvals: Jenkins Builder: Verified laforge: Looks good to me, approved pespin: Looks good to me, but someone else must approve diff --git a/src/input/misdn.c b/src/input/misdn.c index 5d47a79..0925cbf 100644 --- a/src/input/misdn.c +++ b/src/input/misdn.c @@ -822,6 +822,7 @@ //LOGPIL(line, DLMI, LOGL_DEBUG, "%d device%s found\n", cnt, (cnt==1)?"":"s"); printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); #if 1 + memset(&devinfo, 0, sizeof(devinfo)); devinfo.id = line->port_nr; ret = ioctl(sk, IMGETDEVINFO, &devinfo); if (ret < 0) { 1 is the latest approved patch-set. No files were changed between the latest approved patch-set and the submitted one. -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30449 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: I3a60eee96366561acac9ccfc92eb0d6ca828b7b9 Gerrit-Change-Number: 30449 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-MessageType: merged
1 0
0 0
Change in libosmo-abis[master]: Remove mISDN header from received channel data
by laforge 19 Jan '23

19 Jan '23
laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30448 ) Change subject: Remove mISDN header from received channel data ...................................................................... Remove mISDN header from received channel data Change-Id: I37f12baa1013cd1cfc6f6531b132669071eec926 --- M src/input/misdn.c 1 file changed, 10 insertions(+), 6 deletions(-) Approvals: Jenkins Builder: Verified pespin: Looks good to me, but someone else must approve laforge: Looks good to me, approved diff --git a/src/input/misdn.c b/src/input/misdn.c index 5f6dd62..5d47a79 100644 --- a/src/input/misdn.c +++ b/src/input/misdn.c @@ -372,9 +372,10 @@ switch (hh->prim) { case PH_DATA_IND: - msg->l2h = msg->data + MISDN_HEADER_LEN; + /* remove the Misdn Header */ + msg->l2h = msgb_pull(msg, MISDN_HEADER_LEN); LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "BCHAN RX: %s\n", - osmo_hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN)); + osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); /* the number of bytes received indicates that data to send */ handle_tsX_write(bfd, msgb_l2len(msg)); return e1inp_rx_ts(e1i_ts, msg, 0, 0); @@ -454,9 +455,10 @@ switch (hh->prim) { case PH_DATA_IND: - msg->l2h = msg->data + MISDN_HEADER_LEN; + /* remove the Misdn Header */ + msg->l2h = msgb_pull(msg, MISDN_HEADER_LEN); LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "RAW CHAN RX: %s\n", - osmo_hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN)); + osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); /* the number of bytes received indicates that data to send */ handle_ts_raw_write(bfd, msgb_l2len(msg)); return e1inp_rx_ts(e1i_ts, msg, 0, 0); @@ -539,9 +541,11 @@ switch (hh->prim) { case PH_DATA_IND: - msg->l2h = msg->data + MISDN_HEADER_LEN; + /* remove the Misdn Header */ + msgb_pull(msg, MISDN_HEADER_LEN); + msg->l2h = msg->data; LOGPITS(e1i_ts, DLMIB, LOGL_DEBUG, "HDLC CHAN RX: %s\n", - osmo_hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN)); + osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); return e1inp_rx_ts(e1i_ts, msg, 0, 0); case PH_ACTIVATE_IND: break; -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30448 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: I37f12baa1013cd1cfc6f6531b132669071eec926 Gerrit-Change-Number: 30448 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-MessageType: merged
1 0
0 0
Change in libosmo-abis[master]: Remove mISDN header from received channel data
by laforge 19 Jan '23

19 Jan '23
Attention is currently required from: jolly. laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30448 ) Change subject: Remove mISDN header from received channel data ...................................................................... Patch Set 2: Code-Review+2 -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30448 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: I37f12baa1013cd1cfc6f6531b132669071eec926 Gerrit-Change-Number: 30448 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: jolly <andreas(a)eversberg.eu> Gerrit-Comment-Date: Thu, 19 Jan 2023 19:17:07 +0000 Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
1 0
0 0
Change in libosmo-abis[master]: Initialize devinfo at misdn.c
by jolly 19 Jan '23

19 Jan '23
jolly has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30449 ) Change subject: Initialize devinfo at misdn.c ...................................................................... Patch Set 2: (1 comment) Patchset: PS2: Looks good to me. -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30449 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: I3a60eee96366561acac9ccfc92eb0d6ca828b7b9 Gerrit-Change-Number: 30449 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Comment-Date: Thu, 19 Jan 2023 19:09:47 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No Gerrit-MessageType: comment
1 0
0 0
Change in libosmo-abis[master]: Send raw data with mISDN as it arrives from application
by jolly 19 Jan '23

19 Jan '23
Attention is currently required from: pespin. jolly has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30450 ) Change subject: Send raw data with mISDN as it arrives from application ...................................................................... Patch Set 2: (1 comment) File src/input/misdn.c: https://gerrit.osmocom.org/c/libosmo-abis/+/30450/comment/220afa56_2e94a509 PS2, Line 405: osmo_fd_write_disable(bfd); > Write must be disabled in all cases. […] Done -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30450 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: Ib311331fbbeff3661f6745e3474918987a8aa460 Gerrit-Change-Number: 30450 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: pespin <pespin(a)sysmocom.de> Gerrit-Comment-Date: Thu, 19 Jan 2023 19:08:06 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: jolly <andreas(a)eversberg.eu> Comment-In-Reply-To: pespin <pespin(a)sysmocom.de> Gerrit-MessageType: comment
1 0
0 0
Change in libosmo-abis[master]: Remove mISDN header from received channel data
by jolly 19 Jan '23

19 Jan '23
Attention is currently required from: laforge. jolly has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-abis/+/30448 ) Change subject: Remove mISDN header from received channel data ...................................................................... Patch Set 2: (2 comments) File src/input/misdn.c: https://gerrit.osmocom.org/c/libosmo-abis/+/30448/comment/a1f2ff52_a1b32ab1 PS1, Line 459: msgb_pull(msg, MISDN_HEADER_LEN); > same here Done File src/input/misdn.c: https://gerrit.osmocom.org/c/libosmo-abis/+/30448/comment/de258335_a8620e88 PS2, Line 545: msgb_pull(msg, MISDN_HEADER_LEN); : msg->l2h = msg->data; > so tsx_read and ts_raw_read now use msg->l2h = msgb_pull... but here it's still separate. […] Done -- To view, visit https://gerrit.osmocom.org/c/libosmo-abis/+/30448 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmo-abis Gerrit-Branch: master Gerrit-Change-Id: I37f12baa1013cd1cfc6f6531b132669071eec926 Gerrit-Change-Number: 30448 Gerrit-PatchSet: 2 Gerrit-Owner: jolly <andreas(a)eversberg.eu> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge <laforge(a)osmocom.org> Gerrit-Reviewer: pespin <pespin(a)sysmocom.de> Gerrit-Attention: laforge <laforge(a)osmocom.org> Gerrit-Comment-Date: Thu, 19 Jan 2023 19:04:46 +0000 Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: laforge <laforge(a)osmocom.org> Gerrit-MessageType: comment
1 0
0 0
  • ← Newer
  • 1
  • ...
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • ...
  • 221
  • Older →

HyperKitty Powered by HyperKitty version 1.3.12.