intertask_interface_dump.c 16.6 KB
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2013 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fr/openairinterface
  Address      : EURECOM, Campus SophiaTech, 450 Route des Chappes
                 06410 Biot FRANCE

*******************************************************************************/

/** @brief Intertask Interface Signal Dumper
 * Allows users to connect their itti_debugger to this process and dump
 * signals exchanged between tasks.
 * @author Sebastien Roux <sebastien.roux@eurecom.fr>
 */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <arpa/inet.h>

#include "assertions.h"
#include "queue.h"

#include "intertask_interface.h"
#include "intertask_interface_dump.h"

#define SIGNAL_NAME_LENGTH  50

/* Declared in intertask_interface.c */
extern int itti_debug;

#define ITTI_DEBUG(x, args...) do { fprintf(stdout, "[ITTI][D]"x, ##args); } \
    while(0)
#define ITTI_ERROR(x, args...) do { fprintf(stdout, "[ITTI][E]"x, ##args); } \
    while(0)

typedef struct itti_queue_item_s {
    STAILQ_ENTRY(itti_queue_item_s) entry;
    void    *data;
    uint32_t data_size;
    uint32_t message_number;
    char     message_name[SIGNAL_NAME_LENGTH];
} itti_queue_item_t;

typedef struct {
    int      sd;
    uint32_t last_message_number;
} itti_client_desc_t;

typedef struct itti_desc_s {
    /* The acceptor thread */
    pthread_t itti_acceptor_thread;
    pthread_t itti_write_thread;

    /* Protect the circular queue */
    pthread_mutex_t queue_mutex;

    /* List of messages to dump.
     * NOTE: we limit the size of this queue to retain only the last exchanged
     * messages. The size can be increased by setting up the ITTI_QUEUE_SIZE_MAX
     * in mme_default_values.h or by putting a custom in the configuration file.
     */
    STAILQ_HEAD(itti_queue_s, itti_queue_item_s) itti_message_queue;
    struct itti_queue_item_s *itti_queue_last;
    uint32_t queue_size;

    int nb_connected;

    itti_client_desc_t itti_clients[ITTI_DUMP_MAX_CON];
} itti_desc_t;

static itti_desc_t itti_queue;

/* Message sent is an intertask dump type */
#define ITTI_DUMP_MESSAGE_TYPE      0x1
#define ITTI_STATISTIC_MESSAGE_TYPE 0x2
#define ITTI_DUMP_XML_DEFINITION    0x3

typedef struct {
    /* The size of this structure */
    uint32_t message_size;
    uint32_t message_type;
} itti_socket_header_t;

typedef struct {
    itti_socket_header_t header;

    uint32_t message_number;
    char signal_name[SIGNAL_NAME_LENGTH];

    /* Message payload is added here, this struct is used as an header */
} itti_dump_message_t;

typedef struct {
    itti_socket_header_t header;
    
} itti_statistic_message_t;

static int itti_dump_send_message(int sd, itti_queue_item_t *message);
static int itti_dump_handle_new_connection(int sd, const char *xml_definition,
                                      uint32_t xml_definition_length);
static int itti_dump_send_xml_definition(const int sd, const char *message_definition_xml,
                                         const uint32_t message_definition_xml_length);

static
int itti_dump_send_message(int sd, itti_queue_item_t *message)
{
    int result = 0;
    itti_dump_message_t *new_message;

    /* Allocate memory for message header and payload */
    uint32_t size = sizeof(itti_dump_message_t) + message->data_size;

    DevCheck(sd > 0, sd, 0, 0);
    DevAssert(message != NULL);

    new_message = calloc(1, size);
    DevAssert(new_message != NULL);

    /* Preparing the header */
    new_message->header.message_size = size;
    new_message->header.message_type = ITTI_DUMP_MESSAGE_TYPE;

    new_message->message_number = message->message_number;
    /* Copy the name, but leaves last byte set to 0 in case name is too long */
    memcpy(new_message->signal_name, message->message_name, SIGNAL_NAME_LENGTH - 1);
    /* Appends message payload */
    memcpy(&new_message[1], message->data, message->data_size);

    if (write(sd, new_message, size) == -1) {
        ITTI_ERROR("[%d] Failed to write message of size %u to socket (%d:%s)\n",
                   sd, size, errno, strerror(errno));
        result = -1;
    }

    free(new_message);
    return result;
}

