diff options
| -rw-r--r-- | packet.c | 18 | ||||
| -rw-r--r-- | packet.h | 1 | ||||
| -rw-r--r-- | tap.c | 4 | ||||
| -rw-r--r-- | tcp.c | 30 | ||||
| -rw-r--r-- | udp.c | 23 |
5 files changed, 52 insertions, 24 deletions
@@ -207,3 +207,21 @@ void pool_flush(struct pool *p) { p->count = 0; } + +/** + * packet_next() - Index of the packet following the one at @idx + * @p: Packet pool + * @idx: Slot index of the current packet (marker or single iovec) + * + * A multi-iovec packet is stored as a marker slot (iov_base == NULL) whose + * iov_len holds the number of iovec slots that follow it. + * + * Return: slot index of the next packet + */ +size_t packet_next(const struct pool *p, size_t idx) +{ + if (p->pkt[idx].iov_base == NULL) + return idx + p->pkt[idx].iov_len + 1; + + return idx + 1; +} @@ -41,6 +41,7 @@ bool packet_get_do(const struct pool *p, const size_t idx, struct iov_tail *data, const char *func, int line); bool pool_can_fit(const struct pool *p, struct iov_tail *data); void pool_flush(struct pool *p); +size_t packet_next(const struct pool *p, size_t idx); #define packet_add(p, data) \ packet_add_do(p, data, __func__, __LINE__) @@ -700,7 +700,7 @@ static int tap4_handler(struct ctx *c, const struct pool *in, i = 0; resume: - for (seq_count = 0, seq = NULL; i < in->count; i++) { + for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) { struct iovec trim_iov[UIO_MAXIOV]; size_t l3len, hlen, l4len, check; struct ethhdr eh_storage; @@ -948,7 +948,7 @@ static int tap6_handler(struct ctx *c, const struct pool *in, i = 0; resume: - for (seq_count = 0, seq = NULL; i < in->count; i++) { + for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) { size_t l4len, plen, check; struct in6_addr *saddr, *daddr; struct ipv6hdr ip6h_storage; @@ -1954,7 +1954,8 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn, assert(conn->events & ESTABLISHED); - for (i = idx, iov_i = 0; i < (int)p->count; i++) { + for (i = idx, iov_i = 0; i < (int)p->count; + i = (i == -1) ? 0 : packet_next(p, i)) { uint32_t seq, seq_offset, ack_seq; struct tcphdr th_storage; const struct tcphdr *th; @@ -1977,7 +1978,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn, if (th->rst) { tcp_sock_rst(c, conn, now); - return 1; + return packet_next(p, i) - i; } len -= off; @@ -1998,10 +1999,10 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn, &((int){ 1 }), sizeof(int))) flow_trace(conn, "failed to set SO_KEEPALIVE"); - if (p->count == 1) { + if (packet_next(p, i) == p->count) { tcp_tap_window_update(c, conn, ntohs(th->window), now); - return 1; + return packet_next(p, i) - i; } continue; @@ -2329,13 +2330,13 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, (void)pif; if (!packet_get(p, idx, &data)) - return 1; + return packet_next(p, idx) - idx; l4len = iov_tail_size(&data); th = IOV_REMOVE_HEADER(&data, th_storage); if (!th) - return 1; + return packet_next(p, idx) - idx; optlen = th->doff * 4UL - sizeof(*th); /* Static checkers might fail to see this: */ @@ -2353,7 +2354,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, opts, optlen, now); else tcp_rst_no_conn(c, af, saddr, daddr, flow_lbl, th, l4len); - return 1; + return packet_next(p, idx) - idx; } assert(flow->f.type == FLOW_TCP); @@ -2364,7 +2365,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, if (th->rst) { tcp_sock_rst(c, conn, now); - return 1; + return packet_next(p, idx) - idx; } conn->inactive = false; @@ -2378,7 +2379,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, if (th->syn && th->ack && !th->fin) { tcp_conn_from_sock_finish(c, conn, th, opts, optlen, now); - return 1; + return packet_next(p, idx) - idx; } goto reset; @@ -2386,8 +2387,9 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, /* Establishing connection from tap */ if (conn->events & TAP_SYN_RCVD) { + /* SYN retry: ignore and keep waiting */ if (th->syn && !th->ack && !th->fin) - return 1; /* SYN retry: ignore and keep waiting */ + return packet_next(p, idx) - idx; if (!(conn->events & TAP_SYN_ACK_SENT)) goto reset; @@ -2410,7 +2412,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, conn_event(c, conn, SOCK_FIN_SENT, now); - return 1; + return packet_next(p, idx) - idx; } if (!th->ack) @@ -2419,8 +2421,8 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, if (tcp_tap_window_update(c, conn, ntohs(th->window), now)) tcp_data_from_sock(c, conn, now); - if (p->count - idx == 1) - return 1; + if (packet_next(p, idx) == p->count) + return packet_next(p, idx) - idx; } /* Established connections not accepting data from tap */ @@ -2473,7 +2475,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af, conn_event(c, conn, CLOSED, now); } - return 1; + return packet_next(p, idx) - idx; } /* Established connections accepting data from tap */ @@ -1023,6 +1023,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif, { const struct flowside *toside; struct mmsghdr mm[UIO_MAXIOV]; + size_t packet_idx[UIO_MAXIOV + 1]; union sockaddr_inany to_sa; struct iovec m[UIO_MAXIOV]; struct udphdr uh_storage; @@ -1033,15 +1034,16 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif, flow_sidx_t tosidx; in_port_t src, dst; uint8_t topif; + size_t slot; assert(!c->no_udp); if (!packet_get(p, idx, &data)) - return 1; + return packet_next(p, idx) - idx; uh = IOV_PEEK_HEADER(&data, uh_storage); if (!uh) - return 1; + return packet_next(p, idx) - idx; /* The caller already checks that all the messages have the same source * and destination, so we can just take those from the first message. @@ -1057,7 +1059,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif, pif_name(pif), inet_ntop(af, saddr, sstr, sizeof(sstr)), src, inet_ntop(af, daddr, dstr, sizeof(dstr)), dst); - return 1; + return packet_next(p, idx) - idx; } topif = pif_at_sidx(tosidx); @@ -1068,7 +1070,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif, flow_err_ratelimit(uflow, now, "No support for forwarding UDP from %s to %s", pif_name(frompif), pif_name(topif)); - return 1; + return packet_next(p, idx) - idx; } toside = flowside_at_sidx(tosidx); @@ -1077,10 +1079,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif, pif_sockaddr(c, &to_sa, topif, &toside->eaddr, toside->eport); - for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) { + for (i = 0, j = 0, slot = idx; + slot < p->count && j < UIO_MAXIOV && i < UIO_MAXIOV; + slot = packet_next(p, slot), i++) { const struct udphdr *uh_send; - if (!packet_get(p, idx + i, &data)) + packet_idx[count] = slot; + + if (!packet_get(p, slot, &data)) return p->count - idx; uh_send = IOV_REMOVE_HEADER(&data, uh_storage); @@ -1129,12 +1135,13 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif, count++; } + packet_idx[count] = slot; count = sendmmsg(s, mm, count, MSG_NOSIGNAL); if (count < 0) - return 1; + return packet_next(p, idx) - idx; - return count; + return packet_idx[count] - idx; } /** |
