static enum pg_side fail_get_side(struct pg_brick *brick) { struct pg_fail_state *state = pg_brick_get_state(brick, struct pg_fail_state); return pg_flip_side(state->output); }
static void fail_link(struct pg_brick *brick, enum pg_side side, int edge) { struct pg_fail_state *state = pg_brick_get_state(brick, struct pg_fail_state); state->output = pg_flip_side(side); }
static int packetsgen_burst(struct pg_brick *brick, enum pg_side side, uint16_t edge_index, struct rte_mbuf **pkts, uint64_t pkts_mask, struct pg_error **errp) { struct pg_brick_side *s = &brick->sides[pg_flip_side(side)]; return pg_brick_side_forward(s, side, pkts, pkts_mask, errp); }
static int nop_burst(struct pg_brick *brick, enum pg_side from, uint16_t edge_index, struct rte_mbuf **pkts, uint16_t nb, uint64_t pkts_mask, struct pg_error **errp) { struct pg_brick_side *s = &brick->sides[pg_flip_side(from)]; if (s->edge.link == NULL) return 1; return pg_brick_burst(s->edge.link, from, s->edge.pair_index, pkts, nb, pkts_mask, errp); }
static void firewall_filter_rules(enum pg_side dir) { struct pg_brick *gen; struct pg_brick *fw; struct pg_brick *col; struct pg_error *error = NULL; uint16_t i; int ret; static uint16_t nb = 30; struct rte_mbuf *packets[nb]; uint64_t filtered_pkts_mask; struct rte_mbuf **filtered_pkts; uint64_t bit; uint16_t packet_count; struct ip *ip; struct ether_hdr *eth; /* create and connect 3 bricks: generator -> firewall -> collector */ gen = pg_packetsgen_new("gen", 2, 2, pg_flip_side(dir), packets, nb, &error); g_assert(!error); fw = pg_firewall_new("fw", 2, 2, PG_NONE, &error); g_assert(!error); col = pg_collect_new("col", 2, 2, &error); g_assert(!error); /* revert link if needed */ if (dir == WEST_SIDE) { pg_brick_link(gen, fw, &error); g_assert(!error); pg_brick_link(fw, col, &error); g_assert(!error); } else { pg_brick_link(col, fw, &error); g_assert(!error); pg_brick_link(fw, gen, &error); g_assert(!error); } /* build some UDP packets mixed sources */ for (i = 0; i < nb; i++) switch (i % 3) { case 0: packets[i] = build_ip_packet("10.0.0.1", "10.0.0.255", i); break; case 1: packets[i] = build_ip_packet("10.0.0.2", "10.0.0.255", i); break; case 2: packets[i] = build_ip_packet("10.0.0.3", "10.0.0.255", i); break; } /* configure firewall to allow traffic from 10.0.0.1 */ ret = pg_firewall_rule_add(fw, "src host 10.0.0.1", dir, 0, &error); g_assert(!error); g_assert(ret == 0); ret = pg_firewall_reload(fw, &error); g_assert(ret < 0); g_assert(!error); /* let's burst ! */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); /* check collect brick */ if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == nb / 3); for (; filtered_pkts_mask;) { pg_low_bit_iterate_full(filtered_pkts_mask, bit, i); g_assert(i % 3 == 0); eth = rte_pktmbuf_mtod(filtered_pkts[i], struct ether_hdr*); ip = (struct ip *)(eth + 1); g_assert(ip->ip_src.s_addr == inet_addr("10.0.0.1")); } /* now allow packets from 10.0.0.2 */ ret = pg_firewall_rule_add(fw, "src host 10.0.0.2", dir, 0, &error); g_assert(!error); g_assert(ret == 0); ret = pg_firewall_reload(fw, &error); g_assert(ret < 0); g_assert(!error); /* let it goooo */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); /* check collect brick */ if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == nb * 2 / 3); for (; filtered_pkts_mask;) { pg_low_bit_iterate_full(filtered_pkts_mask, bit, i); g_assert(i % 3 == 0 || i % 3 == 1); eth = rte_pktmbuf_mtod(filtered_pkts[i], struct ether_hdr*); ip = (struct ip *)(eth + 1); g_assert(ip->ip_src.s_addr == inet_addr("10.0.0.1") || ip->ip_src.s_addr == inet_addr("10.0.0.2")); } /* test that flush really blocks */ pg_firewall_rule_flush(fw); ret = pg_firewall_reload(fw, &error); g_assert(!error); g_assert(ret < 0); /* let it goooo */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); /* check collect brick */ if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == 0); /* flush and only allow packets from 10.0.0.2 */ pg_firewall_rule_flush(fw); ret = pg_firewall_rule_add(fw, "src host 10.0.0.2", dir, 0, &error); g_assert(!error); g_assert(ret == 0); ret = pg_firewall_reload(fw, &error); g_assert(ret < 0); g_assert(!error); /* let it goooo */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); /* check collect brick */ if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == nb / 3); for (; filtered_pkts_mask;) { pg_low_bit_iterate_full(filtered_pkts_mask, bit, i); g_assert(i % 3 == 1); eth = rte_pktmbuf_mtod(filtered_pkts[i], struct ether_hdr*); ip = (struct ip *)(eth + 1); g_assert(ip->ip_src.s_addr == inet_addr("10.0.0.2")); } /* flush and make two rules in one */ pg_firewall_rule_flush(fw); ret = pg_firewall_rule_add(fw, "src host (10.0.0.1 or 10.0.0.2)", dir, 0, &error); g_assert(!error); g_assert(ret == 0); ret = pg_firewall_reload(fw, &error); g_assert(ret < 0); g_assert(!error); /* let it goooo */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); /* check collect brick */ if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == nb * 2 / 3); for (; filtered_pkts_mask;) { pg_low_bit_iterate_full(filtered_pkts_mask, bit, i); g_assert(i % 3 == 0 || i % 3 == 1); eth = rte_pktmbuf_mtod(filtered_pkts[i], struct ether_hdr*); ip = (struct ip *)(eth + 1); g_assert(ip->ip_src.s_addr == inet_addr("10.0.0.1") || ip->ip_src.s_addr == inet_addr("10.0.0.2")); } /* flush and revert rules, packets should not pass */ pg_firewall_rule_flush(fw); ret = pg_firewall_rule_add(fw, "src host (10.0.0.1)", pg_flip_side(dir), 0, &error); g_assert(!error); g_assert(ret == 0); ret = pg_firewall_reload(fw, &error); g_assert(ret < 0); g_assert(!error); /* let it goooo */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); /* check collect brick */ if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == 0); /* flush and allow packets from both sides */ pg_firewall_rule_flush(fw); ret = pg_firewall_rule_add(fw, "src host (10.0.0.1)", MAX_SIDE, 0, &error); g_assert(!error); g_assert(ret == 0); ret = pg_firewall_reload(fw, &error); g_assert(ret < 0); g_assert(!error); /* let it goooo */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == nb / 3); for (; filtered_pkts_mask;) { pg_low_bit_iterate_full(filtered_pkts_mask, bit, i); g_assert(i % 3 == 0); eth = rte_pktmbuf_mtod(filtered_pkts[i], struct ether_hdr*); ip = (struct ip *)(eth + 1); g_assert(ip->ip_src.s_addr == inet_addr("10.0.0.1")); } /* inverse generator and collector to test both sides */ pg_brick_unlink(fw, &error); g_assert(!error); if (dir == WEST_SIDE) { pg_brick_link(col, fw, &error); g_assert(!error); pg_brick_link(fw, gen, &error); g_assert(!error); } else { pg_brick_link(gen, fw, &error); g_assert(!error); pg_brick_link(fw, col, &error); g_assert(!error); } /* let it goooo */ pg_brick_poll(gen, &packet_count, &error); g_assert(!error); g_assert(packet_count == nb); if (dir == WEST_SIDE) filtered_pkts = pg_brick_west_burst_get(col, &filtered_pkts_mask, &error); else filtered_pkts = pg_brick_east_burst_get(col, &filtered_pkts_mask, &error); g_assert(!error); g_assert(pg_mask_count(filtered_pkts_mask) == nb / 3); for (; filtered_pkts_mask;) { pg_low_bit_iterate_full(filtered_pkts_mask, bit, i); g_assert(i % 3 == 0); eth = rte_pktmbuf_mtod(filtered_pkts[i], struct ether_hdr*); ip = (struct ip *)(eth + 1); g_assert(ip->ip_src.s_addr == inet_addr("10.0.0.1")); } /* clean */ for (i = 0; i < nb; i++) rte_pktmbuf_free(packets[i]); pg_brick_destroy(gen); pg_brick_destroy(fw); pg_brick_destroy(col); }
int pg_bench_run(struct pg_bench *bench, struct pg_bench_stats *result, struct pg_error **error) { uint64_t bit; uint64_t it_mask; uint64_t i; uint16_t cnt; uint64_t pkts_burst; struct pg_brick_side *side = NULL; struct pg_brick *count_brick; struct pg_bench bl; if (bench == NULL || result == NULL || bench->pkts == NULL || bench->pkts_nb == 0 || bench->max_burst_cnt == 0 || bench->pkts_mask == 0) { *error = pg_error_new("missing or bad bench parameters"); return -1; } /* Link ouput brick to a nop brick to count outcoming packets. */ if (bench->count_brick == NULL) { count_brick = pg_nop_new("nop-bench", error); if (*error) return -1; if (bench->output_side == WEST_SIDE) pg_brick_link(count_brick, bench->output_brick, error); else pg_brick_link(bench->output_brick, count_brick, error); if (*error) return -1; } else { count_brick = bench->count_brick; } /* Set all stats to zero. */ memset(result, 0, sizeof(struct pg_bench_stats)); /* Setup callback to get burst count. */ pkts_burst = 0; switch (bench->input_brick->type) { case PG_MONOPOLE: side = bench->input_brick->sides; break; case PG_DIPOLE: case PG_MULTIPOLE: side = &(bench->input_brick->sides [pg_flip_side(bench->input_side)]); break; default: g_assert(0); break; } side->burst_count_cb = pg_bench_burst_cb; side->burst_count_private_data = (void *)(&pkts_burst); /* Compute average size of packets. */ it_mask = bench->pkts_mask; for (; it_mask;) { pg_low_bit_iterate_full(it_mask, bit, i); result->pkts_average_size += bench->pkts[i]->data_len; } result->pkts_average_size /= bench->pkts_nb; /* Let's run ! */ memcpy(&bl, bench, sizeof(struct pg_bench)); gettimeofday(&result->date_start, NULL); for (i = 0; i < bl.max_burst_cnt; i++) { /* Burst packets. */ pg_brick_burst(bl.input_brick, bl.input_side, 0, bl.pkts, bl.pkts_mask, error); sched_yield(); if (*error) return -1; /* Poll back packets if needed. */ if (bl.output_poll) pg_brick_poll(bl.output_brick, &cnt, error); if (bl.post_burst_op) bl.post_burst_op(bench); } gettimeofday(&result->date_end, NULL); memcpy(bench, &bl, sizeof(struct pg_bench)); result->pkts_sent = bench->max_burst_cnt * bench->pkts_nb; result->burst_cnt = bench->max_burst_cnt; result->pkts_burst = pkts_burst; result->pkts_received = pg_brick_pkts_count_get( count_brick, bench->output_side); if (bench->count_brick == NULL) { pg_brick_unlink(count_brick, error); if (*error) return -1; } return 0; }
static int packetsgen_poll(struct pg_brick *brick, uint16_t *pkts_cnt, struct pg_error **errp) { struct pg_packetsgen_state *state; struct rte_mempool *mp = pg_get_mempool(); struct rte_mbuf **pkts; struct pg_brick_side *s; uint64_t pkts_mask; int ret; uint16_t i; state = pg_brick_get_state(brick, struct pg_packetsgen_state); s = &brick->sides[state->output]; pkts = g_new0(struct rte_mbuf*, state->packets_nb); for (i = 0; i < state->packets_nb; i++) { pkts[i] = rte_pktmbuf_clone(state->packets[i], mp); pkts[i]->udata64 = i; } pkts_mask = pg_mask_firsts(state->packets_nb); *pkts_cnt = state->packets_nb; ret = pg_brick_side_forward(s, pg_flip_side(state->output), pkts, pkts_mask, errp); pg_packets_free(pkts, pkts_mask); g_free(pkts); return ret; }