blob: 31ae39f6a76fb34a3beaf08a953e60cfc8069721 [file] [log] [blame]
Pau Espin Pedrolf9f5b302022-04-12 12:34:25 +02001/* SPDX-License-Identifier: GPL-2.0 */
2#include <unistd.h>
3#include <stdint.h>
4#include <stdbool.h>
5#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
8#include <assert.h>
9#include <errno.h>
10
11#include <pthread.h>
12
13#include <osmocom/core/linuxlist.h>
14#include <osmocom/core/talloc.h>
15#include <osmocom/core/logging.h>
16
17#include "internal.h"
18
19
20/***********************************************************************
21 * GTP Daemon
22 ***********************************************************************/
23
24#ifndef OSMO_VTY_PORT_UECUPS
25#define OSMO_VTY_PORT_UECUPS 4268
26#endif
27
28struct gtp_daemon *g_daemon;
29
30static void gtp_daemon_itq_read_cb(struct osmo_it_q *q, struct llist_head *item)
31{
32 struct gtp_daemon *d = (struct gtp_daemon *)q->data;
33 struct gtp_daemon_itq_msg *itq_msg = container_of(item, struct gtp_daemon_itq_msg, list);
34
35 LOGP(DTUN, LOGL_DEBUG, "Rx new itq message from %s\n",
36 itq_msg->tun_released.tun->devname);
37
38 _tun_device_destroy(itq_msg->tun_released.tun);
39 if (d->reset_all_state_tun_remaining > 0) {
40 d->reset_all_state_tun_remaining--;
41 if (d->reset_all_state_tun_remaining == 0) {
42 struct cups_client *cc;
43 llist_for_each_entry(cc, &d->cups_clients, list) {
44 json_t *jres;
45 if (!cc->reset_all_state_res_pending)
46 continue;
47 cc->reset_all_state_res_pending = false;
48 jres = gen_uecups_result("reset_all_state_res", "OK");
49 cups_client_tx_json(cc, jres);
50 }
51 }
52 }
53}
54
55struct gtp_daemon *gtp_daemon_alloc(void *ctx)
56{
Harald Welte3f2361e2023-07-18 14:33:22 +020057 int rc;
Pau Espin Pedrolf9f5b302022-04-12 12:34:25 +020058 struct gtp_daemon *d = talloc_zero(ctx, struct gtp_daemon);
59 if (!d)
60 return NULL;
61
62 INIT_LLIST_HEAD(&d->gtp_endpoints);
63 INIT_LLIST_HEAD(&d->tun_devices);
64 INIT_LLIST_HEAD(&d->gtp_tunnels);
65 INIT_LLIST_HEAD(&d->subprocesses);
66 pthread_rwlock_init(&d->rwlock, NULL);
67 d->main_thread = pthread_self();
68
69 d->itq = osmo_it_q_alloc(d, "itq", 4096, gtp_daemon_itq_read_cb, d);
Harald Welte3f2361e2023-07-18 14:33:22 +020070 if (!d->itq)
71 goto out_free;
72
73 rc = osmo_fd_register(&d->itq->event_ofd);
74 if (rc < 0)
75 goto out_free_q;
Pau Espin Pedrolf9f5b302022-04-12 12:34:25 +020076
77 INIT_LLIST_HEAD(&d->cups_clients);
78
79 d->cfg.cups_local_ip = talloc_strdup(d, "localhost");
80 d->cfg.cups_local_port = UECUPS_SCTP_PORT;
81
82 return d;
Harald Welte3f2361e2023-07-18 14:33:22 +020083
84out_free_q:
85 osmo_it_q_destroy(d->itq);
86out_free:
87 talloc_free(d);
88 return NULL;
Pau Espin Pedrolf9f5b302022-04-12 12:34:25 +020089}