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 502 503 504 505 506 507
| static void *trace_skb_header(struct trace_skb *skb, uint16_t offset, uint16_t size) { uint16_t data_len = RTE_MIN((size_t)skb->pkt_len, sizeof(skb->data));
if (offset >= data_len || offset + size > data_len) return NULL;
return skb->data + offset; }
#define TRACE_SKB_HEADER(skb, offset, type) \ ((type *)trace_skb_header(skb, offset, sizeof(type)))
#define VALUE_IN_RANGE(v, min, max) ((v) >= (min) && (v) <= (max))
static int match_addr(struct addr_filter *af, u32 ip, u16 port) { if (!VALUE_IN_RANGE(ip, af->ip_min, af->ip_max)) return 0; if (!VALUE_IN_RANGE(port, af->port_min, af->port_max)) return 0; return 1; }
static int match_tuple(struct tuple_filter *tf, u32 sip, u32 dip, u16 sport, u16 dport) { if (match_addr(&tf->src, sip, sport) && match_addr(&tf->dst, dip, dport)) return 1; if (tf->nr_dir == 1) return 0; if (match_addr(&tf->dst, sip, sport) && match_addr(&tf->src, dip, dport)) return 1; return 0; }
static struct pal_ip_hdr *parse_trace_skb(struct trace_skb *skb, uint32_t *vpcid, struct pal_l4port_hdr **l4h, uint16_t *l3_proto) { struct pal_ip_hdr *iph; struct pal_geneve_hdr *genh; uint16_t off = 0;
*vpcid = skb->vpcid;
if (skb->has_eth_hdr) { struct pal_eth_hdr *eth = TRACE_SKB_HEADER(skb, off, struct pal_eth_hdr); if (!eth) return NULL; *l3_proto = pal_ntohs(eth->type); off += sizeof(struct pal_eth_hdr); }
iph = TRACE_SKB_HEADER(skb, off, struct pal_ip_hdr); if (!iph) return NULL;
off += iph->ihl << 2; *l4h = TRACE_SKB_HEADER(skb, off, struct pal_l4port_hdr); if (iph->protocol == PAL_IPPROTO_UDP && !ip_is_fragment(iph) && (*l4h)->dest == pal_ntohs(GENEVE_PORT)) { off += 8; genh = TRACE_SKB_HEADER(skb, off, struct pal_geneve_hdr); *vpcid = ((uint32_t)genh->vni[0] << 16) | ((uint32_t)genh->vni[1] << 8) | (uint32_t)genh->vni[2]; off += sizeof(*genh) + (genh->opt_len << 2); if (genh->proto == pal_ntohs(GENEVE_TYPE_ETH)) { struct pal_eth_hdr *eth = TRACE_SKB_HEADER(skb, off, struct pal_eth_hdr); if (!eth) return NULL; *l3_proto = pal_ntohs(eth->type); off += sizeof(struct pal_eth_hdr); } else if (genh->proto == pal_ntohs(GENEVE_TYPE_IPV4)){ *l3_proto = pal_ntohs(genh->proto); } iph = TRACE_SKB_HEADER(skb, off, struct pal_ip_hdr); if (!iph) return NULL; off += iph->ihl << 2; *l4h = TRACE_SKB_HEADER(skb, off, struct pal_l4port_hdr); }
return iph; }
static void pop_outer_header(struct trace_skb *skb) { struct pal_ip_hdr *outer_iph, *inner_iph; struct pal_geneve_hdr *genh; uint16_t off = 0; uint16_t move_len = 0;
if (skb->has_eth_hdr) { struct pal_eth_hdr *eth = TRACE_SKB_HEADER(skb, off, struct pal_eth_hdr); if (!eth) return; off += sizeof(struct pal_eth_hdr); }
outer_iph = TRACE_SKB_HEADER(skb, off, struct pal_ip_hdr); if (!outer_iph) return;
off += outer_iph->ihl << 2; if (outer_iph->protocol != PAL_IPPROTO_UDP || ip_is_fragment(outer_iph)) return; struct pal_udp_hdr *udph = TRACE_SKB_HEADER(skb, off, struct pal_udp_hdr); if (udph->dest != pal_ntohs(GENEVE_PORT)) return; off += sizeof(*udph); genh = TRACE_SKB_HEADER(skb, off, struct pal_geneve_hdr); if (!genh) return; off += sizeof(*genh) + (genh->opt_len << 2);
if (genh->proto == pal_ntohs(GENEVE_TYPE_ETH)) off += sizeof(struct pal_eth_hdr);
inner_iph = TRACE_SKB_HEADER(skb, off, struct pal_ip_hdr); if (!inner_iph) return;
move_len = skb->data + skb->pkt_len - (char *)inner_iph; skb->pkt_len -= (char *)inner_iph - (char *)outer_iph; memmove(outer_iph, inner_iph, move_len); }
static void mangle_skb(struct trace_option *opt, struct trace_record *r) { struct trace_skb *rx_skb = &r->skbs[0]; struct trace_skb *tx_skb = &r->skbs[1];
if (opt->inner) { pop_outer_header(rx_skb); pop_outer_header(tx_skb); } }
static int match_skb(struct trace_option *opt, struct trace_skb *skb) { uint16_t l3_proto = 0; struct pal_ip_hdr *iph; struct pal_l4port_hdr *l4h = NULL; uint32_t vpcid = -1;
if (!skb->pkt_len) return 0;
iph = parse_trace_skb(skb, &vpcid, &l4h, &l3_proto); if (opt->arp) { return l3_proto == PAL_ETH_ARP; }
if (opt->vpcid != -1u && opt->vpcid != vpcid) return 0;
if (iph && ip_is_fragment(iph) && opt->ip_frag == 0) return 0;
if (!opt->tuple.proto && !opt->tuple.nr_dir) return 1;
if (iph) { if (opt->tuple.proto && opt->tuple.proto != iph->protocol) return 0; if (!opt->tuple.nr_dir) return 1; if (l4h && !ip_is_fragment(iph)) { return match_tuple(&opt->tuple, ntohl(iph->saddr), ntohl(iph->daddr), ntohs(l4h->source), ntohs(l4h->dest)); } else { return match_tuple(&opt->tuple, ntohl(iph->saddr), ntohl(iph->daddr), opt->tuple.src.port_min, opt->tuple.dst.port_min); } }
return 0; }
static int match_record(struct trace_option *opt, struct trace_record *r) { struct trace_skb *rx_skb = &r->skbs[0]; struct trace_skb *tx_skb = &r->skbs[1];
if (opt->pkt_err && r->nr_point && r->points[r->nr_point-1].err != opt->pkt_err) return 0;
if (!trace_option_has(opt, rx_skb->recv_if, TRACE_ON_RX) && !trace_option_has(opt, tx_skb->send_if, TRACE_ON_TX)) return 0;
if (!match_skb(opt, rx_skb) && !match_skb(opt, tx_skb)) return 0;
return 1; }
static void trace_cache_init(struct trace_cache *c, trace_ring_t *ring) { c->ring = ring; c->next_seq = obj_ring_next_seq(ring); c->nr_lost = 0; c->r = 0; c->w = 0; }
static struct trace_record *trace_cache_at(struct trace_cache *c, size_t index) { return &c->records[index & (ARRAY_SIZE(c->records) - 1)]; }
static int trace_cache_full(struct trace_cache *c) { return c->w - c->r >= ARRAY_SIZE(c->records); }
static int trace_cache_empty(struct trace_cache *c) { return c->w == c->r; }
static void trace_cache_fill(struct trace_cache *c, struct trace_option *opt) { struct trace_record *r;
while (!trace_cache_full(c)) { r = trace_cache_at(c, c->w); if (!obj_ring_read(c->ring, c->next_seq, r)) break; assert(r->seq >= c->next_seq); c->nr_lost += r->seq - c->next_seq; c->next_seq = r->seq + 1; if (match_record(opt, r)) { c->w++; mangle_skb(opt, r); } else c->nr_filtered++; } }
void trace_option_sync(struct trace_option *opt) { int port_id; int i;
fp_memory_set_writable(g_trace, 1);
for (port_id = 0; port_id < PAL_MAX_PORT; port_id++) { for (i = 0; i < NR_TRACE_ON; i++) { if (trace_option_has(opt, port_id, i)) g_trace->ports[port_id][i] = 30; } }
fp_memory_set_writable(g_trace, 0); }
static void *trace_option_thread(void *arg) { struct trace_option *opt = arg;
while (1) { trace_option_sync(opt); sleep(20); }
return NULL; }
static void trace_option_init(struct trace_option *opt) { const char *pkt_err; const char *vpcid; const char *proto; const char *addr; const char *inner; const char *ip_frag; const char *count; const char *file_r; const char *file_w; const char *arp; pthread_t pid;
fp_memset(opt->ports_flags, 0); opt->pkt_err = 0; opt->vpcid = -1; opt->inner = 0; opt->ip_frag = 0; tuple_filter_init(&opt->tuple); opt->nr_read_max = -1u; opt->fp_r = NULL; opt->fp_w = NULL;
pkt_err = getenv("dpdk_trace_error"); if (pkt_err) { opt->pkt_err = error_code(pkt_err); if (!opt->pkt_err) { fprintf(stderr, "Invalid trace error: %s\n", pkt_err); exit(1); } }
vpcid = getenv("dpdk_trace_vpcid"); if (vpcid) opt->vpcid = strtoul(vpcid, NULL, 10);
proto = getenv("dpdk_trace_proto"); if (proto) opt->tuple.proto = strtoul(proto, NULL, 10);
addr = getenv("dpdk_trace_addr"); if (addr) { int err = tuple_filter_parse(&opt->tuple, &addr); if (err) { fprintf(stderr, "Invalid trace addr: %s, at '%s'\n", tuple_filter_error(err), addr); exit(1); } if (*addr) { fprintf(stderr, "Unexpected input for trace addr: '%s'\n", addr); exit(1); } }
inner = getenv("dpdk_trace_inner"); if (inner) opt->inner = !!strtoul(inner, NULL, 10);
ip_frag = getenv("dpdk_trace_ip_frag"); if (ip_frag) opt->ip_frag = !!strtoul(ip_frag, NULL, 10);
count = getenv("dpdk_trace_count"); if (count) opt->nr_read_max = strtoul(count, NULL, 10);
file_r = getenv("dpdk_trace_read"); if (file_r) opt->fp_r = fopen(file_r, "rb");
file_w = getenv("dpdk_trace_write"); if (file_w) opt->fp_w = fopen(file_w, "wb");
arp = getenv("dpdk_trace_arp"); if (arp) opt->arp = strtoul(arp, NULL, 10);
if (pthread_create(&pid, NULL, trace_option_thread, opt) != 0) { fprintf(stderr, "<%s> pthread_create error: %s\n", __func__, strerror(errno)); exit(1); } }
static uint64_t current_time_usec(void) { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec * 1000000 + tv.tv_usec; }
void trace_reader_strftime(struct trace_reader *reader, uint64_t tsc, struct time_buf *buf) { uint64_t usec = trace_reader_get_usec(reader, tsc); time_t sec = usec / 1000000; struct tm tm;
localtime_r(&sec, &tm);
buf->len = strftime(buf->data, sizeof(buf->data), "%Y-%m-%d %H:%M:%S", &tm); SPRINTF(*buf, ".%06d", (int)(usec % 1000000)); }
void trace_reader_init(struct trace_reader *reader) { struct trace_cache *c;
array_for_each(c, reader->percpu) { trace_cache_init(c, &g_trace->percpu[c - reader->percpu]); }
reader->start_time_usec = current_time_usec(); reader->start_time_tsc = rte_rdtsc(); reader->nr_read = 0; reader->nr_lost = 0; reader->nr_filtered = 0;
trace_option_init(&reader->option);
if (reader->option.fp_r) { if (fread(&reader->start_time_usec, sizeof(uint64_t), 2, reader->option.fp_r) != 2) { fprintf(stderr, "failed to read binary records: %s\n", strerror(errno)); fclose(reader->option.fp_r); reader->option.fp_r = NULL; } }
if (reader->option.fp_w) { if (fwrite(&reader->start_time_usec, sizeof(uint64_t), 2, reader->option.fp_w) != 2) { fprintf(stderr, "failed to write binary records: %s\n", strerror(errno)); fclose(reader->option.fp_w); reader->option.fp_w = NULL; } }
{ struct time_buf time_buf; trace_reader_strftime(reader, reader->start_time_tsc, &time_buf); fprintf(stderr, "trace_start_time: %.*s\n", time_buf.len, time_buf.data); fprintf(stderr, "filter: vpcid %d ", reader->option.vpcid); tuple_filter_dump(&reader->option.tuple, stderr); } }
static void fill_trace_cache(struct trace_reader *reader) { int i;
for (i = 0; i < 1; i++) { struct trace_cache *c; size_t nr_lost = 0; size_t nr_filtered = 0; array_for_each(c, reader->percpu) { trace_cache_fill(c, &reader->option); nr_lost += c->nr_lost; nr_filtered += c->nr_filtered; } reader->nr_lost = nr_lost; reader->nr_filtered = nr_filtered; } }
static struct trace_cache *choose_trace_cache(struct trace_reader *reader) { uint64_t tsc_min = 0; struct trace_cache *tsc_min_c = NULL; struct trace_cache *c; struct trace_record *r;
array_for_each(c, reader->percpu) { if (trace_cache_empty(c)) continue; r = trace_cache_at(c, c->r); if (!tsc_min_c || tsc_min > r->rx_tsc) { tsc_min_c = c; tsc_min = r->rx_tsc; } }
return tsc_min_c; }
struct trace_record *trace_read(struct trace_reader *reader) { struct trace_cache *c; struct trace_record *r; static struct trace_record rbuf;
if (reader->nr_read >= reader->option.nr_read_max) exit(1);
if (reader->option.fp_r) { r = &rbuf; for (;;) { if (fread(r, sizeof(*r), 1, reader->option.fp_r) != 1) { reader->option.nr_read_max = reader->nr_read; (void)raise(SIGINT); return NULL; } if (match_record(&reader->option, r)) break; reader->nr_filtered++; } } else { fill_trace_cache(reader); c = choose_trace_cache(reader); if (!c) return NULL; r = trace_cache_at(c, c->r++); }
if (reader->option.fp_w) { if (fwrite(r, sizeof(*r), 1, reader->option.fp_w) != 1) { fprintf(stderr, "failed to write binary records: %s\n", strerror(errno)); fclose(reader->option.fp_w); reader->option.fp_w = NULL; (void)raise(SIGINT); } }
reader->nr_read++;
if (reader->nr_read == reader->option.nr_read_max) { if (reader->option.fp_w) fclose(reader->option.fp_w); (void)raise(SIGINT); }
return r; }
|