int send_ip6_udp_any(int fd) { struct ofp_sockaddr_in6 addr = {0}; const char *buf = "socket_test"; addr.sin6_len = sizeof(struct ofp_sockaddr_in6); addr.sin6_family = OFP_AF_INET6; addr.sin6_port = odp_cpu_to_be_16(TEST_PORT + 1); addr.sin6_addr = ofp_in6addr_any; if (ofp_connect(fd, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in6)) == -1) { OFP_ERR("Faild to connect socket (errno = %d)\n", ofp_errno); return -1; } if (ofp_send(fd, buf, strlen(buf), 0) == -1) { OFP_ERR("Faild to send data(errno = %d)\n", ofp_errno); return -1; } OFP_INFO("Data (%s) sent successfully.\n", buf); OFP_INFO("SUCCESS.\n"); return 0; }
uma_zone_t ofp_uma_pool_create(const char *name, int nitems, int size) { odp_pool_param_t pool_params; odp_pool_t pool; uma_zone_t zone; pool_params.buf.size = size + sizeof(struct uma_pool_metadata); pool_params.buf.align = 0; pool_params.buf.num = nitems; pool_params.type = ODP_POOL_BUFFER; OFP_INFO("Creating pool '%s', nitems=%d size=%d total=%d", name, pool_params.buf.num, pool_params.buf.size, pool_params.buf.num * pool_params.buf.size); if (shm->num_pools >= OFP_NUM_UMA_POOLS) { OFP_ERR("Exceeded max number (%d) of pools", OFP_NUM_UMA_POOLS); return OFP_UMA_ZONE_INVALID; } pool = ofp_pool_create(name, &pool_params); if (pool == ODP_POOL_INVALID) { OFP_ERR("odp_pool_create failed"); return OFP_UMA_ZONE_INVALID; } zone = shm->num_pools++; shm->pools[zone] = pool; return zone; }
static int webserver(void *arg) { int serv_fd, tmp_fd; struct ofp_sockaddr_in my_addr; (void)arg; OFP_INFO("HTTP thread started"); if (ofp_init_local()) { OFP_ERR("Error: OFP local init failed.\n"); return -1; } sleep(1); myaddr = ofp_port_get_ipv4_addr(0, 0, OFP_PORTCONF_IP_TYPE_IP_ADDR); if ((serv_fd = ofp_socket(OFP_AF_INET, OFP_SOCK_STREAM, OFP_IPPROTO_TCP)) < 0) { OFP_ERR("ofp_socket failed"); perror("serv socket"); return -1; } memset(&my_addr, 0, sizeof(my_addr)); my_addr.sin_family = OFP_AF_INET; my_addr.sin_port = odp_cpu_to_be_16(2048); my_addr.sin_addr.s_addr = myaddr; my_addr.sin_len = sizeof(my_addr); if (ofp_bind(serv_fd, (struct ofp_sockaddr *)&my_addr, sizeof(struct ofp_sockaddr)) < 0) { OFP_ERR("Cannot bind http socket (%s)!", ofp_strerror(ofp_errno)); return -1; } ofp_listen(serv_fd, 10); #ifndef USE_EPOLL OFP_INFO("Using ofp_select"); ofp_fd_set read_fd; OFP_FD_ZERO(&read_fd); int nfds = serv_fd; #else OFP_INFO("Using ofp_epoll"); int epfd = ofp_epoll_create(1); struct ofp_epoll_event e = { OFP_EPOLLIN, { .fd = serv_fd } };
static int handle_connection(int i) { int fd, r; static char buf[1024]; if (connections[i].fd == 0) return 0; fd = connections[i].fd; r = ofp_recv(connections[i].fd, buf, sizeof(buf)-1, 0); if (r < 0) return 0; if (r > 0) { buf[r] = 0; OFP_INFO("recv data: %s", buf); if (!strncmp(buf, "GET", 3)) analyze_http(buf, connections[i].fd); else OFP_INFO("Not an HTTP GET request"); OFP_INFO("closing %d\n", connections[i].fd); while (ofp_close(connections[i].fd) < 0) { OFP_ERR("ofp_close failed, fd=%d err='%s'", connections[i].fd, ofp_strerror(ofp_errno)); sleep(1); } OFP_INFO("closed fd=%d", connections[i].fd); connections[i].fd = 0; } else if (r == 0) { if (connections[i].post) { OFP_INFO("File download finished"); fclose(connections[i].post); connections[i].post = NULL; } ofp_close(connections[i].fd); connections[i].fd = 0; } return fd; }
static void test_arp(void) { struct ofp_ifnet mock_ifnet; struct in_addr ip; uint8_t mac[OFP_ETHER_ADDR_LEN] = { 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, }; /* The buffer passed into ofp_ipv4_lookup_mac() must be 8 bytes since * a 64-bit operation is currently being used to copy a MAC address. */ uint8_t mac_result[OFP_ETHER_ADDR_LEN + 2]; CU_ASSERT(0 == ofp_init_local()); memset(&mock_ifnet, 0, sizeof(mock_ifnet)); CU_ASSERT(0 != inet_aton("1.1.1.1", &ip)); /* Test entry insert, lookup, and remove. */ CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); CU_ASSERT(0 == ofp_arp_ipv4_insert(ip.s_addr, mac, &mock_ifnet)); memset(mac_result, 0xFF, OFP_ETHER_ADDR_LEN); CU_ASSERT(0 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); CU_ASSERT(0 == memcmp(mac, mac_result, OFP_ETHER_ADDR_LEN)); CU_ASSERT(0 == ofp_arp_ipv4_remove(ip.s_addr, &mock_ifnet)); CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); /* Test entry is aged out. */ CU_ASSERT(0 == ofp_arp_ipv4_insert(ip.s_addr, mac, &mock_ifnet)); OFP_INFO("Inserted ARP entry"); sleep(ARP_AGE_INTERVAL + ARP_ENTRY_TIMEOUT); CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); /* Test entry is aged out after a few hits. */ CU_ASSERT(0 == ofp_arp_ipv4_insert(ip.s_addr, mac, &mock_ifnet)); OFP_INFO("Inserted ARP entry"); sleep(ARP_AGE_INTERVAL); CU_ASSERT(0 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); sleep(ARP_AGE_INTERVAL); CU_ASSERT(0 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); sleep(ARP_AGE_INTERVAL + ARP_ENTRY_TIMEOUT); CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); }
static void app_processing(void) { int fd_rcv = -1; char buf[1500]; int len = sizeof(buf); struct ofp_sockaddr_in addr = {0}; fd_rcv = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP); if (fd_rcv == -1) { OFP_ERR("Faild to create RCV socket (errno = %d)\n", ofp_errno); return; } addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_bind(fd_rcv, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to bind socket (errno = %d)\n", ofp_errno); return; } do { len = ofp_recv(fd_rcv, buf, len, 0); if (len == -1) { OFP_ERR("Faild to receive data (errno = %d)\n", ofp_errno); break; } OFP_INFO("Data received: length = %d.\n", len); } while (1); if (fd_rcv != -1) { ofp_close(fd_rcv); fd_rcv = -1; } OFP_INFO("Test ended.\n"); }
static int analyze_http(char *http, int s) { char *url; if (!strncmp(http, "GET ", 4)) { url = http + 4; while (*url == ' ') url++; char *p = strchr(url, ' '); if (p) *p = 0; else return -1; OFP_INFO("GET %s (fd=%d)", url, s); get_file(s, url); } else if (!strncmp(http, "POST ", 5)) { /* Post is not supported. */ OFP_INFO("%s", http); } return 0; }
int sendto_ip4_udp_local_ip(int fd) { struct ofp_sockaddr_in dest_addr = {0}; const char *buf = "socket_test"; dest_addr.sin_len = sizeof(struct ofp_sockaddr_in); dest_addr.sin_family = OFP_AF_INET; dest_addr.sin_port = odp_cpu_to_be_16(TEST_PORT); dest_addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_sendto(fd, buf, strlen(buf), 0, (struct ofp_sockaddr *)&dest_addr, sizeof(dest_addr)) == -1) { OFP_ERR("Faild to send data(errno = %d)\n", ofp_errno); return -1; } OFP_INFO("Data (%s) sent successfully.\n", buf); OFP_INFO("SUCCESS.\n"); return 0; }
int sendto_ip6_udp_local_ip(int fd) { struct ofp_sockaddr_in6 dest_addr = {0}; const char *buf = "socket_snd2"; dest_addr.sin6_len = sizeof(struct ofp_sockaddr_in6); dest_addr.sin6_family = OFP_AF_INET6; dest_addr.sin6_port = odp_cpu_to_be_16(TEST_PORT); inet_pton(AF_INET6, "fd00:1baf::1", (void *)&dest_addr.sin6_addr); if (ofp_sendto(fd, buf, strlen(buf), 0, (struct ofp_sockaddr *)&dest_addr, sizeof(dest_addr)) == -1) { OFP_ERR("Faild to send data(errno = %d)\n", ofp_errno); return -1; } OFP_INFO("Data (%s) sent successfully.\n", buf); OFP_INFO("SUCCESS.\n"); return 0; }
static inline int accept_connection(int serv_fd) { int tmp_fd, i; struct ofp_sockaddr_in caller; unsigned int alen = sizeof(caller); if ((tmp_fd = ofp_accept(serv_fd, (struct ofp_sockaddr *)&caller, &alen)) > 0) { OFP_INFO("accept fd=%d", tmp_fd); for (i = 0; i < NUM_CONNECTIONS; i++) if (connections[i].fd == 0) break; if (i >= NUM_CONNECTIONS) { OFP_ERR("Node cannot accept new connections!"); ofp_close(tmp_fd); return -1; } #if 0 struct ofp_linger so_linger; so_linger.l_onoff = 1; so_linger.l_linger = 0; int r1 = ofp_setsockopt(tmp_fd, OFP_SOL_SOCKET, OFP_SO_LINGER, &so_linger, sizeof so_linger); if (r1) OFP_ERR("SO_LINGER failed!"); #endif struct ofp_timeval tv; tv.tv_sec = 3; tv.tv_usec = 0; int r2 = ofp_setsockopt(tmp_fd, OFP_SOL_SOCKET, OFP_SO_SNDTIMEO, &tv, sizeof tv); if (r2) OFP_ERR("SO_SNDTIMEO failed!"); connections[i].fd = tmp_fd; connections[i].addr = caller.sin_addr.s_addr; connections[i].closed = FALSE; } return tmp_fd; }
int ofp_init_global(ofp_init_global_t *params) { int i; HANDLE_ERROR(ofp_init_pre_global(NULL, NULL, params->pkt_hook, NULL, ARP_AGE_INTERVAL, ARP_ENTRY_TIMEOUT)); /* cpu mask for slow path threads */ odp_cpumask_zero(&cpumask); odp_cpumask_set(&cpumask, params->linux_core_id); OFP_INFO("Slow path threads on core %d", odp_cpumask_first(&cpumask)); HANDLE_ERROR(ofp_set_vxlan_interface_queue()); /* Create interfaces */ for (i = 0; i < params->if_count; ++i) HANDLE_ERROR(ofp_ifnet_create(params->if_names[i], params->burst_recv_mode ? ODP_PKTIN_MODE_RECV : ODP_PKTIN_MODE_SCHED)); #ifdef SP /* Start Netlink server process */ if (!ofp_linux_pthread_create(&shm->nl_thread, &cpumask, START_NL_SERVER, NULL, ODP_THREAD_CONTROL)) { OFP_ERR("Failed to start Netlink thread."); return -1; } shm->nl_thread_is_running = 1; #endif /* SP */ odp_schedule_resume(); return 0; }
int main(void) { static ofp_init_global_t oig; odp_instance_t instance; if (odp_init_global(&instance, NULL, NULL)) { OFP_ERR("Error: ODP global init failed.\n"); exit(EXIT_FAILURE); } if (odp_init_local(instance, ODP_THREAD_CONTROL)) { OFP_ERR("Error: ODP local init failed.\n"); exit(EXIT_FAILURE); } if (ofp_init_global(instance, &oig)) { OFP_ERR("Error: OFP global init failed.\n"); exit(EXIT_FAILURE); } OFP_INFO("Init successful.\n"); return 0; }
static odp_cos_t build_cos_w_queue(const char *name) { odp_cos_t cos; odp_queue_t queue_cos; odp_queue_param_t qparam; cos = odp_cos_create(name); if (cos == ODP_COS_INVALID) { OFP_ERR("Failed to create COS"); return ODP_COS_INVALID; } memset(&qparam, 0, sizeof(odp_queue_param_t)); qparam.sched.prio = ODP_SCHED_PRIO_DEFAULT; qparam.sched.sync = ODP_SCHED_SYNC_ATOMIC; qparam.sched.group = ODP_SCHED_GROUP_ALL; queue_cos = odp_queue_create(name, ODP_QUEUE_TYPE_SCHED, &qparam); if (queue_cos == ODP_QUEUE_INVALID) { OFP_ERR("Failed to create queue\n"); odp_cos_destroy(cos); return ODP_COS_INVALID; } #if ODP_VERSION < 104 if (odp_cos_set_queue(cos, queue_cos) < 0) { #else if (odp_cos_queue_set(cos, queue_cos) < 0) { #endif OFP_ERR("Failed to set queue on COS"); odp_cos_destroy(cos); odp_queue_destroy(queue_cos); return ODP_COS_INVALID; } return cos; } static odp_cos_t build_cos_set_queue(const char *name, odp_queue_t queue_cos) { odp_cos_t cos; cos = odp_cos_create(name); if (cos == ODP_COS_INVALID) { OFP_ERR("Failed to create COS"); return ODP_COS_INVALID; } #if ODP_VERSION < 104 if (odp_cos_set_queue(cos, queue_cos) < 0) { #else if (odp_cos_queue_set(cos, queue_cos) < 0) { #endif OFP_ERR("Failed to set queue on COS"); odp_cos_destroy(cos); return ODP_COS_INVALID; } return cos; } static odp_pmr_t build_udp_prm(void) { uint32_t pmr_udp_val = TEST_PORT; uint32_t pmr_udp_mask = 0xffffffff; #if ODP_VERSION < 104 return odp_pmr_create(ODP_PMR_UDP_DPORT, &pmr_udp_val, &pmr_udp_mask, 1); #else const odp_pmr_match_t match = { .term = ODP_PMR_UDP_DPORT, .val = &pmr_udp_val, .mask = &pmr_udp_mask, .val_sz = 1 }; return odp_pmr_create(&match); #endif } static void app_processing(void) { int fd_rcv = -1; char buf[1500]; int len = sizeof(buf); do { struct ofp_sockaddr_in addr = {0}; fd_rcv = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP); if (fd_rcv == -1) { OFP_ERR("Faild to create RCV socket (errno = %d)\n", ofp_errno); break; } addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_bind(fd_rcv, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to bind socket (errno = %d)\n", ofp_errno); break; } len = ofp_recv(fd_rcv, buf, len, 0); if (len == -1) OFP_ERR("Faild to receive data (errno = %d)\n", ofp_errno); else OFP_INFO("Data received: length = %d.\n", len); } while (0); if (fd_rcv != -1) { ofp_close(fd_rcv); fd_rcv = -1; } OFP_INFO("Test ended.\n"); }
int ofp_term_global(void) { int rc = 0; uint16_t i; struct ofp_ifnet *ifnet; ofp_stop(); /* Terminate CLI thread*/ CHECK_ERROR(ofp_stop_cli_thread(), rc); #ifdef SP /* Terminate Netlink thread*/ if (shm->nl_thread_is_running) { odph_linux_pthread_join(&shm->nl_thread, 1); shm->nl_thread_is_running = 0; } #endif /* SP */ /* Cleanup interfaces: queues and pktios*/ for (i = 0; i < VXLAN_PORTS; i++) { ifnet = ofp_get_ifnet((uint16_t)i, 0); if (!ifnet) { OFP_ERR("Failed to locate interface for port %d", i); rc = -1; continue; } if (ifnet->if_state == OFP_IFT_STATE_FREE) continue; if (ifnet->pktio == ODP_PKTIO_INVALID) continue; OFP_INFO("Cleaning device '%s' addr %s", ifnet->if_name, ofp_print_mac((uint8_t *)ifnet->mac)); CHECK_ERROR(odp_pktio_stop(ifnet->pktio), rc); #ifdef SP close(ifnet->fd); odph_linux_pthread_join(ifnet->rx_tbl, 1); odph_linux_pthread_join(ifnet->tx_tbl, 1); ifnet->fd = -1; #endif /*SP*/ /* Multicasting. */ ofp_igmp_domifdetach(ifnet); ifnet->ii_inet.ii_igmp = NULL; if (ifnet->loopq_def != ODP_QUEUE_INVALID) { if (odp_queue_destroy(ifnet->loopq_def) < 0) { OFP_ERR("Failed to destroy loop queue for %s", ifnet->if_name); rc = -1; } ifnet->loopq_def = ODP_QUEUE_INVALID; } #ifdef SP if (ifnet->spq_def != ODP_QUEUE_INVALID) { cleanup_pkt_queue(ifnet->spq_def); if (odp_queue_destroy(ifnet->spq_def) < 0) { OFP_ERR("Failed to destroy slow path " "queue for %s", ifnet->if_name); rc = -1; } ifnet->spq_def = ODP_QUEUE_INVALID; } #endif /*SP*/ ifnet->outq_def = ODP_QUEUE_INVALID; if (ifnet->pktio != ODP_PKTIO_INVALID) { if (odp_pktio_close(ifnet->pktio) < 0) { OFP_ERR("Failed to destroy pktio for %s", ifnet->if_name); rc = -1; } ifnet->pktio = ODP_PKTIO_INVALID; } if (ifnet->inq_def != ODP_QUEUE_INVALID) { cleanup_pkt_queue(ifnet->inq_def); if (odp_queue_destroy(ifnet->inq_def) < 0) { OFP_ERR("Failed to destroy default input " "queue for %s", ifnet->if_name); rc = -1; } ifnet->inq_def = ODP_QUEUE_INVALID; } } CHECK_ERROR(ofp_clean_vxlan_interface_queue(), rc); if (ofp_term_post_global(SHM_PACKET_POOL_NAME)) { OFP_ERR("Failed to cleanup resources\n"); rc = -1; } return rc; }
/** main() Application entry point * * @param argc int * @param argv[] char* * @return int * */ int main(int argc, char *argv[]) { odph_linux_pthread_t thread_tbl[MAX_WORKERS]; appl_args_t params; int core_count, num_workers; odp_cpumask_t cpumask; char cpumaskstr[64]; /* Parse and store the application arguments */ parse_args(argc, argv, ¶ms); /* Print both system and application information */ print_info(NO_PATH(argv[0]), ¶ms); if (odp_init_global(NULL, NULL)) { OFP_ERR("Error: ODP global init failed.\n"); exit(EXIT_FAILURE); } if (odp_init_local(ODP_THREAD_CONTROL)) { OFP_ERR("Error: ODP local init failed.\n"); exit(EXIT_FAILURE); } core_count = odp_cpu_count(); num_workers = core_count; if (params.core_count) num_workers = params.core_count; if (num_workers > MAX_WORKERS) num_workers = MAX_WORKERS; if (core_count > 1) num_workers--; num_workers = odp_cpumask_default_worker(&cpumask, num_workers); odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); printf("Num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); printf("cpu mask: %s\n", cpumaskstr); memset(&app_init_params, 0, sizeof(app_init_params)); app_init_params.linux_core_id = 0; app_init_params.if_count = params.if_count; app_init_params.if_names = params.if_names; if (ofp_init_global(&app_init_params)) { OFP_ERR("Error: OFP global init failed.\n"); exit(EXIT_FAILURE); } memset(thread_tbl, 0, sizeof(thread_tbl)); /* Start dataplane dispatcher worker threads */ ofp_linux_pthread_create(thread_tbl, &cpumask, default_event_dispatcher, ofp_eth_vlan_processing, ODP_THREAD_CONTROL ); /* other app code here.*/ /* Start CLI */ ofp_start_cli_thread(app_init_params.linux_core_id, params.conf_file); sleep(5); ofp_loglevel = OFP_LOG_INFO; config_suite_framework(app_init_params.linux_core_id); OFP_INFO("\n\nSuite: IPv4 UDP socket: create and close.\n\n"); if (!init_suite(NULL)) run_suite(create_close_udp, create_close_udp_noproto); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 TCP socket: create and close.\n\n"); if (!init_suite(NULL)) run_suite(create_close_tcp, create_close_tcp_noproto); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP socket: create and close.\n\n"); if (!init_suite(NULL)) run_suite(create_close_udp6, create_close_udp6_noproto); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 TCP socket: create and close.\n\n"); if (!init_suite(NULL)) run_suite(create_close_tcp6, create_close_tcp6_noproto); end_suite(); OFP_INFO("Test ended.\n"); #endif /* INET6 */ OFP_INFO("\n\nSuite: IPv4 UDP socket: bind.\n\n"); if (!init_suite(init_udp_create_socket)) run_suite(bind_ip4_local_ip, bind_ip4_any); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 TCP socket: bind.\n\n"); if (!init_suite(init_tcp_create_socket)) run_suite(bind_ip4_local_ip, bind_ip4_any); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP socket: bind.\n\n"); if (!init_suite(init_udp6_create_socket)) run_suite(bind_ip6_local_ip, bind_ip6_any); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 TCP socket: bind.\n\n"); if (!init_suite(init_tcp6_create_socket)) run_suite(bind_ip6_local_ip, bind_ip6_any); end_suite(); OFP_INFO("Test ended.\n"); #endif /* INET6 */ OFP_INFO("\n\nSuite: IPv4 UDP socket: shutdown.\n\n"); if (!init_suite(init_udp_create_socket)) run_suite(shutdown_socket, shutdown_socket); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 TCP socket: shutdown (no connection).\n\n"); if (!init_suite(init_tcp_create_socket)) run_suite(shutdown_socket, shutdown_socket); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP socket: shutdown.\n\n"); if (!init_suite(init_udp6_create_socket)) run_suite(shutdown_socket, shutdown_socket); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 TCP socket: shutdown (no connection).\n\n"); if (!init_suite(init_tcp6_create_socket)) run_suite(shutdown_socket, shutdown_socket); end_suite(); OFP_INFO("Test ended.\n"); #endif /* INET6 */ OFP_INFO("\n\nSuite: IPv4 UDP socket: connect.\n\n"); if (!init_suite(init_udp_create_socket)) run_suite(connect_udp4, connect_bind_udp4); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 UDP socket: connect + shutdown.\n\n"); if (!init_suite(init_udp_create_socket)) run_suite(connect_shutdown_udp4, connect_shutdown_bind_udp4); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP socket: connect.\n\n"); if (!init_suite(init_udp6_create_socket)) run_suite(connect_udp6, connect_bind_udp6); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 UDP socket: connect + shutdown.\n\n"); if (!init_suite(init_udp6_create_socket)) run_suite(connect_shutdown_udp6, connect_shutdown_bind_udp6); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 UDP socket: connect + shutdown + any.\n\n"); if (!init_suite(init_udp6_create_socket)) run_suite(connect_shutdown_udp6_any, connect_shutdown_bind_udp6_any); end_suite(); OFP_INFO("Test ended.\n"); #endif /* INET6 */ OFP_INFO("\n\nSuite: IPv4 UDP socket BIND local address: send + sendto\n\n"); if (!init_suite(init_udp_bind_local_ip)) run_suite(send_ip4_udp_local_ip, sendto_ip4_udp_local_ip); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 UDP socket bind any address: send + sendto\n\n"); if (!init_suite(init_udp_bind_any)) run_suite(send_ip4_udp_any, sendto_ip4_udp_any); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP socket BIND local address: send + sendto\n\n"); if (!init_suite(init_udp6_bind_local_ip)) run_suite(send_ip6_udp_local_ip, sendto_ip6_udp_local_ip); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 UDP socket bind any address: send + sendto\n\n"); if (!init_suite(init_udp6_bind_any)) run_suite(send_ip6_udp_any, sendto_ip6_udp_any); end_suite(); OFP_INFO("Test ended.\n"); #endif /* INET6 */ OFP_INFO("\n\nSuite: IPv4 UDP bind local IP: sendto + recv.\n\n"); if (!init_suite(init_udp_local_ip)) run_suite(send_udp_local_ip, recv_udp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 UDP bind local IP: sendto + recvfrom.\n\n"); if (!init_suite(init_udp_bind_local_ip)) run_suite(send_udp_local_ip, recvfrom_udp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 UDP bind any address: sendto + recv.\n\n"); if (!init_suite(init_udp_any)) run_suite(send_udp_any, recv_udp); end_suite(); OFP_INFO("\n\nSuite: IPv4 UDP bind any address: sendto + recvfrom.\n\n"); if (!init_suite(init_udp_bind_any)) run_suite(send_udp_any, recvfrom_udp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 UDP bind any address: sendto + recvfrom(NULL addr).\n\n"); if (!init_suite(init_udp_bind_any)) run_suite(send_udp_any, recvfrom_udp_null_addr); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP bind local IP: sendto + recv.\n\n"); if (!init_suite(init_udp6_bind_local_ip)) run_suite(send_udp6_local_ip, recv_udp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 UDP bind local IP: sendto + recvfrom.\n\n"); if (!init_suite(init_udp6_bind_local_ip)) run_suite(send_udp6_local_ip, recvfrom_udp6); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 UDP bind any IP: sendto + recv.\n\n"); if (!init_suite(init_udp6_bind_any)) run_suite(send_udp6_any, recv_udp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 UDP bind any IP: sendto + recvfrom.\n\n"); if (!init_suite(init_udp6_bind_any)) run_suite(send_udp6_any, recvfrom_udp6); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 UDP bind any IP: sendto + recvfrom(NULL addr).\n\n"); if (!init_suite(init_udp6_bind_any)) run_suite(send_udp6_any, recvfrom_udp_null_addr); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ OFP_INFO("\n\nSuite: IPv4 TCP socket local IP: listen.\n\n"); if (!init_suite(init_tcp_bind_local_ip)) run_suite(listen_tcp, listen_tcp); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 TCP socket local IP: listen.\n\n"); if (!init_suite(init_tcp6_bind_local_ip)) run_suite(listen_tcp, listen_tcp); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ OFP_INFO("\n\nSuite: IPv4 TCP socket local IP: connect + accept.\n\n"); if (!init_suite(init_tcp_bind_listen_local_ip)) run_suite(connect_tcp4_local_ip, accept_tcp4); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 TCP socket any IP: connect + accept.\n\n"); if (!init_suite(init_tcp_bind_listen_any)) run_suite(connect_tcp4_any, accept_tcp4); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 TCP socket local IP: connect + accept null address.\n\n"); if (!init_suite(init_tcp_bind_listen_local_ip)) run_suite(connect_tcp4_local_ip, accept_tcp4_null_addr); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 TCP socket local IP: connect + accept.\n\n"); if (!init_suite(init_tcp6_bind_listen_local_ip)) run_suite(connect_tcp6_local_ip, accept_tcp6); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 TCP socket any IP: connect + accept.\n\n"); if (!init_suite(init_tcp6_bind_listen_any)) run_suite(connect_tcp6_any, accept_tcp6); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 TCP socket local IP: connect + accept null address.\n\n"); if (!init_suite(init_tcp6_bind_listen_local_ip)) run_suite(connect_tcp6_local_ip, accept_tcp6_null_addr); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ OFP_INFO("\n\nSuite: IPv4 TCP socket local IP: send + recv.\n\n"); if (!init_suite(init_tcp_bind_listen_local_ip)) run_suite(send_tcp4_local_ip, receive_tcp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 TCP socket any IP: send + recv.\n\n"); if (!init_suite(init_tcp_bind_listen_any)) run_suite(send_tcp4_any, receive_tcp); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 TCP socket local IP: send + recv.\n\n"); if (!init_suite(init_tcp6_bind_listen_local_ip)) run_suite(send_tcp6_local_ip, receive_tcp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 TCP socket any IP: send + recv.\n\n"); if (!init_suite(init_tcp6_bind_listen_any)) run_suite(send_tcp6_any, receive_tcp); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ OFP_INFO("\n\nSuite: IPv4 UDP bind local IP: select + recv.\n\n"); if (!init_suite(init_udp_bind_local_ip)) run_suite(send_udp_local_ip, select_recv_udp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 TCP bind local IP: select + accept + recv.\n\n"); if (!init_suite(init_tcp_bind_listen_local_ip)) run_suite(send_tcp4_local_ip, select_recv_tcp); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP bind local IP: select + recv.\n\n"); if (!init_suite(init_udp6_bind_local_ip)) run_suite(send_udp6_local_ip, select_recv_udp); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv6 TCP bind local IP: select + accept + recv.\n\n"); if (!init_suite(init_tcp6_bind_listen_local_ip)) run_suite(send_tcp6_local_ip, select_recv_tcp); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ OFP_INFO("\n\nSuite: IPv4 UDP bindlocal IP: select + recv x2.\n\n"); if (!init_suite(init_udp_bind_local_ip)) run_suite(send_udp_local_ip, select_recv_udp_2); end_suite(); OFP_INFO("Test ended.\n"); OFP_INFO("\n\nSuite: IPv4 UDP bind local IP: socket_sigevent rcv.\n\n"); if (!init_suite(init_udp_bind_local_ip)) run_suite(recv_send_udp_local_ip, socket_sigevent_udp4); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 UDP bind local IP: socket_sigevent rcv.\n\n"); if (!init_suite(init_udp6_bind_local_ip)) run_suite(recv_send_udp6_local_ip, socket_sigevent_udp6); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ OFP_INFO("\n\nSuite: IPv4 TCP bind local IP: socket_sigevent rcv.\n\n"); if (!init_suite(init_tcp_bind_listen_local_ip)) run_suite(connect_recv_send_tcp_local_ip, socket_sigevent_tcp_rcv); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 TCP bind local IP: socket_sigevent rcv.\n\n"); if (!init_suite(init_tcp6_bind_listen_local_ip)) run_suite(connect_recv_send_tcp6_local_ip, socket_sigevent_tcp_rcv); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ OFP_INFO("\n\nSuite: IPv4 TCP bind local IP: socket_sigevent accept.\n\n"); if (!init_suite(init_tcp_bind_listen_local_ip)) run_suite(connect_tcp_delayed_local_ip, socket_sigevent_tcp_accept); end_suite(); OFP_INFO("Test ended.\n"); #ifdef INET6 OFP_INFO("\n\nSuite: IPv6 TCP bind local IP: socket_sigevent accept.\n\n"); if (!init_suite(init_tcp6_bind_listen_local_ip)) run_suite(connect_tcp6_delayed_local_ip, socket_sigevent_tcp_accept); end_suite(); OFP_INFO("Test ended.\n"); #endif /*INET6*/ odph_linux_pthread_join(thread_tbl, num_workers); printf("End Main()\n"); return 0; }
static void * sysctl(void *arg) { (void)arg; if (odp_init_local(ODP_THREAD_CONTROL)) { OFP_ERR("Error: ODP local init failed.\n"); return NULL; } if (ofp_init_local()) { OFP_ERR("Error: OFP local init failed.\n"); return NULL; } sleep(2); /* * Variables may be visible per thread. Addresses of the shared * variables are not known at compile time. Also sometimes it may be * necessary to create OIDs dynamically. * * Add an OID dynamically to the existing compile time * created branch: */ static int created; OFP_SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_mybranch), OFP_OID_AUTO, "created", OFP_CTLFLAG_RW, &created, 0, "Dynamically created"); /* * Create a branch dynamically: */ struct ofp_sysctl_oid *dyn_root; dyn_root = OFP_SYSCTL_ADD_NODE (NULL, SYSCTL_STATIC_CHILDREN(_mybranch), OFP_OID_AUTO, "subbranch", OFP_CTLFLAG_RW, 0, "Dynamically created branch"); /* * Add a variable to that, for example one from the shared memory. * Here we use a static integer. */ static int shared; OFP_SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(dyn_root), OFP_OID_AUTO, "shared", OFP_CTLFLAG_RW, &shared, 0, "Shared memory variable"); /* * Our branch is complete: * * 73 mybranch RW Node (My test branch) * 256 hello RW string (Hello message) * 261 ssh RW Node (Ssh control) * 257 counter R int64_t (Ssh counter) * 259 enabled RW int (Enable ssh protocol) * 262 telnet RW Node (Telnet control) * 258 counter R int64_t (Telnet counter) * 260 enabled RW int (Enable telnet protocol) * 328 created RW int (Dynamically created) * 329 subbranch RW Node (Dynamically created branch) * 330 shared RW int (Shared memory variable) */ /* * Use created variables. First set some meaningful values: */ telnet_bytes = 123456; ssh_bytes = 567890; strcpy(hello_msg, "Hello, world!"); /* * There are several functions to access MIB data. Simplest one * is the following: * * ofp_sysctl(const char *name, void *old, size_t *oldlenp, * const void *new, size_t newlen, size_t *retval) * * name: OID using string notation (like "net.inet.udp.checksum"). * old: Pointer to memory where old value will be saved. * Can be NULL. * oldlenp: Pointer to variable whose value is the result space * in bytes. Will be updated to the real space. * new: Pointer to the new value. Can be NULL. * newlen: Size of the new value in bytes or zero. * retval: Pointer to a variable that will be set to * response's length. */ /* * Read the telnet bytes: */ uint64_t counter; size_t counterlen = sizeof(counter); size_t retval; ofp_sysctl("mybranch.telnet.counter", &counter, &counterlen, NULL, 0, &retval); OFP_INFO("mybranch.telnet.counter=%"PRIu64" len=%zu retval=%zu\n", counter, counterlen, retval); /* * Read the ssh bytes: */ ofp_sysctl("mybranch.ssh.counter", &counter, &counterlen, NULL, 0, &retval); OFP_INFO("mybranch.ssh.counter=%"PRIu64" len=%zu retval=%zu\n", counter, counterlen, retval); /* * Check if telnet is enabled: */ int enabled; size_t enalen = sizeof(enabled); ofp_sysctl("mybranch.telnet.enabled", &enabled, &enalen, NULL, 0, &retval); OFP_INFO("mybranch.telnet.enabled=%d\n", enabled); /* * Disable telnet: */ enabled = 0; ofp_sysctl("mybranch.telnet.enabled", NULL, 0, &enabled, sizeof(enabled), &retval); /* * Check if that worked. Init variable with something to ensure it is * really changed: */ enabled = 123; enalen = sizeof(enabled); ofp_sysctl("mybranch.telnet.enabled", &enabled, &enalen, NULL, 0, &retval); OFP_INFO("After disabling: mybranch.telnet.enabled=%d, real value=%d\n", enabled, enable_telnet); /* * Read and change the hello message: */ char msg[32]; size_t msglen = sizeof(msg); ofp_sysctl("mybranch.hello", msg, &msglen, "Server is down.", 16, &retval); OFP_INFO("mybranch.hello: old value=%s, new value=%s\n", msg, hello_msg); /* * Make telnet connection to local address port 2345. * Try commands: * sysctl dump * sysctl r mybranch.ssh.counter * sysctl w mybranch.ssh.enabled 1 * sysctl w mybranch.ssh.counter 777 */ while (1) sleep(1); return NULL; }
void *pp_thread(void *arg) { ALLOW_UNUSED_LOCAL(arg); #if ODP_VERSION >= 102 if (odp_init_local(ODP_THREAD_WORKER)) { #else if (odp_init_local()) { #endif OFP_ERR("odp_init_local failed"); return NULL; } if (ofp_init_local()) { OFP_ERR("ofp_init_local failed"); return NULL; } while (odp_atomic_load_u32(&still_running)) { odp_event_t event; odp_queue_t source_queue; event = odp_schedule(&source_queue, ODP_SCHED_WAIT); if (odp_event_type(event) != ODP_EVENT_TIMEOUT) { OFP_ERR("Unexpected event type %d", odp_event_type(event)); continue; } ofp_timer_handle(event); } return NULL; } static void test_arp(void) { struct ofp_ifnet mock_ifnet; struct in_addr ip; uint8_t mac[OFP_ETHER_ADDR_LEN] = { 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, }; /* The buffer passed into ofp_ipv4_lookup_mac() must be 8 bytes since * a 64-bit operation is currently being used to copy a MAC address. */ uint8_t mac_result[OFP_ETHER_ADDR_LEN + 2]; CU_ASSERT(0 == ofp_init_local()); memset(&mock_ifnet, 0, sizeof(mock_ifnet)); CU_ASSERT(0 != inet_aton("1.1.1.1", &ip)); /* Test entry insert, lookup, and remove. */ CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); CU_ASSERT(0 == ofp_arp_ipv4_insert(ip.s_addr, mac, &mock_ifnet)); memset(mac_result, 0xFF, OFP_ETHER_ADDR_LEN); CU_ASSERT(0 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); CU_ASSERT(0 == memcmp(mac, mac_result, OFP_ETHER_ADDR_LEN)); CU_ASSERT(0 == ofp_arp_ipv4_remove(ip.s_addr, &mock_ifnet)); CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); /* Test entry is aged out. */ CU_ASSERT(0 == ofp_arp_ipv4_insert(ip.s_addr, mac, &mock_ifnet)); OFP_INFO("Inserted ARP entry"); sleep(ARP_AGE_INTERVAL + ARP_ENTRY_TIMEOUT); CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); /* Test entry is aged out after a few hits. */ CU_ASSERT(0 == ofp_arp_ipv4_insert(ip.s_addr, mac, &mock_ifnet)); OFP_INFO("Inserted ARP entry"); sleep(ARP_AGE_INTERVAL); CU_ASSERT(0 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); sleep(ARP_AGE_INTERVAL); CU_ASSERT(0 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); sleep(ARP_AGE_INTERVAL + ARP_ENTRY_TIMEOUT); CU_ASSERT(-1 == ofp_ipv4_lookup_mac(ip.s_addr, mac_result, &mock_ifnet)); } int main(void) { CU_pSuite ptr_suite = NULL; int nr_of_failed_tests = 0; int nr_of_failed_suites = 0; /* Initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); /* add a suite to the registry */ ptr_suite = CU_add_suite("ofp errno", init_suite, end_suite); if (NULL == ptr_suite) { CU_cleanup_registry(); return CU_get_error(); } if (NULL == CU_ADD_TEST(ptr_suite, test_arp)) { CU_cleanup_registry(); return CU_get_error(); } #if defined(OFP_TESTMODE_AUTO) CU_set_output_filename("CUnit-Util"); CU_automated_run_tests(); #else /* Run all tests using the CUnit Basic interface */ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); #endif nr_of_failed_tests = CU_get_number_of_tests_failed(); nr_of_failed_suites = CU_get_number_of_suites_failed(); CU_cleanup_registry(); return (nr_of_failed_suites > 0 ? nr_of_failed_suites : nr_of_failed_tests); }
static void *webserver(void *arg) { int serv_fd, tmp_fd, nfds; unsigned int alen; struct ofp_sockaddr_in my_addr, caller; ofp_fd_set read_fd; (void)arg; OFP_INFO("HTTP thread started"); if (ofp_init_local()) { OFP_ERR("Error: OFP local init failed.\n"); return NULL; } sleep(1); myaddr = ofp_port_get_ipv4_addr(0, 0, OFP_PORTCONF_IP_TYPE_IP_ADDR); if ((serv_fd = ofp_socket(OFP_AF_INET, OFP_SOCK_STREAM, OFP_IPPROTO_TCP)) < 0) { OFP_ERR("ofp_socket failed"); perror("serv socket"); return NULL; } memset(&my_addr, 0, sizeof(my_addr)); my_addr.sin_family = OFP_AF_INET; my_addr.sin_port = odp_cpu_to_be_16(2048); my_addr.sin_addr.s_addr = myaddr; my_addr.sin_len = sizeof(my_addr); if (ofp_bind(serv_fd, (struct ofp_sockaddr *)&my_addr, sizeof(struct ofp_sockaddr)) < 0) { OFP_ERR("Cannot bind http socket (%s)!", ofp_strerror(ofp_errno)); return 0; } ofp_listen(serv_fd, 10); OFP_FD_ZERO(&read_fd); nfds = serv_fd; for ( ; ; ) { int r, i; static char buf[1024]; struct ofp_timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 200000; OFP_FD_SET(serv_fd, &read_fd); monitor_connections(&read_fd); r = ofp_select(nfds + 1, &read_fd, NULL, NULL, &timeout); if (r <= 0) continue; if (OFP_FD_ISSET(serv_fd, &read_fd)) { alen = sizeof(caller); if ((tmp_fd = ofp_accept(serv_fd, (struct ofp_sockaddr *)&caller, &alen)) > 0) { OFP_INFO("accept fd=%d", tmp_fd); for (i = 0; i < NUM_CONNECTIONS; i++) if (connections[i].fd == 0) break; if (i >= NUM_CONNECTIONS) { OFP_ERR("Node cannot accept new connections!"); ofp_close(tmp_fd); continue; } #if 0 struct ofp_linger so_linger; so_linger.l_onoff = 1; so_linger.l_linger = 0; int r1 = ofp_setsockopt(tmp_fd, OFP_SOL_SOCKET, OFP_SO_LINGER, &so_linger, sizeof so_linger); if (r1) OFP_ERR("SO_LINGER failed!"); #endif struct ofp_timeval tv; tv.tv_sec = 3; tv.tv_usec = 0; int r2 = ofp_setsockopt(tmp_fd, OFP_SOL_SOCKET, OFP_SO_SNDTIMEO, &tv, sizeof tv); if (r2) OFP_ERR("SO_SNDTIMEO failed!"); connections[i].fd = tmp_fd; connections[i].addr = caller.sin_addr.s_addr; connections[i].closed = FALSE; if (tmp_fd > nfds) nfds = tmp_fd; } } for (i = 0; i < NUM_CONNECTIONS; i++) { if (connections[i].fd == 0) continue; if (!(OFP_FD_ISSET(connections[i].fd, &read_fd))) continue; r = ofp_recv(connections[i].fd, buf, sizeof(buf)-1, 0); if (r > 0) { buf[r] = 0; OFP_INFO("recv data: %s", buf); if (!strncmp(buf, "GET", 3)) analyze_http(buf, connections[i].fd); else OFP_INFO("Not a HTTP GET request"); OFP_INFO("closing %d\n", connections[i].fd); OFP_FD_CLR(connections[i].fd, &read_fd); while (ofp_close(connections[i].fd) < 0) { OFP_ERR("ofp_close failed, fd=%d err='%s'", connections[i].fd, ofp_strerror(ofp_errno)); sleep(1); } OFP_INFO("closed fd=%d", connections[i].fd); connections[i].fd = 0; } else if (r == 0) { if (connections[i].post) { OFP_INFO("File download finished"); fclose(connections[i].post); connections[i].post = NULL; } ofp_close(connections[i].fd); OFP_FD_CLR(connections[i].fd, &read_fd); connections[i].fd = 0; } } } OFP_INFO("httpd exiting"); return NULL; }
static void *mcasttest(void *arg) { int fd; struct ofp_sockaddr_in my_addr; struct ofp_ip_mreq mreq; (void)arg; logprint("Multicast thread started\n"); if (odp_init_local(ODP_THREAD_CONTROL)) { OFP_ERR("Error: ODP local init failed.\n"); return NULL; } if (ofp_init_local()) { OFP_ERR("Error: OFP local init failed.\n"); return NULL; } sleep(1); while (myaddr == 0) { myaddr = ofp_port_get_ipv4_addr(0, 0, OFP_PORTCONF_IP_TYPE_IP_ADDR); sleep(1); } if ((fd = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP)) < 0) { perror("socket"); logprint("Cannot open socket!\n"); return NULL; } memset(&my_addr, 0, sizeof(my_addr)); my_addr.sin_family = OFP_AF_INET; my_addr.sin_port = odp_cpu_to_be_16(2048); my_addr.sin_addr.s_addr = 0; my_addr.sin_len = sizeof(my_addr); if (ofp_bind(fd, (struct ofp_sockaddr *)&my_addr, sizeof(struct ofp_sockaddr)) < 0) { logprint("Cannot bind socket (%s)!\n", ofp_strerror(ofp_errno)); return NULL; } memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,5,5,5); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,7,7,7); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } for (;;) { char buf[100]; int len = sizeof(buf); struct ofp_sockaddr_in addr = {0}; ofp_socklen_t addr_len = 0; len = ofp_recvfrom(fd, buf, len, 0, (struct ofp_sockaddr *)&addr, &addr_len); if (len == -1) { OFP_ERR("Faild to rcv data(errno = %d)\n", ofp_errno); continue; } buf[len] = 0; OFP_INFO("Data (%s, len = %d) was received.\n", buf, len); if (addr_len != sizeof(addr)) { OFP_ERR("Faild to rcv source address: %d (errno = %d)\n", addr_len, ofp_errno); continue; } if (strstr(buf, "add")) { OFP_INFO("Add membership to 234.7.7.7\n"); memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,7,7,7); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } } else if (strstr(buf, "drop")) { OFP_INFO("Drop membership from 234.7.7.7\n"); memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,7,7,7); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } } else if (strstr(buf, "quit")) { exit(0); } OFP_INFO("Data was received from address 0x%x, port = %d.\n", odp_be_to_cpu_32(addr.sin_addr.s_addr), odp_be_to_cpu_16(addr.sin_port)); sprintf(buf, "%d bytes\n", len); if (ofp_sendto(fd, buf, strlen(buf), 0, (struct ofp_sockaddr *)&addr, sizeof(addr)) == -1) { OFP_ERR("Faild to send data (errno = %d)\n", ofp_errno); } } logprint("mcast exit\n"); return NULL; }