Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 1 | /* 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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * 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 Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 11 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * 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 Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/osmo_bsc_rf.h> |
| 24 | #include <openbsc/debug.h> |
| 25 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 12b917d | 2010-07-29 02:27:27 +0800 | [diff] [blame] | 26 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 27 | #include <openbsc/osmo_msc_data.h> |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 28 | |
| 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 Freyther | cf6f71a | 2010-11-22 19:32:13 +0100 | [diff] [blame] | 41 | #define RF_CMD_D_OFF 'd' |
| 42 | #define RF_CMD_ON_G 'g' |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 43 | |
| 44 | static 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 Freyther | cf6f71a | 2010-11-22 19:32:13 +0100 | [diff] [blame] | 58 | static void send_resp(struct osmo_bsc_rf_conn *conn, char send) |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 59 | { |
| 60 | struct msgb *msg; |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 61 | |
| 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 Freyther | cf6f71a | 2010-11-22 19:32:13 +0100 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * Send a |
| 83 | * 'g' when we are in grace mode |
| 84 | * '1' when one TRX is online, |
| 85 | * '0' otherwise |
| 86 | */ |
| 87 | static 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 Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 109 | static void send_signal(struct osmo_bsc_rf *rf, int val) |
Holger Hans Peter Freyther | 12b917d | 2010-07-29 02:27:27 +0800 | [diff] [blame] | 110 | { |
| 111 | struct rf_signal_data sig; |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 112 | sig.net = rf->gsm_network; |
Holger Hans Peter Freyther | 12b917d | 2010-07-29 02:27:27 +0800 | [diff] [blame] | 113 | |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 114 | rf->policy = val; |
Holger Hans Peter Freyther | 12b917d | 2010-07-29 02:27:27 +0800 | [diff] [blame] | 115 | dispatch_signal(SS_RF, val, &sig); |
| 116 | } |
| 117 | |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 118 | static int switch_rf_off(struct osmo_bsc_rf *rf) |
| 119 | { |
| 120 | lock_each_trx(rf->gsm_network, 1); |
| 121 | send_signal(rf, S_RF_OFF); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static void grace_timeout(void *_data) |
| 127 | { |
| 128 | struct osmo_bsc_rf *rf = (struct osmo_bsc_rf *) _data; |
| 129 | |
| 130 | LOGP(DINP, LOGL_NOTICE, "Grace timeout. Disabling the TRX.\n"); |
| 131 | switch_rf_off(rf); |
| 132 | } |
| 133 | |
| 134 | static int enter_grace(struct osmo_bsc_rf_conn *conn) |
| 135 | { |
| 136 | struct osmo_bsc_rf *rf = conn->rf; |
| 137 | |
| 138 | rf->grace_timeout.cb = grace_timeout; |
| 139 | rf->grace_timeout.data = rf; |
| 140 | bsc_schedule_timer(&rf->grace_timeout, rf->gsm_network->msc_data->mid_call_timeout, 0); |
| 141 | LOGP(DINP, LOGL_NOTICE, "Going to switch RF off in %d seconds.\n", |
| 142 | rf->gsm_network->msc_data->mid_call_timeout); |
| 143 | |
| 144 | send_signal(conn->rf, S_RF_GRACE); |
| 145 | return 0; |
| 146 | } |
| 147 | |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 148 | static int rf_read_cmd(struct bsc_fd *fd) |
| 149 | { |
| 150 | struct osmo_bsc_rf_conn *conn = fd->data; |
| 151 | char buf[1]; |
| 152 | int rc; |
| 153 | |
| 154 | rc = read(fd->fd, buf, sizeof(buf)); |
| 155 | if (rc != sizeof(buf)) { |
| 156 | LOGP(DINP, LOGL_ERROR, "Short read %d/%s\n", errno, strerror(errno)); |
| 157 | bsc_unregister_fd(fd); |
| 158 | close(fd->fd); |
| 159 | write_queue_clear(&conn->queue); |
| 160 | talloc_free(conn); |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | switch (buf[0]) { |
| 165 | case RF_CMD_QUERY: |
| 166 | handle_query(conn); |
| 167 | break; |
Holger Hans Peter Freyther | cf6f71a | 2010-11-22 19:32:13 +0100 | [diff] [blame] | 168 | case RF_CMD_D_OFF: |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 169 | bsc_del_timer(&conn->rf->grace_timeout); |
| 170 | switch_rf_off(conn->rf); |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 171 | break; |
| 172 | case RF_CMD_ON: |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 173 | bsc_del_timer(&conn->rf->grace_timeout); |
Holger Hans Peter Freyther | 7a1591b | 2010-09-16 00:10:18 +0800 | [diff] [blame] | 174 | lock_each_trx(conn->rf->gsm_network, 0); |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 175 | send_signal(conn->rf, S_RF_ON); |
Holger Hans Peter Freyther | 12b917d | 2010-07-29 02:27:27 +0800 | [diff] [blame] | 176 | break; |
Holger Hans Peter Freyther | cf6f71a | 2010-11-22 19:32:13 +0100 | [diff] [blame] | 177 | case RF_CMD_OFF: |
Holger Hans Peter Freyther | 70c232f | 2010-11-22 19:09:38 +0100 | [diff] [blame] | 178 | enter_grace(conn); |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 179 | break; |
| 180 | default: |
| 181 | LOGP(DINP, LOGL_ERROR, "Unknown command %d\n", buf[0]); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int rf_write_cmd(struct bsc_fd *fd, struct msgb *msg) |
| 189 | { |
| 190 | int rc; |
| 191 | |
| 192 | rc = write(fd->fd, msg->data, msg->len); |
| 193 | if (rc != msg->len) { |
| 194 | LOGP(DINP, LOGL_ERROR, "Short write %d/%s\n", errno, strerror(errno)); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int rf_ctl_accept(struct bsc_fd *bfd, unsigned int what) |
| 202 | { |
| 203 | struct osmo_bsc_rf_conn *conn; |
| 204 | struct osmo_bsc_rf *rf = bfd->data; |
| 205 | struct sockaddr_un addr; |
| 206 | socklen_t len = sizeof(addr); |
| 207 | int fd; |
| 208 | |
| 209 | fd = accept(bfd->fd, (struct sockaddr *) &addr, &len); |
| 210 | if (fd < 0) { |
| 211 | LOGP(DINP, LOGL_ERROR, "Failed to accept. errno: %d/%s\n", |
| 212 | errno, strerror(errno)); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | conn = talloc_zero(rf, struct osmo_bsc_rf_conn); |
| 217 | if (!conn) { |
| 218 | LOGP(DINP, LOGL_ERROR, "Failed to allocate mem.\n"); |
| 219 | close(fd); |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | write_queue_init(&conn->queue, 10); |
| 224 | conn->queue.bfd.data = conn; |
| 225 | conn->queue.bfd.fd = fd; |
| 226 | conn->queue.bfd.when = BSC_FD_READ | BSC_FD_WRITE; |
| 227 | conn->queue.read_cb = rf_read_cmd; |
| 228 | conn->queue.write_cb = rf_write_cmd; |
Holger Hans Peter Freyther | 7a1591b | 2010-09-16 00:10:18 +0800 | [diff] [blame] | 229 | conn->rf = rf; |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 230 | |
| 231 | if (bsc_register_fd(&conn->queue.bfd) != 0) { |
| 232 | close(fd); |
| 233 | talloc_free(conn); |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | struct osmo_bsc_rf *osmo_bsc_rf_create(const char *path, struct gsm_network *net) |
| 241 | { |
| 242 | unsigned int namelen; |
| 243 | struct sockaddr_un local; |
| 244 | struct bsc_fd *bfd; |
| 245 | struct osmo_bsc_rf *rf; |
| 246 | int rc; |
| 247 | |
| 248 | rf = talloc_zero(NULL, struct osmo_bsc_rf); |
| 249 | if (!rf) { |
| 250 | LOGP(DINP, LOGL_ERROR, "Failed to create osmo_bsc_rf.\n"); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | bfd = &rf->listen; |
| 255 | bfd->fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 256 | if (bfd->fd < 0) { |
| 257 | LOGP(DINP, LOGL_ERROR, "Can not create socket. %d/%s\n", |
| 258 | errno, strerror(errno)); |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | local.sun_family = AF_UNIX; |
| 263 | strncpy(local.sun_path, path, sizeof(local.sun_path)); |
| 264 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 265 | unlink(local.sun_path); |
| 266 | |
| 267 | /* we use the same magic that X11 uses in Xtranssock.c for |
| 268 | * calculating the proper length of the sockaddr */ |
| 269 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 270 | local.sun_len = strlen(local.sun_path); |
| 271 | #endif |
| 272 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 273 | namelen = SUN_LEN(&local); |
| 274 | #else |
| 275 | namelen = strlen(local.sun_path) + |
| 276 | offsetof(struct sockaddr_un, sun_path); |
| 277 | #endif |
| 278 | |
| 279 | rc = bind(bfd->fd, (struct sockaddr *) &local, namelen); |
| 280 | if (rc != 0) { |
| 281 | LOGP(DINP, LOGL_ERROR, "Failed to bind '%s' errno: %d/%s\n", |
| 282 | local.sun_path, errno, strerror(errno)); |
| 283 | close(bfd->fd); |
| 284 | talloc_free(rf); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | if (listen(bfd->fd, 0) != 0) { |
| 289 | LOGP(DINP, LOGL_ERROR, "Failed to listen: %d/%s\n", errno, strerror(errno)); |
| 290 | close(bfd->fd); |
| 291 | talloc_free(rf); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | bfd->when = BSC_FD_READ; |
| 296 | bfd->cb = rf_ctl_accept; |
| 297 | bfd->data = rf; |
| 298 | |
| 299 | if (bsc_register_fd(bfd) != 0) { |
| 300 | LOGP(DINP, LOGL_ERROR, "Failed to register bfd.\n"); |
| 301 | close(bfd->fd); |
| 302 | talloc_free(rf); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | rf->gsm_network = net; |
Holger Hans Peter Freyther | 7a1591b | 2010-09-16 00:10:18 +0800 | [diff] [blame] | 307 | rf->policy = S_RF_ON; |
Holger Hans Peter Freyther | 2f4dbeb | 2010-06-30 14:29:53 +0800 | [diff] [blame] | 308 | |
| 309 | return rf; |
| 310 | } |
| 311 | |