Пример #1
0
static void
aim_log_time__(aim_pvs_t* pvs)
{
#if AIM_CONFIG_LOG_INCLUDE_LINUX_TIMESTAMP == 1
    struct timeval timeval;
    struct tm *loctime;
    char lt[128];

    gettimeofday(&timeval, NULL);
    loctime = localtime(&timeval.tv_sec);
    strftime(lt, sizeof(lt), "%b %d %T", loctime);
    aim_printf(pvs, "%s.%.03d ", lt, (int)timeval.tv_usec/1000);
#else
    AIM_REFERENCE(pvs);
    AIM_REFERENCE(size);
#endif
}
int
onlp_platform_defaults_log_flag_value(const char* str, onlp_platform_defaults_log_flag_t* e, int substr)
{
    int i;
    AIM_REFERENCE(substr);
    if(aim_map_si_s(&i, str, onlp_platform_defaults_log_flag_map, 0)) {
        /* Enum Found */
        *e = i;
        return 0;
    }
    else {
        return -1;
    }
}
int
ofconnectionmanager_log_flag_value(const char* str, ofconnectionmanager_log_flag_t* e, int substr)
{
    int i;
    AIM_REFERENCE(substr);
    if(aim_map_si_s(&i, str, ofconnectionmanager_log_flag_map, 0)) {
        /* Enum Found */
        *e = i;
        return 0;
    }
    else {
        return -1;
    }
}
Пример #4
0
int
sff_module_caps_value(const char* str, sff_module_caps_t* e, int substr)
{
    int i;
    AIM_REFERENCE(substr);
    if(aim_map_si_s(&i, str, sff_module_caps_map, 0)) {
        /* Enum Found */
        *e = i;
        return 0;
    }
    else {
        return -1;
    }
}
Пример #5
0
int main(int argc, char* argv[])
{
    ucli_t* uopts; 

    AIM_REFERENCE(argc); 

    ucli_init(); 
    ucli_module_init(&options_module); 
    uopts = ucli_create(NULL, &options_module, NULL); 
    ucli_dispatch_argv(uopts, &aim_pvs_stdout, argv+1); 
    ucli_destroy(uopts); 
    ucli_denit();
    return 0;
}
int
platform_id_value(const char* str, platform_id_t* e, int substr)
{
	int i;
	AIM_REFERENCE(substr);
	if(aim_map_si_s(&i, str, platform_id_map, 0)) {
		/* Enum Found */
		*e = i;
		return 0;
	}
	else{
		return -1;
	}

}
Пример #7
0
/*
 * The {ppe_field_info} type will convert the argument
 * directly to a ppe_field_info_t pointer
 */
