int main(int argc, char **argv) { /** * Simple example of firewall between two NICs. * Print brick simply show packets. * * [nic-west]--[print-west]--[firewall]--[print-east]--[nic_east] */ struct pg_error *error = NULL; struct pg_brick *nic_west, *nic_east; struct pg_brick *print_west, *print_east; struct pg_brick *fw; struct pg_graph *graph; /* init packetgraph and nics */ pg_start(argc, argv, &error); if (pg_nic_port_count() < 2) { printf("You need two DPDK ports to run this example\n"); return 1; } /* create bricks */ nic_west = pg_nic_new_by_id("nic-west", 0, &error); fw = pg_firewall_new("fw", PG_NO_CONN_WORKER, &error); print_west = pg_print_new("print-west", 0, PG_PRINT_FLAG_MAX, 0, &error); print_east = pg_print_new("print-east", 0, PG_PRINT_FLAG_MAX, 0, &error); nic_east = pg_nic_new_by_id("nic_east", 1, &error); /* link bricks */ pg_brick_chained_links(&error, nic_west, print_west, fw, print_east, nic_east); /* add some rules to firewall */ pg_firewall_rule_add(fw, "tcp portrange 1-1024", PG_MAX_SIDE, 1, &error); pg_firewall_rule_add(fw, "icmp", PG_MAX_SIDE, 1, &error); pg_firewall_reload(fw, &error); /* create a graph with all bricks inside */ graph = pg_graph_new("graph", fw, &error); printf("let's pool 1000*1000 times ...\n"); for (int i = 0; i < 1000000; i++) { if (pg_graph_poll(graph, &error) < 0) { pg_error_print(error); pg_error_free(error); } } pg_graph_destroy(graph); pg_stop(); return 0; }
static int start_loop(int verbose) { struct pg_error *error = NULL; struct pg_brick *nic_tmp, *switch_east, *print_tmp; uint16_t port_count = rte_eth_dev_count(); GList *nic_manager = NULL; GList *manager = NULL; g_assert(port_count > 1); /* * Here is an ascii graph of the links: * * [NIC-X] - [PRINT-X] --\ * \ * [NIC-X+1] - [PRINT-X+1] } -- [SWITCH] * / * [NIC-X+2] - [PRINT-X+2] / * .... */ switch_east = pg_switch_new("switch", 20, 20, &error); CHECK_ERROR(error); PG_BM_ADD(manager, switch_east); for (int i = 0; i < port_count; ++i) { nic_tmp = pg_nic_new_by_id("nic", 1, 1, WEST_SIDE, i, &error); CHECK_ERROR(error); print_tmp = pg_print_new("print", 1, 1, NULL, PG_PRINT_FLAG_MAX, NULL, &error); CHECK_ERROR(error); if (!verbose) pg_brick_chained_links(&error, nic_tmp, switch_east); else pg_brick_chained_links(&error, nic_tmp, print_tmp, switch_east); CHECK_ERROR(error); PG_BM_ADD(nic_manager, nic_tmp); PG_BM_ADD(manager, print_tmp); } while (1) { uint64_t tot_send_pkts = 0; for (int i = 0; i < 100000; ++i) { uint16_t nb_send_pkts; PG_BM_GET_NEXT(nic_manager, nic_tmp); pg_brick_poll(nic_tmp, &nb_send_pkts, &error); tot_send_pkts += nb_send_pkts; CHECK_ERROR(error); usleep(1); } printf("poll pkts: %lu\n", tot_send_pkts); } nic_manager = g_list_first(nic_manager); PG_BM_DESTROY(manager); PG_BM_DESTROY(nic_manager); return 0; }
void test_benchmark_print(int argc, char **argv) { struct pg_error *error = NULL; struct pg_brick *print; struct pg_bench bench; struct pg_bench_stats stats; struct ether_addr mac1 = {{0x52,0x54,0x00,0x12,0x34,0x11}}; struct ether_addr mac2 = {{0x52,0x54,0x00,0x12,0x34,0x21}}; uint32_t len; g_assert(!pg_bench_init(&bench, "print", argc, argv, &error)); print = pg_print_new("print", stderr, PG_PRINT_FLAG_SUMMARY | PG_PRINT_FLAG_TIMESTAMP, NULL, &error); g_assert(!error); bench.input_brick = print; bench.input_side = WEST_SIDE; bench.output_brick = print; bench.output_side = EAST_SIDE; bench.output_poll = false; bench.max_burst_cnt = 100000; bench.count_brick = NULL; bench.pkts_nb = 64; bench.pkts_mask = pg_mask_firsts(64); bench.pkts = pg_packets_create(bench.pkts_mask); bench.pkts = pg_packets_append_ether( bench.pkts, bench.pkts_mask, &mac1, &mac2, ETHER_TYPE_IPv4); bench.brick_full_burst = 1; len = sizeof(struct ipv4_hdr) + sizeof(struct udp_hdr) + 1400; pg_packets_append_ipv4( bench.pkts, bench.pkts_mask, 0x000000EE, 0x000000CC, len, 17); bench.pkts = pg_packets_append_udp( bench.pkts, bench.pkts_mask, 1000, 2000, 1400); bench.pkts = pg_packets_append_blank(bench.pkts, bench.pkts_mask, 1400); g_assert(pg_bench_run(&bench, &stats, &error) == 0); pg_bench_print(&stats); pg_packets_free(bench.pkts, bench.pkts_mask); pg_brick_destroy(print); }
static int start_loop(int verbose, int nb_vhost) { struct pg_error *error = NULL; struct pg_brick *nic_tmp, *switch_east, *print_tmp; uint16_t port_count = rte_eth_dev_count(); GList *nic_manager = NULL; GList *manager = NULL; int ret = -1; /* * Here is an ascii graph of the links: * * [NIC-X] - [PRINT-X] --\ * \ * [NIC-X+1] - [PRINT-X+1] } -- [SWITCH] * / * [NIC-X+2] - [PRINT-X+2] / * .... */ switch_east = pg_switch_new("switch", 20, 20, EAST_SIDE, &error); CHECK_ERROR(error); PG_BM_ADD(manager, switch_east); if (nb_vhost) { if (pg_vhost_start("/tmp", &error) < 0) goto free_switch; port_count = nb_vhost; } g_assert(port_count > 1); for (int i = 0; i < port_count; ++i) { char *tmp_name; if (nb_vhost) { tmp_name = g_strdup_printf("vhost-%d", i); nic_tmp = pg_vhost_new(tmp_name, 1, 1, WEST_SIDE, &error); } else { tmp_name = g_strdup_printf("nic-%d", i); nic_tmp = pg_nic_new_by_id(tmp_name, i, &error); } g_free(tmp_name); CHECK_ERROR(error); tmp_name = g_strdup_printf("print-%d", i); print_tmp = pg_print_new(tmp_name, 1, 1, NULL, PG_PRINT_FLAG_MAX, NULL, &error); g_free(tmp_name); CHECK_ERROR(error); if (!verbose) pg_brick_chained_links(&error, nic_tmp, switch_east); else pg_brick_chained_links(&error, nic_tmp, print_tmp, switch_east); CHECK_ERROR(error); PG_BM_ADD(nic_manager, nic_tmp); PG_BM_ADD(manager, print_tmp); } while (1) { uint64_t tot_send_pkts = 0; for (int i = 0; i < 100000; ++i) { uint16_t nb_send_pkts; PG_BM_GET_NEXT(nic_manager, nic_tmp); pg_brick_poll(nic_tmp, &nb_send_pkts, &error); tot_send_pkts += nb_send_pkts; CHECK_ERROR(error); usleep(1); } printf("poll pkts: %lu\n", tot_send_pkts); } ret = 0; nic_manager = g_list_first(nic_manager); PG_BM_DESTROY(nic_manager); free_switch: PG_BM_DESTROY(manager); pg_vhost_stop(); return ret; }
static int start_loop(uint32_t vtep_ip, struct ether_addr *vtep_mac, struct ether_addr *inner_mac, GList *neighbor_macs) { struct pg_error *error = NULL; struct pg_brick *nic_east, *nic_west, *vtep_east, *vtep_west; struct pg_brick *print_east, *print_west, *print_middle; /* * Here is an ascii graph of the links: * NIC = nic * VT = vtep * * [NIC] - [PRINT] - [VT] -- [PRINT] -- [VT] -- [PRINT] -- [NIC] */ nic_east = pg_nic_new_by_id("nic-e", 1, 1, EAST_SIDE, 0, &error); CHECK_ERROR(error); nic_west = pg_nic_new_by_id("nic-w", 1, 1, WEST_SIDE, 1, &error); CHECK_ERROR(error); vtep_east = pg_vtep_new("vt-e", 1, 1, WEST_SIDE, vtep_ip, *vtep_mac, 1, &error); CHECK_ERROR(error); inverse_mac(vtep_mac); pg_print_mac(vtep_mac); printf("\n"); vtep_west = pg_vtep_new("vt-w", 1, 1, EAST_SIDE, ~vtep_ip, *vtep_mac, 1, &error); CHECK_ERROR(error); print_west = pg_print_new("west", 1, 1, NULL, PG_PRINT_FLAG_MAX, NULL, &error); CHECK_ERROR(error); print_east = pg_print_new("east", 1, 1, NULL, PG_PRINT_FLAG_MAX, NULL, &error); CHECK_ERROR(error); print_middle = pg_print_new("middle", 1, 1, NULL, PG_PRINT_FLAG_MAX, NULL, &error); CHECK_ERROR(error); /* If you want to print transmiting pkts uncomment this and coment * the bellow pg_brick_chained_links * Attention: this may slow down the transmition */ /* pg_brick_chained_links(&error, nic_west, print_west, */ /* vtep_west, print_middle, vtep_east, */ /* print_east, nic_east); */ pg_brick_chained_links(&error, nic_west, vtep_west, vtep_east, nic_east); CHECK_ERROR(error); pg_vtep_add_vni(vtep_west, nic_west, 0, inet_addr("225.0.0.43"), &error); CHECK_ERROR(error); pg_vtep_add_vni(vtep_east, nic_east, 0, inet_addr("225.0.0.43"), &error); CHECK_ERROR(error); while (!quit) { uint16_t nb_send_pkts; g_assert(pg_brick_poll(nic_west, &nb_send_pkts, &error)); usleep(1); g_assert(pg_brick_poll(nic_east, &nb_send_pkts, &error)); usleep(1); } pg_brick_destroy(nic_west); pg_brick_destroy(print_west); pg_brick_destroy(vtep_west); pg_brick_destroy(print_middle); pg_brick_destroy(vtep_east); pg_brick_destroy(print_east); pg_brick_destroy(nic_east); return 0; }