static int itti_dump_send_xml_definition(const int sd, const char *message_definition_xml,
                                         const uint32_t message_definition_xml_length)
{
    itti_socket_header_t xml_definition_header;

    DevCheck(sd > 0, sd, 0, 0);
    DevAssert(message_definition_xml != NULL);

    ITTI_DEBUG("[%d] Sending XML definition of size %u to observer peer\n",
               sd, message_definition_xml_length);

    xml_definition_header.message_size = sizeof(xml_definition_header)
    + message_definition_xml_length;
    xml_definition_header.message_type = ITTI_DUMP_XML_DEFINITION;

    if (write(sd, &xml_definition_header, sizeof(xml_definition_header)) < 0) {
        ITTI_ERROR("[%d] Failed to write header of size %zu to socket (%d:%s)\n",
                   sd, sizeof(xml_definition_header), errno, strerror(errno));
        return -1;
    }
    if (write(sd, message_definition_xml, message_definition_xml_length) < 0) {
        ITTI_ERROR("[%d] Failed to write XML definition of size %u to socket (%d:%s)\n",
                   sd, message_definition_xml_length, errno, strerror(errno));
        return -1;
    }
    return 0;
}

int itti_dump_queue_message(message_number_t message_number,
                            MessageDef *message_p,
                            const char *message_name,
                            const uint32_t message_size)
{
    itti_queue_item_t *new;
    itti_queue_item_t *head = NULL;
    size_t message_name_length;
    int i;

    DevAssert(message_name != NULL);
    DevAssert(message_p != NULL);

    new = calloc(1, sizeof(itti_queue_item_t));

    if (new == NULL) {
        ITTI_ERROR("Failed to allocate memory (%s:%d)\n",
                   __FILE__, __LINE__);
        return -1;
    }

    new->data = malloc(message_size);

    if (new->data == NULL) {
        ITTI_ERROR("Failed to allocate memory (%s:%d)\n",
                   __FILE__, __LINE__);
        return -1;
    }
    memcpy(new->data, message_p, message_size);
    new->data_size = message_size;
    new->message_number = message_number;

    message_name_length = strlen(message_name) + 1;
    DevCheck(message_name_length <= SIGNAL_NAME_LENGTH, message_name_length,
             SIGNAL_NAME_LENGTH, 0);
    memcpy(new->message_name, message_name, message_name_length);

    /* Lock the queue mutex for writing to insert the new element */
    pthread_mutex_lock(&itti_queue.queue_mutex);

    /* We reached the maximum size for the queue of messages -> remove the head */
    if (itti_queue.queue_size + message_size > ITTI_QUEUE_SIZE_MAX) {
        head = STAILQ_FIRST(&itti_queue.itti_message_queue);
        /* Remove the head */
        STAILQ_REMOVE_HEAD(&itti_queue.itti_message_queue, entry);
    } else {
        itti_queue.queue_size += message_size;
    }
    /* Insert the packet at tail */
    STAILQ_INSERT_TAIL(&itti_queue.itti_message_queue, new, entry);
    itti_queue.itti_queue_last = new;

    /* Release the mutex */
    pthread_mutex_unlock(&itti_queue.queue_mutex);

    for (i = 0; i < ITTI_DUMP_MAX_CON; i++) {
        if (itti_queue.itti_clients[i].sd == -1)
            continue;
        itti_dump_send_message(itti_queue.itti_clients[i].sd, new);
    }

    /* No need to have the mutex locked to free data as at this point the message
     * is no more in the list.
     */
    if (head) {
        free(head->data);
        free(head);
        head = NULL;
    }

    return 0;
}

