1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* MGMTD Frontend Server
*
* Copyright (C) 2021 Vmware, Inc.
* Pushpasis Sarkar <spushpasis@vmware.com>
*/
#include <zebra.h>
#include "network.h"
#include "libfrr.h"
#include "mgmtd/mgmt.h"
#include "mgmtd/mgmt_fe_server.h"
#include "mgmtd/mgmt_fe_adapter.h"
#ifdef REDIRECT_DEBUG_TO_STDERR
#define MGMTD_FE_SRVR_DBG(fmt, ...) \
fprintf(stderr, "%s: " fmt "\n", __func__, ##__VA_ARGS__)
#define MGMTD_FE_SRVR_ERR(fmt, ...) \
fprintf(stderr, "%s: ERROR, " fmt "\n", __func__, ##__VA_ARGS__)
#else /* REDIRECT_DEBUG_TO_STDERR */
#define MGMTD_FE_SRVR_DBG(fmt, ...) \
do { \
if (mgmt_debug_fe) \
zlog_debug("%s: " fmt, __func__, ##__VA_ARGS__); \
} while (0)
#define MGMTD_FE_SRVR_ERR(fmt, ...) \
zlog_err("%s: ERROR: " fmt, __func__, ##__VA_ARGS__)
#endif /* REDIRECT_DEBUG_TO_STDERR */
static int mgmt_fe_listen_fd = -1;
static struct thread_master *mgmt_fe_listen_tm;
static struct event *mgmt_fe_listen_ev;
static void mgmt_fe_server_register_event(enum mgmt_fe_event event);
static void mgmt_fe_conn_accept(struct event *thread)
{
int client_conn_fd;
union sockunion su;
if (mgmt_fe_listen_fd < 0)
return;
/* We continue hearing server listen socket. */
mgmt_fe_server_register_event(MGMTD_FE_SERVER);
memset(&su, 0, sizeof(union sockunion));
/* We can handle IPv4 or IPv6 socket. */
client_conn_fd = sockunion_accept(mgmt_fe_listen_fd, &su);
if (client_conn_fd < 0) {
MGMTD_FE_SRVR_ERR(
"Failed to accept MGMTD Frontend client connection : %s",
safe_strerror(errno));
return;
}
set_nonblocking(client_conn_fd);
set_cloexec(client_conn_fd);
MGMTD_FE_SRVR_DBG("Got a new MGMTD Frontend connection");
mgmt_fe_create_adapter(client_conn_fd, &su);
}
static void mgmt_fe_server_register_event(enum mgmt_fe_event event)
{
if (event == MGMTD_FE_SERVER) {
thread_add_read(mgmt_fe_listen_tm, mgmt_fe_conn_accept,
NULL, mgmt_fe_listen_fd,
&mgmt_fe_listen_ev);
assert(mgmt_fe_listen_ev);
} else {
assert(!"mgmt_fe_server_post_event() called incorrectly");
}
}
static void mgmt_fe_server_start(const char *hostname)
{
int ret;
int sock;
struct sockaddr_un addr;
mode_t old_mask;
/* Set umask */
old_mask = umask(0077);
sock = socket(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
if (sock < 0) {
MGMTD_FE_SRVR_ERR("Failed to create server socket: %s",
safe_strerror(errno));
goto mgmt_fe_server_start_failed;
}
addr.sun_family = AF_UNIX,
strlcpy(addr.sun_path, MGMTD_FE_SERVER_PATH, sizeof(addr.sun_path));
unlink(addr.sun_path);
ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) {
MGMTD_FE_SRVR_ERR(
"Failed to bind server socket to '%s'. Err: %s",
addr.sun_path, safe_strerror(errno));
goto mgmt_fe_server_start_failed;
}
ret = listen(sock, MGMTD_FE_MAX_CONN);
if (ret < 0) {
MGMTD_FE_SRVR_ERR("Failed to listen on server socket: %s",
safe_strerror(errno));
goto mgmt_fe_server_start_failed;
}
/* Restore umask */
umask(old_mask);
mgmt_fe_listen_fd = sock;
mgmt_fe_server_register_event(MGMTD_FE_SERVER);
MGMTD_FE_SRVR_DBG("Started MGMTD Frontend Server!");
return;
mgmt_fe_server_start_failed:
if (sock)
close(sock);
mgmt_fe_listen_fd = -1;
exit(-1);
}
int mgmt_fe_server_init(struct thread_master *master)
{
if (mgmt_fe_listen_tm) {
MGMTD_FE_SRVR_DBG("MGMTD Frontend Server already running!");
return 0;
}
mgmt_fe_listen_tm = master;
mgmt_fe_server_start("localhost");
return 0;
}
void mgmt_fe_server_destroy(void)
{
if (mgmt_fe_listen_tm) {
MGMTD_FE_SRVR_DBG("Closing MGMTD Frontend Server!");
if (mgmt_fe_listen_ev) {
THREAD_OFF(mgmt_fe_listen_ev);
mgmt_fe_listen_ev = NULL;
}
if (mgmt_fe_listen_fd >= 0) {
close(mgmt_fe_listen_fd);
mgmt_fe_listen_fd = -1;
}
mgmt_fe_listen_tm = NULL;
}
}
|