daniel has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/30936 )
Change subject: gpsr_ns2_udp: Use osmo_io_fd instead of osmo_fd ......................................................................
gpsr_ns2_udp: Use osmo_io_fd instead of osmo_fd
Change-Id: Id776d2d9f35c304620f3d5b94492148dd987f5a0 --- M src/gb/gprs_ns2_udp.c 1 file changed, 31 insertions(+), 72 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/36/30936/1
diff --git a/src/gb/gprs_ns2_udp.c b/src/gb/gprs_ns2_udp.c index 4816021..faeece4 100644 --- a/src/gb/gprs_ns2_udp.c +++ b/src/gb/gprs_ns2_udp.c @@ -35,6 +35,7 @@
#include "common_vty.h" #include "gprs_ns2_internal.h" +#include "osmocom/core/osmo_io.h"
static void free_bind(struct gprs_ns2_vc_bind *bind); @@ -46,7 +47,7 @@ };
struct priv_bind { - struct osmo_fd fd; + struct osmo_io_fd *fd; struct osmo_sockaddr addr; int dscp; uint8_t priority; @@ -68,7 +69,8 @@
priv = bind->priv;
- osmo_fd_close(&priv->fd); + if (priv->fd) + osmo_iofd_close(priv->fd); talloc_free(priv); }
@@ -148,10 +150,7 @@ int rc; struct priv_bind *priv = bind->priv;
- rc = sendto(priv->fd.fd, msg->data, msg->len, 0, - &dest->u.sa, sizeof(*dest)); - - msgb_free(msg); + rc = osmo_iofd_sendto_msgb(priv->fd, msg, dest);
return rc; } @@ -171,39 +170,6 @@ return rc; }
-/* Read a single NS-over-IP message */ -static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error, struct osmo_sockaddr *saddr, - const struct gprs_ns2_vc_bind *bind) -{ - struct msgb *msg = ns2_msgb_alloc(); - int ret = 0; - socklen_t saddr_len = sizeof(*saddr); - - if (!msg) { - *error = -ENOMEM; - return NULL; - } - - ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0, - &saddr->u.sa, &saddr_len); - if (ret < 0) { - LOGBIND(bind, LOGL_ERROR, "recv error %s during NSIP recvfrom %s\n", - strerror(errno), osmo_sock_get_name2(bfd->fd)); - msgb_free(msg); - *error = ret; - return NULL; - } else if (ret == 0) { - msgb_free(msg); - *error = ret; - return NULL; - } - - msg->l2h = msg->data; - msgb_put(msg, ret); - - return msg; -} - static struct priv_vc *ns2_driver_alloc_vc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_vc *nsvc, struct osmo_sockaddr *remote) { struct priv_vc *priv = talloc_zero(bind, struct priv_vc); @@ -216,24 +182,22 @@ return priv; }
-static int handle_nsip_read(struct osmo_fd *bfd) +static void handle_nsip_recvmsg(struct osmo_io_fd * iofd, int error, struct msgb *msg, + struct osmo_sockaddr *saddr) { int rc = 0; - int error = 0; - struct gprs_ns2_vc_bind *bind = bfd->data; - struct osmo_sockaddr saddr; + struct gprs_ns2_vc_bind *bind = osmo_iofd_get_data(iofd); struct gprs_ns2_vc *nsvc; - struct msgb *msg = read_nsip_msg(bfd, &error, &saddr, bind); + struct msgb *reject;
- if (!msg) - return -EINVAL; + msg->l2h = msgb_data(msg);
/* check if a vc is available */ - nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, &saddr); + nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, saddr); if (!nsvc) { /* VC not found */ - rc = ns2_create_vc(bind, msg, &saddr, "newconnection", &reject, &nsvc); + rc = ns2_create_vc(bind, msg, saddr, "newconnection", &reject, &nsvc); switch (rc) { case NS2_CS_FOUND: break; @@ -243,10 +207,10 @@ goto out; case NS2_CS_REJECTED: /* nsip_sendmsg will free reject */ - rc = nsip_sendmsg(bind, reject, &saddr); + rc = nsip_sendmsg(bind, reject, saddr); goto out; case NS2_CS_CREATED: - ns2_driver_alloc_vc(bind, nsvc, &saddr); + ns2_driver_alloc_vc(bind, nsvc, saddr); /* only start the fsm for non SNS. SNS will take care of its own */ if (nsvc->nse->dialect != GPRS_NS2_DIALECT_SNS) ns2_vc_fsm_start(nsvc); @@ -254,29 +218,15 @@ } }
- return ns2_recv_vc(nsvc, msg); + ns2_recv_vc(nsvc, msg); + return;
out: msgb_free(msg); - return rc; }
-static int handle_nsip_write(struct osmo_fd *bfd) +static void handle_nsip_sendmsg(struct osmo_io_fd *iofd, int res, struct msgb *msg) { - /* FIXME: actually send the data here instead of nsip_sendmsg() */ - return -EIO; -} - -static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what) -{ - int rc = 0; - - if (what & OSMO_FD_READ) - rc = handle_nsip_read(bfd); - if (what & OSMO_FD_WRITE) - rc = handle_nsip_write(bfd); - - return rc; }
/*! Find NS bind for a given socket address @@ -321,6 +271,11 @@ struct priv_bind *priv; int rc;
+ struct osmo_io_ops ioops = { + .sendmsg_cb = &handle_nsip_sendmsg, + .recvmsg_cb = &handle_nsip_recvmsg, + }; + if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) return -EINVAL;
@@ -353,12 +308,11 @@ gprs_ns2_free_bind(bind); return -ENOMEM; } - priv->fd.cb = nsip_fd_cb; - priv->fd.data = bind; + priv->addr = *local; priv->dscp = dscp;
- rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_DGRAM, IPPROTO_UDP, + rc = osmo_sock_init_osa(SOCK_DGRAM, IPPROTO_UDP, local, NULL, OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(priv->dscp)); if (rc < 0) { @@ -366,6 +320,11 @@ return rc; }
+ priv->fd = osmo_iofd_setup(bind, 4096, 128, rc, "NS bind", + OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops, bind, 0); + + osmo_iofd_read_enable(priv->fd); + /* IPv4: max fragmented payload can be (13 bit) * 8 byte => 65535. * IPv6: max payload can be 65535 (RFC 2460). * UDP header = 8 byte */ @@ -521,7 +480,7 @@ if (dscp != priv->dscp) { priv->dscp = dscp;
- rc = osmo_sock_set_dscp(priv->fd.fd, dscp); + rc = osmo_sock_set_dscp(osmo_iofd_get_fd(priv->fd), dscp); if (rc < 0) { LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %u with ret(%d) errno(%d)\n", dscp, rc, errno); @@ -543,7 +502,7 @@ if (priority != priv->priority) { priv->priority = priority;
- rc = osmo_sock_set_priority(priv->fd.fd, priority); + rc = osmo_sock_set_priority(osmo_iofd_get_fd(priv->fd), priority); if (rc < 0) { LOGBIND(bind, LOGL_ERROR, "Failed to set the priority to %u with ret(%d) errno(%d)\n", priority, rc, errno);