static void *itti_dump_socket(void *arg_p)
{
    uint32_t message_definition_xml_length;
    char *message_definition_xml;
    int rc;
    int itti_listen_socket, max_sd;
    int on = 1;
    fd_set master_set, working_set;
    struct sockaddr_in servaddr; /* socket address structure */

    ITTI_DEBUG("Creating TCP dump socket on port %u\n", ITTI_PORT);

    message_definition_xml = (char *)arg_p;
    DevAssert(message_definition_xml != NULL);

    message_definition_xml_length = strlen(message_definition_xml) + 1;

    if ((itti_listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        ITTI_ERROR("Socket creation failed (%d:%s)\n", errno, strerror(errno));
        pthread_exit(NULL);
    }

    /* Allow socket reuse */
    rc = setsockopt(itti_listen_socket, SOL_SOCKET, SO_REUSEADDR,
                    (char *)&on, sizeof(on));
    if (rc < 0) {
        ITTI_ERROR("setsockopt SO_REUSEADDR failed (%d:%s)\n", errno, strerror(errno));
        close(itti_listen_socket);
        pthread_exit(NULL);
    }

    /* Set socket to be non-blocking.
     * NOTE: sockets accepted will inherit this option.
     */
    rc = ioctl(itti_listen_socket, FIONBIO, (char *)&on);
    if (rc < 0) {
        ITTI_ERROR("ioctl FIONBIO (non-blocking) failed (%d:%s)\n", errno, strerror(errno));
        close(itti_listen_socket);
        pthread_exit(NULL);
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family      = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port        = htons(ITTI_PORT);

    if (bind(itti_listen_socket, (struct sockaddr *) &servaddr,
             sizeof(servaddr)) < 0 ) {
        ITTI_ERROR("Bind failed (%d:%s)\n", errno, strerror(errno));
        pthread_exit(NULL);
    }
    if (listen(itti_listen_socket, 5) < 0 ) {
        ITTI_ERROR("Listen failed (%d:%s)\n", errno, strerror(errno));
        pthread_exit(NULL);
    }

    FD_ZERO(&master_set);
    max_sd = itti_listen_socket;
    FD_SET(itti_listen_socket, &master_set);

    /* Loop waiting for incoming connects or for incoming data
     * on any of the connected sockets.
     */
    while (1) {
        int desc_ready;
        int client_socket = -1;
        int i;

        memcpy(&working_set, &master_set, sizeof(master_set));

        ITTI_DEBUG("Stuck on select\n");

        /* No timeout: select blocks till a new event has to be handled
         * on sd's.
         */
        rc = select(max_sd + 1, &working_set, NULL, NULL, NULL);

        if (rc < 0) {
            ITTI_ERROR("select failed (%d:%s)\n", errno, strerror(errno));
            pthread_exit(NULL);
        }

        desc_ready = rc;
        for (i = 0; i <= max_sd && desc_ready > 0; i++) {
            if (FD_ISSET(i, &working_set)) {
                ITTI_DEBUG("Handling socket %d\n", i);
                desc_ready -= 1;
                /* Check if the socket where data available is the listening
                 * socket.
                 */
                if (i == itti_listen_socket) {
                    do {
                        client_socket = accept(itti_listen_socket, NULL, NULL);
                        if (client_socket < 0) {
                            if (errno == EWOULDBLOCK || errno == EAGAIN) {
                                /* No more new connection */
                                ITTI_DEBUG("No more new connection\n");
                                continue;
                            } else {
                                ITTI_ERROR("accept failed (%d:%s)\n", errno, strerror(errno));
                                pthread_exit(NULL);
                            }
                        }
                        if (itti_dump_handle_new_connection(client_socket, message_definition_xml,
                            message_definition_xml_length) == 0) {
                            /* The socket has been accepted.
                             * We have to update the set to include this new sd.
                             */
                            FD_SET(client_socket, &master_set);
                            if (client_socket > max_sd)
                                max_sd = client_socket;
                        }
                    } while(client_socket != -1);
                } else {
                    /* For now the MME itti dumper should not receive data
                     * other than connection oriented (CLOSE).
                     */
                    uint8_t j;

                    ITTI_DEBUG("Socket %d disconnected\n", i);

                    /* Close the socket and update info related to this connection */
                    close(i);

                    for (j = 0; j < ITTI_DUMP_MAX_CON; j++) {
                        if (itti_queue.itti_clients[j].sd == i)
                            break;
                    }

                    /* In case we don't find the matching sd in list of known
                     * connections -> assert.
                     */
                    DevCheck(j < ITTI_DUMP_MAX_CON, j, ITTI_DUMP_MAX_CON, i);

                    /* Re-initialize the socket to -1 so we can accept new
                     * incoming connections.
                     */
                    itti_queue.itti_clients[j].sd                  = -1;
                    itti_queue.itti_clients[j].last_message_number = 0;
                    itti_queue.nb_connected--;

                    /* Remove the socket from the FD set and update the max sd */
                    FD_CLR(i, &master_set);
                    if (i == max_sd)
                    {
                        if (itti_queue.nb_connected == 0) {
                            /* No more new connection max_sd = itti_listen_socket */
                            max_sd = itti_listen_socket;
                        } else {
                            while (FD_ISSET(max_sd, &master_set) == 0) {
                                max_sd -= 1;
                            }
                        }
                    }
                }
            }
        }
    }
    return NULL;
}

static
int itti_dump_handle_new_connection(int sd, const char *xml_definition, uint32_t xml_definition_length)
{
    if (itti_queue.nb_connected < ITTI_DUMP_MAX_CON) {
        itti_queue_item_t *item;
        uint8_t i;

        for (i = 0; i < ITTI_DUMP_MAX_CON; i++) {
            /* Let's find a place to store the new client */
            if (itti_queue.itti_clients[i].sd == -1) {
                break;
            }
        }

        ITTI_DEBUG("Found place to store new connection: %d\n", i);

        DevCheck(i < ITTI_DUMP_MAX_CON, i, ITTI_DUMP_MAX_CON, sd);
        itti_queue.itti_clients[i].sd = sd;
        itti_queue.nb_connected++;

        ITTI_DEBUG("Socket %d accepted\n", sd);

        /* Send the XML message definition */
        if (itti_dump_send_xml_definition(sd, xml_definition, xml_definition_length) < 0) {
            ITTI_ERROR("Failed to send XML definition\n");
            close (sd);
            return -1;
        }

        /* At this point we have to dump the complete list */
        pthread_mutex_lock(&itti_queue.queue_mutex);
        STAILQ_FOREACH(item, &itti_queue.itti_message_queue, entry) {
            itti_dump_send_message(sd, item);
        }
        pthread_mutex_unlock(&itti_queue.queue_mutex);
    } else {
        ITTI_DEBUG("Socket %d rejected\n", sd);
        /* We have reached max number of users connected...
         * Reject the connection.
         */
        close (sd);
        return -1;
    }

    return 0;
}

int itti_dump_init(const char * const messages_definition_xml)
{
    int i;

    memset(&itti_queue, 0, sizeof(itti_desc_t));

    pthread_mutex_init(&itti_queue.queue_mutex, NULL);
    STAILQ_INIT(&itti_queue.itti_message_queue);
    itti_queue.queue_size = 0;
    itti_queue.nb_connected = 0;

    for(i = 0; i < ITTI_DUMP_MAX_CON; i++) {
        itti_queue.itti_clients[i].sd = -1;
        itti_queue.itti_clients[i].last_message_number = 0;
    }
    if (pthread_create(&itti_queue.itti_acceptor_thread, NULL, &itti_dump_socket,
        (void *)messages_definition_xml) < 0) {
        ITTI_ERROR("pthread_create failed (%d:%s)\n", errno, strerror(errno));
        return -1;
    }
    return 0;
}