static int
ppe_aim_fs__ppe_field_info(aim_datatype_context_t* dtc,
                            const char* arg, aim_va_list_t* vargs)
{
    const ppe_field_info_t** fip = va_arg(vargs->val,
                                          const ppe_field_info_t**);
    ppe_field_t field;

    AIM_REFERENCE(dtc);

    if(ppe_field_value(arg, &field, 0) == 0) {
        /* Found -- return corresponding pointer */
        *fip = ppe_field_info_table+field;
        return AIM_DATATYPE_OK;
    }
    else {
        return AIM_DATATYPE_ERROR;
    }
}
Пример #8
0
char* 
ucli_util_data_to_string(ucli_context_t* uc, uint8_t* data, int size, 
                         int columns)
{
    int c; 
    int i;

    /**
     * We will need at least 3*size for all bytes. 
     * Each column requires 8 extra characters, including newline. 
     * 
     */
    int len = (size*3) + (columns*8) + 1; 
    char* s = aim_zmalloc(len+1); 
    char* sp = s; 

    AIM_REFERENCE(uc); 

    for(i = 0; i < size; i++) {
        if(i % columns == 0) {
            if(i != 0) {
                c = aim_snprintf(sp, len, "\n");
                sp += c, len -= c; 
            }
            if(size > columns) {
                c = aim_snprintf(sp, len, "  %.4x: ", i); 
                sp += c, len -= c; 
            }
        }
        c = aim_snprintf(sp, len, "%.2x ", data[i]); 
        sp += c, len -= c; 
    }
    if((size > columns) && (size % columns != 0)) {
        c = aim_snprintf(sp, len, "\n"); 
        sp += c, len -= c; 
    }
    return s; 
}
Пример #9
0
int
vpi_mmap_interface_create(vpi_interface_t** vi, char* args[], int flags,
                       const char* vpi_name_ptr)
{
    struct sockaddr_ll sockaddr;
    struct packet_mreq sockparams;
    struct tpacket_req treq;
    struct ifreq ifreq;
    vpi_interface_mmap_t* nvi = aim_zmalloc(sizeof(*nvi));
    char** arg = args;
    int version;
    int i;
    frame_control_t* fc;

    AIM_REFERENCE(flags);

    if(nvi == NULL) {
        VPI_MERROR("interface allocation failed for %s.",
                   vpi_name_ptr);
        return -1;
    }

    /*
     * Point our log_string to our name so we can use it immediately
     * in log messages.
     */
    nvi->log_string = vpi_name_ptr;


    /*
     * The first argument is the type -- skip for now
     */
    arg++;

    aim_strlcpy(nvi->interface_name, *arg, sizeof(nvi->interface_name));

    /* Create RAW socket */
    if((nvi->fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
        VPI_ERROR(nvi, "socket() failed: %s\n", strerror(errno));
        aim_free(nvi);
        return -1;
    }

    /* Set version */
    version = TPACKET_V2;
    if(setsockopt(nvi->fd, SOL_PACKET, PACKET_VERSION,
                  &version, sizeof(version)) < 0) {
        VPI_ERROR(nvi, "setsockopt() version failed: %s\n", strerror(errno));
        aim_free(nvi);
        return -1;
    }

    /*
     * Get the interface index for the requested interface, as specified
     * in the current argument.
     */
    VPI_MEMSET(&ifreq, 0, sizeof(ifreq));
    aim_strlcpy(ifreq.ifr_name, nvi->interface_name, IFNAMSIZ);
    if(ioctl(nvi->fd, SIOCGIFINDEX, &ifreq) < 0) {
        VPI_ERROR(nvi, "ioctl() failed: %s", strerror(errno));
        close(nvi->fd);
        aim_free(nvi);
        return -1;
    }
    nvi->ifindex = ifreq.ifr_ifindex;
    VPI_INFO(nvi, "ifndex is %d", nvi->ifindex);

    /* Set promisc */
    VPI_MEMSET(&sockparams, 0, sizeof(sockparams));
    sockparams.mr_type = PACKET_MR_PROMISC;
    sockparams.mr_ifindex = nvi->ifindex;
    if(setsockopt(nvi->fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
                  (void *)&sockparams, sizeof(sockparams)) < 0) {
        VPI_ERROR(nvi, "setsockopt() promisc failed. %s\n", strerror(errno));
        close(nvi->fd);
        aim_free(nvi);
        return -1;
    }

    /* Set up rx_ring buffer */
    VPI_MEMSET(&treq, 0, sizeof(treq));
    treq.tp_block_size = BLOCK_SIZE;
    treq.tp_frame_size = FRAME_SIZE;
    treq.tp_block_nr = NUM_BLOCKS;
    treq.tp_frame_nr = NUM_FRAMES;;
    if(setsockopt(nvi->fd, SOL_PACKET, PACKET_RX_RING,
                  (void*)&treq , sizeof(treq)) < 0) {
        VPI_ERROR(nvi, "setsockopt() rx_ring failed. %s\n", strerror(errno));
        close(nvi->fd);
        aim_free(nvi);
        return -1;
    }

    /* If num blocks change, bail! */
    if(treq.tp_block_nr != NUM_BLOCKS) {
        AIM_DIE("Unhandled: RX_RING block number changed!\n");
    }

    /* Set up rx_ring */
    nvi->rx_ring.buf = mmap(NULL, BLOCK_SIZE*NUM_BLOCKS,
                            PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
                            nvi->fd, 0);
    if(nvi->rx_ring.buf == MAP_FAILED) {
        VPI_ERROR(nvi, "mmap() failed.\n");
        close(nvi->fd);
        aim_free(nvi);
        return -1;
    }

    /* Set up rx_ring and frame controls */
    nvi->rx_ring.current_frame = 0;
    for(i = 0; i < NUM_FRAMES; i++) {
        fc = &nvi->rx_ring.frames[i];
        fc->base = nvi->rx_ring.buf + (i*FRAME_SIZE);
    }

    /* Bind */
    VPI_MEMSET(&sockaddr, 0, sizeof(sockaddr));
    sockaddr.sll_family = AF_PACKET;
    sockaddr.sll_protocol = htons(ETH_P_ALL);
    sockaddr.sll_ifindex = nvi->ifindex;
    if(bind(nvi->fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) {
        VPI_ERROR(nvi, "bind() failed.\n");
        close(nvi->fd);
        aim_free(nvi);
        return -1;
    }

    nvi->interface.send = vpi_mmap_interface_send;
    nvi->interface.recv = vpi_mmap_interface_recv;
    nvi->interface.recv_ready = vpi_mmap_interface_recv_ready;
    nvi->interface.destroy = vpi_mmap_interface_destroy;
    nvi->interface.descriptor = vpi_mmap_interface_descriptor;

    *vi = (vpi_interface_t*)nvi;
    return 0;
}
Пример #10
0
int
vpi_veth_interface_create(vpi_interface_t** vi, char* args[], int flags,
                       const char* vpi_name_ptr)
{
    struct sockaddr_ll sockaddr;
    struct ifreq ifreq;
    vpi_interface_veth_t* nvi = aim_zmalloc(sizeof(*nvi));
    char** arg = args;

    AIM_REFERENCE(flags);

    if(nvi == NULL) {
        VPI_MERROR("interface allocation failed for %s.",
                   vpi_name_ptr);
        return -1;
    }

    /*
     * Point our log_string to our name so we can use it immediately
     * in log messages.
     */
    nvi->log_string = vpi_name_ptr;


    /*
     * The first argument is the type -- skip for now
     */
    arg++;

    aim_strlcpy(nvi->interface_name, *arg, sizeof(nvi->interface_name));

    /* Create RAW socket */
    if((nvi->fd = socket(PF_PACKET, SOCK_RAW, 0)) < 0) {
        VPI_ERROR(nvi, "socket() failed: %s\n", strerror(errno));
        aim_free(nvi);
        return -1;
    }

    /*
     * Get the interface index for the requested interface, as specified
     * in the current argument.
     */

    VPI_MEMSET(&ifreq, 0, sizeof(ifreq));
    aim_strlcpy(ifreq.ifr_name, nvi->interface_name, IFNAMSIZ);

    if(ioctl(nvi->fd, SIOCGIFINDEX, &ifreq) < 0) {
        VPI_ERROR(nvi, "ioctl() failed: %s", strerror(errno));
        close(nvi->fd);
        aim_free(nvi);
        return -1;
    }
    nvi->ifindex = ifreq.ifr_ifindex;

    VPI_INFO(nvi, "ifndex is %d", nvi->ifindex);

    VPI_MEMSET(&sockaddr, 0, sizeof(sockaddr));
    sockaddr.sll_family=AF_PACKET;
    sockaddr.sll_protocol = htons(ETH_P_ALL);
    sockaddr.sll_ifindex = ifreq.ifr_ifindex;

    if(bind(nvi->fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) {
        VPI_ERROR(nvi, "bind() failed");
        return -1;
    }

    nvi->interface.send = vpi_veth_interface_send;
    nvi->interface.recv = vpi_veth_interface_recv;
    nvi->interface.recv_ready = vpi_veth_interface_recv_ready;
    nvi->interface.destroy = vpi_veth_interface_destroy;
    nvi->interface.descriptor = vpi_veth_interface_descriptor;

    *vi = (vpi_interface_t*)nvi;
    return 0;
}
Пример #11
0
int
vpi_tap_interface_create(vpi_interface_t** vi, char* args[], int flags,
                       const char* vpi_name_ptr)
{
    struct ifreq ifr;
    int fd, rv;
    vpi_interface_tap_t* nvi;
    char** arg = args;
    int persist = 1;

    AIM_REFERENCE(flags);

    nvi = aim_zmalloc(sizeof(*nvi));
    nvi->log_string = vpi_name_ptr;

    if( (fd = open("/dev/net/tun", O_RDWR)) < 0) {
        AIM_LOG_ERROR(nvi, "open(/dev/net/tun): %{errno}", errno);
        goto error;
    }

    /* The first argument is the VPI type. */
    arg++;

    VPI_STRNCPY(nvi->interface_name, *arg, IFNAMSIZ);

    VPI_MEMSET(&ifr, 0, sizeof(ifr));
    VPI_STRNCPY(ifr.ifr_name, *arg, IFNAMSIZ);
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;

    if( (rv = ioctl(fd, TUNSETIFF, (void*)&ifr)) < 0) {
        AIM_LOG_ERROR(nvi, "ioctl(TUNSETIFF): %{errno}", errno);
        goto error;
    }

    arg++;

    while(*arg) {
        uint8_t mac[6];

        if(!VPI_STRCMP(*arg, "nopersist")) {
            persist = 0;
        }
        else if(!VPI_STRCMP(*arg, "persist")) {
            persist = 1;
        }
        else if(aim_sparse(arg, NULL, "{mac}", mac) == 0) {

            ifr.ifr_flags = 0;
            ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
            VPI_MEMCPY(ifr.ifr_hwaddr.sa_data, mac, 6);
            if(ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) {
                AIM_LOG_ERROR(nvi, "ioctl(%s, SIOCSIFHWADDR,%{mac}): %{errno}",
                              ifr.ifr_name, ifr.ifr_hwaddr.sa_data, errno);
                goto error;
            }
        }
        else {
            AIM_LOG_ERROR(nvi, "argument '%s' cannot be parsed.", *arg);
            goto error;
        }
        arg++;
    }

    if( (rv = ioctl(fd, TUNSETPERSIST, persist)) < 0) {
        AIM_LOG_ERROR(nvi, "ioctl(TUNSETPERSIST, %d): %{errno}",
                      persist, errno);
        goto error;
    }

    nvi->fd = fd;
    nvi->interface.send = vpi_tap_interface_send;
    nvi->interface.recv = vpi_tap_interface_recv;
    nvi->interface.recv_ready = vpi_tap_interface_recv_ready;
    nvi->interface.destroy = vpi_tap_interface_destroy;
    nvi->interface.descriptor = vpi_tap_interface_descriptor;

    *vi = (vpi_interface_t*)nvi;
    return 0;

 error:
    if(fd > 0) {
        close(fd);
    }
    if(nvi) {
        aim_free(nvi);
    }
    return -1;
}
Пример #12
0
char* 
vpi_bpp_interface_preprocess(char* args[], const char* create_spec)
{       
    char** arg = args;
    char track_script[256]; 
    char cmd[256]; 
    int port; 
    char* type = "udp"; 

    AIM_REFERENCE(create_spec); 

    
    /*
     * First arg is bpp
     */
    
    arg++;

    /*
     * The second argument may be an optional selection for
     * UDP or TCP. The default is udp. 
     */
    if(!VPI_STRCMP("tcp", *arg) || !VPI_STRCMP("udp", *arg)) {
        type = *arg; 
        arg++; 
    }

    /*
     * Support special localhost processing. 
     * The special "localhost:port" target points directly to
     * localhost on the given port number.
     * 
     * This is mostly a testing convenience. 
     *
     */
    if(VPI_SSCANF(*arg, "localhost:%d", &port) == 1) {
        VPI_SPRINTF(cmd, "*s|%s|send:localhost:%d|recv:0",
                    type, port); 
    }
    else {
        /*
         * Determine the UDP spec we need to talk to the target.
         */
        char server[64] = {0}; 
        FILE* rv; 

        if(getenv("TRACK_SCRIPT")) { 
            VPI_STRCPY(track_script, getenv("TRACK_SCRIPT")); 
        }
        else if(getenv("BIGLAB")) { 
            VPI_SPRINTF(track_script, "%s/track", getenv("BIGLAB")); 
        }
        else {
            VPI_STRCPY(track_script, "/usr/bin/track"); 
        }
        if(access(track_script, F_OK) != 0) { 
            VPI_MERROR("Cannot access track script @ %s", track_script); 
            return NULL; 
        }
    
        VPI_SPRINTF(cmd, "%s vpi %s %s", track_script, type, *arg); 
        
        rv = popen(cmd, "r"); 
        if(VPI_SSCANF(fgets(cmd, sizeof(cmd), rv), "%[^:]:%d", server, &port) == 2) {
            VPI_MINFO("track: server=%s port=%d", server, port); 
        }
        else {
            VPI_MERROR("error reading output from track script."); 
            return NULL; 
        }

        VPI_SPRINTF(cmd, "*s|%s|send:%s:%d|recv:0", 
                    type, server, port); 
    }
    return aim_strdup(cmd); 
}
Пример #13
0
int
vpi_pcap_interface_create(vpi_interface_t** vi, char* args[], int flags, 
                       const char* vpi_name_ptr)
{
    vpi_interface_pcap_t* nvi = aim_zmalloc(sizeof(*nvi)); 
    char** arg = args; 
    char errbuf[PCAP_ERRBUF_SIZE];

    AIM_REFERENCE(flags); 

    if(nvi == NULL) {
        VPI_MERROR("interface allocation failed for %s.", 
                   vpi_name_ptr); 
        return -1; 
    }   
    
    /*
     * Point our log_string to our name so we can use it immediately 
     * in log messages. 
     */
    nvi->log_string = vpi_name_ptr; 


    /* 
     * The first argument is the type -- skip for now
     */
    arg++; 

    /*
     * next arg - interface name. 
     */
    if((nvi->pcap = pcap_create(*arg, errbuf)) == NULL) {
        VPI_ERROR(nvi, "pcap_create(%s) failed: %s\n", 
                  *arg, errbuf); 
        aim_free(nvi); 
        return -1; 
    }

    if(pcap_set_promisc(nvi->pcap, 1) != 0) {
        VPI_WARN(nvi, "pcap_set_promisc() failed."); 
    }
    
    if (pcap_activate(nvi->pcap) != 0) {
        VPI_ERROR(nvi, "pcap_activate() failed: %s", pcap_geterr(nvi->pcap));
        pcap_close(nvi->pcap); 
        aim_free(nvi); 
        return -1; 
     }

    nvi->fd = pcap_get_selectable_fd(nvi->pcap); 
    if(nvi->fd < 0) {
        VPI_WARN(nvi, "pcap_get_selectable_fd() returned %d", nvi->fd); 
    }

    nvi->interface.send = vpi_pcap_interface_send;
    nvi->interface.recv = vpi_pcap_interface_recv; 
    nvi->interface.recv_ready = vpi_pcap_interface_recv_ready; 
    nvi->interface.destroy = vpi_pcap_interface_destroy; 
    nvi->interface.descriptor = vpi_pcap_interface_descriptor; 

    *vi = (vpi_interface_t*)nvi; 
    return 0; 
}
int
vpi_pcapdump_interface_create(vpi_interface_t** vi, char* args[], int flags,
                              const char* vpi_name_ptr)
{
    vpi_interface_pcapdump_t* nvi = aim_zmalloc(sizeof(*nvi));
    char** arg = args;
    char errbuf[PCAP_ERRBUF_SIZE];

    AIM_REFERENCE(flags);

    if(nvi == NULL) {
        VPI_MERROR("interface allocation failed for %s.",
                   vpi_name_ptr);
        return -1;
    }

    /*
     * Point our log_string to our name so we can use it immediately
     * in log messages.
     */
    nvi->log_string = vpi_name_ptr;


    /*
     * The first argument is the type -- skip for now
     */
    arg++;

    if((nvi->pcap = pcap_open_dead(DLT_EN10MB, 10000)) == NULL) {
        VPI_ERROR(nvi, "pcap_open_dead() failed: %s\n",
                  *arg, errbuf);
        aim_free(nvi);
        return -1;
    }

    /*
     * next arg - filename name.
     */
    if((nvi->pcap_dumper = pcap_dump_open(nvi->pcap, *arg)) == NULL) {
        VPI_ERROR(nvi, "pcap_dump_open(%s) failed\n", *arg);
        pcap_close(nvi->pcap);
        aim_free(nvi);
        return -1;
    }

    arg++;
    if(*arg) {
        /*
         * if the next arg is 'mpls', packets will be encapsulated
         * in an MPLS frame with the outer fields indicating useful
         * information, like port number, send or recieved, etc
         */
        if(!VPI_STRCMP(*arg, "mpls")) {
            nvi->mpls = 1;
        }
        arg++;
        if(*arg) {
            /*
             * Set the label field
             */
            aim_strlcpy(nvi->ident, *arg, sizeof(nvi->ident));
        }
    }

    nvi->interface.send = vpi_pcapdump_interface_send;
    nvi->interface.recv = NULL;
    nvi->interface.recv_ready = NULL;
    nvi->interface.destroy = vpi_pcapdump_interface_destroy;
    nvi->interface.descriptor = NULL;

    *vi = (vpi_interface_t*)nvi;
    return 0;
}