blob: b5b7ac815bca14f1ba98bd5e84ba0efde19ab4b9 [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{
57 struct gtp_daemon *d = talloc_zero(ctx, struct gtp_daemon);
58 if (!d)
59 return NULL;
60
61 INIT_LLIST_HEAD(&d->gtp_endpoints);
62 INIT_LLIST_HEAD(&d->tun_devices);
63 INIT_LLIST_HEAD(&d->gtp_tunnels);
64 INIT_LLIST_HEAD(&d->subprocesses);
65 pthread_rwlock_init(&d->rwlock, NULL);
66 d->main_thread = pthread_self();
67
68 d->itq = osmo_it_q_alloc(d, "itq", 4096, gtp_daemon_itq_read_cb, d);
69 osmo_fd_register(&d->itq->event_ofd);
70
71 INIT_LLIST_HEAD(&d->cups_clients);
72
73 d->cfg.cups_local_ip = talloc_strdup(d, "localhost");
74 d->cfg.cups_local_port = UECUPS_SCTP_PORT;
75
76 return d;
77}