blob: 627915af1fe772e38cc8dff1c9ad1ed2a74fb895 [file] [log] [blame]
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +08001/* RF Ctl handling socket */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080020 *
21 */
22
23#include <openbsc/osmo_bsc_rf.h>
24#include <openbsc/debug.h>
25#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +080026#include <openbsc/signal.h>
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +010027#include <openbsc/osmo_msc_data.h>
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080028
29#include <osmocore/talloc.h>
30#include <osmocore/protocol/gsm_12_21.h>
31
32#include <sys/socket.h>
33#include <sys/un.h>
34
35#include <errno.h>
36#include <unistd.h>
37
38#define RF_CMD_QUERY '?'
39#define RF_CMD_OFF '0'
40#define RF_CMD_ON '1'
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010041#define RF_CMD_D_OFF 'd'
42#define RF_CMD_ON_G 'g'
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080043
44static int lock_each_trx(struct gsm_network *net, int lock)
45{
46 struct gsm_bts *bts;
47
48 llist_for_each_entry(bts, &net->bts_list, list) {
49 struct gsm_bts_trx *trx;
50 llist_for_each_entry(trx, &bts->trx_list, list) {
51 gsm_trx_lock_rf(trx, lock);
52 }
53 }
54
55 return 0;
56}
57
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010058static void send_resp(struct osmo_bsc_rf_conn *conn, char send)
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080059{
60 struct msgb *msg;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +080061
62 msg = msgb_alloc(10, "RF Query");
63 if (!msg) {
64 LOGP(DINP, LOGL_ERROR, "Failed to allocate response msg.\n");
65 return;
66 }
67
68 msg->l2h = msgb_put(msg, 1);
69 msg->l2h[0] = send;
70
71 if (write_queue_enqueue(&conn->queue, msg) != 0) {
72 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the answer.\n");
73 msgb_free(msg);
74 return;
75 }
76
77 return;
78}
79
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +010080
81/*
82 * Send a
83 * 'g' when we are in grace mode
84 * '1' when one TRX is online,
85 * '0' otherwise
86 */
87static void handle_query(struct osmo_bsc_rf_conn *conn)
88{
89 struct gsm_bts *bts;
90 char send = RF_CMD_OFF;
91
92 if (conn->rf->policy == S_RF_GRACE)
93 return send_resp(conn, RF_CMD_ON_G);
94
95 llist_for_each_entry(bts, &conn->rf->gsm_network->bts_list, list) {
96 struct gsm_bts_trx *trx;
97 llist_for_each_entry(trx, &bts->trx_list, list) {
98 if (trx->nm_state.availability == NM_AVSTATE_OK &&
99 trx->nm_state.operational != NM_STATE_LOCKED) {
100 send = RF_CMD_ON;
101 break;
102 }
103 }
104 }
105
106 send_resp(conn, send);
107}
108
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100109static void rf_check_cb(void *_data)
110{
111 struct gsm_bts *bts;
112 struct osmo_bsc_rf *rf = _data;
113
114 llist_for_each_entry(bts, &rf->gsm_network->bts_list, list) {
115 struct gsm_bts_trx *trx;
116
117 /* don't bother to check a booting or missing BTS */
118 if (!bts->oml_link || !is_ipaccess_bts(bts))
119 continue;
120
121 llist_for_each_entry(trx, &bts->trx_list, list) {
122 if (trx->nm_state.availability != NM_AVSTATE_OK ||
123 trx->nm_state.operational != NM_OPSTATE_ENABLED ||
124 trx->nm_state.administrative != NM_STATE_UNLOCKED) {
125 LOGP(DNM, LOGL_ERROR, "RF activation failed. Starting again.\n");
126 ipaccess_drop_oml(bts);
127 break;
128 }
129 }
130 }
131}
132
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100133static void send_signal(struct osmo_bsc_rf *rf, int val)
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800134{
135 struct rf_signal_data sig;
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100136 sig.net = rf->gsm_network;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800137
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100138 rf->policy = val;
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800139 dispatch_signal(SS_RF, val, &sig);
140}
141
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100142static int switch_rf_off(struct osmo_bsc_rf *rf)
143{
144 lock_each_trx(rf->gsm_network, 1);
145 send_signal(rf, S_RF_OFF);
146
147 return 0;
148}
149
150static void grace_timeout(void *_data)
151{
152 struct osmo_bsc_rf *rf = (struct osmo_bsc_rf *) _data;
153
154 LOGP(DINP, LOGL_NOTICE, "Grace timeout. Disabling the TRX.\n");
155 switch_rf_off(rf);
156}
157
158static int enter_grace(struct osmo_bsc_rf_conn *conn)
159{
160 struct osmo_bsc_rf *rf = conn->rf;
161
162 rf->grace_timeout.cb = grace_timeout;
163 rf->grace_timeout.data = rf;
164 bsc_schedule_timer(&rf->grace_timeout, rf->gsm_network->msc_data->mid_call_timeout, 0);
165 LOGP(DINP, LOGL_NOTICE, "Going to switch RF off in %d seconds.\n",
166 rf->gsm_network->msc_data->mid_call_timeout);
167
168 send_signal(conn->rf, S_RF_GRACE);
169 return 0;
170}
171
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800172static int rf_read_cmd(struct bsc_fd *fd)
173{
174 struct osmo_bsc_rf_conn *conn = fd->data;
175 char buf[1];
176 int rc;
177
178 rc = read(fd->fd, buf, sizeof(buf));
179 if (rc != sizeof(buf)) {
180 LOGP(DINP, LOGL_ERROR, "Short read %d/%s\n", errno, strerror(errno));
181 bsc_unregister_fd(fd);
182 close(fd->fd);
183 write_queue_clear(&conn->queue);
184 talloc_free(conn);
185 return -1;
186 }
187
188 switch (buf[0]) {
189 case RF_CMD_QUERY:
190 handle_query(conn);
191 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100192 case RF_CMD_D_OFF:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100193 conn->rf->last_state_command = "RF Direct Off";
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100194 bsc_del_timer(&conn->rf->rf_check);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100195 bsc_del_timer(&conn->rf->grace_timeout);
196 switch_rf_off(conn->rf);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800197 break;
198 case RF_CMD_ON:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100199 conn->rf->last_state_command = "RF Direct On";
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100200 bsc_del_timer(&conn->rf->grace_timeout);
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800201 lock_each_trx(conn->rf->gsm_network, 0);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100202 send_signal(conn->rf, S_RF_ON);
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100203 bsc_schedule_timer(&conn->rf->rf_check, 3, 0);
Holger Hans Peter Freyther12b917d2010-07-29 02:27:27 +0800204 break;
Holger Hans Peter Freythercf6f71a2010-11-22 19:32:13 +0100205 case RF_CMD_OFF:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100206 conn->rf->last_state_command = "RF Scheduled Off";
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100207 bsc_del_timer(&conn->rf->rf_check);
Holger Hans Peter Freyther70c232f2010-11-22 19:09:38 +0100208 enter_grace(conn);
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800209 break;
210 default:
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100211 conn->rf->last_state_command = "Unknown command";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800212 LOGP(DINP, LOGL_ERROR, "Unknown command %d\n", buf[0]);
213 break;
214 }
215
216 return 0;
217}
218
219static int rf_write_cmd(struct bsc_fd *fd, struct msgb *msg)
220{
221 int rc;
222
223 rc = write(fd->fd, msg->data, msg->len);
224 if (rc != msg->len) {
225 LOGP(DINP, LOGL_ERROR, "Short write %d/%s\n", errno, strerror(errno));
226 return -1;
227 }
228
229 return 0;
230}
231
232static int rf_ctl_accept(struct bsc_fd *bfd, unsigned int what)
233{
234 struct osmo_bsc_rf_conn *conn;
235 struct osmo_bsc_rf *rf = bfd->data;
236 struct sockaddr_un addr;
237 socklen_t len = sizeof(addr);
238 int fd;
239
240 fd = accept(bfd->fd, (struct sockaddr *) &addr, &len);
241 if (fd < 0) {
242 LOGP(DINP, LOGL_ERROR, "Failed to accept. errno: %d/%s\n",
243 errno, strerror(errno));
244 return -1;
245 }
246
247 conn = talloc_zero(rf, struct osmo_bsc_rf_conn);
248 if (!conn) {
249 LOGP(DINP, LOGL_ERROR, "Failed to allocate mem.\n");
250 close(fd);
251 return -1;
252 }
253
254 write_queue_init(&conn->queue, 10);
255 conn->queue.bfd.data = conn;
256 conn->queue.bfd.fd = fd;
257 conn->queue.bfd.when = BSC_FD_READ | BSC_FD_WRITE;
258 conn->queue.read_cb = rf_read_cmd;
259 conn->queue.write_cb = rf_write_cmd;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800260 conn->rf = rf;
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800261
262 if (bsc_register_fd(&conn->queue.bfd) != 0) {
263 close(fd);
264 talloc_free(conn);
265 return -1;
266 }
267
268 return 0;
269}
270
271struct osmo_bsc_rf *osmo_bsc_rf_create(const char *path, struct gsm_network *net)
272{
273 unsigned int namelen;
274 struct sockaddr_un local;
275 struct bsc_fd *bfd;
276 struct osmo_bsc_rf *rf;
277 int rc;
278
279 rf = talloc_zero(NULL, struct osmo_bsc_rf);
280 if (!rf) {
281 LOGP(DINP, LOGL_ERROR, "Failed to create osmo_bsc_rf.\n");
282 return NULL;
283 }
284
285 bfd = &rf->listen;
286 bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0);
287 if (bfd->fd < 0) {
288 LOGP(DINP, LOGL_ERROR, "Can not create socket. %d/%s\n",
289 errno, strerror(errno));
290 return NULL;
291 }
292
293 local.sun_family = AF_UNIX;
294 strncpy(local.sun_path, path, sizeof(local.sun_path));
295 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
296 unlink(local.sun_path);
297
298 /* we use the same magic that X11 uses in Xtranssock.c for
299 * calculating the proper length of the sockaddr */
300#if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
301 local.sun_len = strlen(local.sun_path);
302#endif
303#if defined(BSD44SOCKETS) || defined(SUN_LEN)
304 namelen = SUN_LEN(&local);
305#else
306 namelen = strlen(local.sun_path) +
307 offsetof(struct sockaddr_un, sun_path);
308#endif
309
310 rc = bind(bfd->fd, (struct sockaddr *) &local, namelen);
311 if (rc != 0) {
312 LOGP(DINP, LOGL_ERROR, "Failed to bind '%s' errno: %d/%s\n",
313 local.sun_path, errno, strerror(errno));
314 close(bfd->fd);
315 talloc_free(rf);
316 return NULL;
317 }
318
319 if (listen(bfd->fd, 0) != 0) {
320 LOGP(DINP, LOGL_ERROR, "Failed to listen: %d/%s\n", errno, strerror(errno));
321 close(bfd->fd);
322 talloc_free(rf);
323 return NULL;
324 }
325
326 bfd->when = BSC_FD_READ;
327 bfd->cb = rf_ctl_accept;
328 bfd->data = rf;
329
330 if (bsc_register_fd(bfd) != 0) {
331 LOGP(DINP, LOGL_ERROR, "Failed to register bfd.\n");
332 close(bfd->fd);
333 talloc_free(rf);
334 return NULL;
335 }
336
337 rf->gsm_network = net;
Holger Hans Peter Freyther7a1591b2010-09-16 00:10:18 +0800338 rf->policy = S_RF_ON;
Holger Hans Peter Freyther37ac4202011-02-24 14:19:14 +0100339 rf->last_state_command = "";
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800340
Holger Hans Peter Freyther00c805f2011-02-24 14:40:11 +0100341 /* check the rf state */
342 rf->rf_check.data = rf;
343 rf->rf_check.cb = rf_check_cb;
344
Holger Hans Peter Freyther2f4dbeb2010-06-30 14:29:53 +0800345 return rf;
346}
347