Skip to content

Commit

Permalink
userspace: Add Generic Segmentation Offloading.
Browse files Browse the repository at this point in the history
This provides a software implementation in the case
the egress netdev doesn't support segmentation in hardware.

The challenge here is to guarantee packet ordering in the
original batch that may be full of TSO packets. Each TSO
packet can go up to ~64kB, so with segment size of 1440
that means about 44 packets for each TSO. Each batch has
32 packets, so the total batch amounts to 1408 normal
packets.

The segmentation estimates the total number of packets
and then the total number of batches. Then allocate
enough memory and finally do the work.

Finally each batch is sent in order to the netdev.

Signed-off-by: Flavio Leitner <[email protected]>
Co-authored-by: Mike Pattrick <[email protected]>
Signed-off-by: Mike Pattrick <[email protected]>
Acked-by: Simon Horman <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
2 people authored and igsilya committed Dec 1, 2023
1 parent e005601 commit f78805f
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 65 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Post-v3.2.0
a.k.a. 'configured' values, can be found in the 'status' column of
the Interface table, i.e. with 'ovs-vsctl get interface <..> status'.
Reported names adjusted accordingly.
- Userspace datapath:
* Added support for Generic Segmentation Offloading for the cases where
TSO is enabled, but not supported by the egress interface (except for
tunnel interfaces).


v3.2.0 - 17 Aug 2023
Expand Down
2 changes: 2 additions & 0 deletions lib/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ lib_libopenvswitch_la_SOURCES = \
lib/dpctl.h \
lib/dp-packet.h \
lib/dp-packet.c \
lib/dp-packet-gso.c \
lib/dp-packet-gso.h \
lib/dpdk.h \
lib/dpif-netdev-extract-study.c \
lib/dpif-netdev-lookup.h \
Expand Down
168 changes: 168 additions & 0 deletions lib/dp-packet-gso.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <config.h>
#include <stdlib.h>
#include <string.h>

#include "dp-packet.h"
#include "dp-packet-gso.h"
#include "netdev-provider.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(dp_packet_gso);

/* Retuns a new packet that is a segment of packet 'p'.
*
* The new packet is initialized with 'hdr_len' bytes from the
* start of packet 'p' and then appended with 'data_len' bytes
* from the 'data' buffer.
*
* Note: The packet headers are not updated. */
static struct dp_packet *
dp_packet_gso_seg_new(const struct dp_packet *p, size_t hdr_len,
const char *data, size_t data_len)
{
struct dp_packet *seg = dp_packet_new_with_headroom(hdr_len + data_len,
dp_packet_headroom(p));

/* Append the original packet headers and then the payload. */
dp_packet_put(seg, dp_packet_data(p), hdr_len);
dp_packet_put(seg, data, data_len);

/* The new segment should have the same offsets. */
seg->l2_5_ofs = p->l2_5_ofs;
seg->l3_ofs = p->l3_ofs;
seg->l4_ofs = p->l4_ofs;

/* The protocol headers remain the same, so preserve hash and mark. */
*dp_packet_rss_ptr(seg) = *dp_packet_rss_ptr(p);
*dp_packet_flow_mark_ptr(seg) = *dp_packet_flow_mark_ptr(p);

/* The segment should inherit all the offloading flags from the
* original packet, except for the TCP segmentation, external
* buffer and indirect buffer flags. */
*dp_packet_ol_flags_ptr(seg) = *dp_packet_ol_flags_ptr(p)
& DP_PACKET_OL_SUPPORTED_MASK;

dp_packet_hwol_reset_tcp_seg(seg);

return seg;
}

/* Returns the calculated number of TCP segments in packet 'p'. */
int
dp_packet_gso_nr_segs(struct dp_packet *p)
{
uint16_t segsz = dp_packet_get_tso_segsz(p);
const char *data_tail;
const char *data_pos;

data_pos = dp_packet_get_tcp_payload(p);
data_tail = (char *) dp_packet_tail(p) - dp_packet_l2_pad_size(p);

return DIV_ROUND_UP(data_tail - data_pos, segsz);
}

/* Perform software segmentation on packet 'p'.
*
* Segments packet 'p' into the array of preallocated batches in 'batches',
* updating the 'batches' pointer as needed and returns true.
*
* Returns false if the packet cannot be segmented. */
bool
dp_packet_gso(struct dp_packet *p, struct dp_packet_batch **batches)
{
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
struct dp_packet_batch *curr_batch = *batches;
struct tcp_header *tcp_hdr;
struct ip_header *ip_hdr;
struct dp_packet *seg;
uint16_t tcp_offset;
uint16_t tso_segsz;
uint32_t tcp_seq;
uint16_t ip_id;
int hdr_len;
int seg_len;

tso_segsz = dp_packet_get_tso_segsz(p);
if (!tso_segsz) {
VLOG_WARN_RL(&rl, "GSO packet with len %d with no segment size.",
dp_packet_size(p));
return false;
}

tcp_hdr = dp_packet_l4(p);
tcp_offset = TCP_OFFSET(tcp_hdr->tcp_ctl);
tcp_seq = ntohl(get_16aligned_be32(&tcp_hdr->tcp_seq));
hdr_len = ((char *) dp_packet_l4(p) - (char *) dp_packet_eth(p))
+ tcp_offset * 4;
ip_id = 0;
if (dp_packet_hwol_is_ipv4(p)) {
ip_hdr = dp_packet_l3(p);
ip_id = ntohs(ip_hdr->ip_id);
}

const char *data_tail = (char *) dp_packet_tail(p)
- dp_packet_l2_pad_size(p);
const char *data_pos = dp_packet_get_tcp_payload(p);
int n_segs = dp_packet_gso_nr_segs(p);

for (int i = 0; i < n_segs; i++) {
seg_len = data_tail - data_pos;
if (seg_len > tso_segsz) {
seg_len = tso_segsz;
}

seg = dp_packet_gso_seg_new(p, hdr_len, data_pos, seg_len);
data_pos += seg_len;

/* Update L3 header. */
if (dp_packet_hwol_is_ipv4(seg)) {
ip_hdr = dp_packet_l3(seg);
ip_hdr->ip_tot_len = htons(sizeof *ip_hdr +
dp_packet_l4_size(seg));
ip_hdr->ip_id = htons(ip_id);
ip_hdr->ip_csum = 0;
ip_id++;
} else {
struct ovs_16aligned_ip6_hdr *ip6_hdr = dp_packet_l3(seg);

ip6_hdr->ip6_ctlun.ip6_un1.ip6_un1_plen
= htons(sizeof *ip_hdr + dp_packet_l4_size(seg));
}

/* Update L4 header. */
tcp_hdr = dp_packet_l4(seg);
put_16aligned_be32(&tcp_hdr->tcp_seq, htonl(tcp_seq));
tcp_seq += seg_len;
if (OVS_LIKELY(i < (n_segs - 1))) {
/* Reset flags PUSH and FIN unless it is the last segment. */
uint16_t tcp_flags = TCP_FLAGS(tcp_hdr->tcp_ctl)
& ~(TCP_PSH | TCP_FIN);
tcp_hdr->tcp_ctl = TCP_CTL(tcp_flags, tcp_offset);
}

if (dp_packet_batch_is_full(curr_batch)) {
curr_batch++;
}

dp_packet_batch_add(curr_batch, seg);
}

*batches = curr_batch;
return true;
}
23 changes: 23 additions & 0 deletions lib/dp-packet-gso.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef DP_PACKET_GSO_H
#define DP_PACKET_GSO_H 1

bool dp_packet_gso(struct dp_packet *, struct dp_packet_batch **);
int dp_packet_gso_nr_segs(struct dp_packet *);

#endif /* dp-packet-gso.h */
7 changes: 7 additions & 0 deletions lib/dp-packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,13 @@ dp_packet_hwol_set_tcp_seg(struct dp_packet *b)
*dp_packet_ol_flags_ptr(b) |= DP_PACKET_OL_TX_TCP_SEG;
}

/* Resets TCP Segmentation flag in packet 'p'. */
static inline void
dp_packet_hwol_reset_tcp_seg(struct dp_packet *p)
{
*dp_packet_ol_flags_ptr(p) &= ~DP_PACKET_OL_TX_TCP_SEG;
}

/* Returns 'true' if the IP header has good integrity and the
* checksum in it is complete. */
static inline bool
Expand Down
44 changes: 32 additions & 12 deletions lib/netdev-dpdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,7 @@ static bool
netdev_dpdk_prep_hwol_packet(struct netdev_dpdk *dev, struct rte_mbuf *mbuf)
{
struct dp_packet *pkt = CONTAINER_OF(mbuf, struct dp_packet, mbuf);
struct tcp_header *th;

if (!(mbuf->ol_flags & (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_L4_MASK
| RTE_MBUF_F_TX_TCP_SEG))) {
Expand All @@ -2483,27 +2484,36 @@ netdev_dpdk_prep_hwol_packet(struct netdev_dpdk *dev, struct rte_mbuf *mbuf)
mbuf->l4_len = 0;
mbuf->outer_l2_len = 0;
mbuf->outer_l3_len = 0;
th = dp_packet_l4(pkt);

if (mbuf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
struct tcp_header *th = dp_packet_l4(pkt);
int hdr_len;

if (!th) {
VLOG_WARN_RL(&rl, "%s: TCP Segmentation without L4 header"
" pkt len: %"PRIu32"", dev->up.name, mbuf->pkt_len);
return false;
}
}

if (mbuf->ol_flags & RTE_MBUF_F_TX_TCP_CKSUM) {
if (!th) {
VLOG_WARN_RL(&rl, "%s: TCP offloading without L4 header"
" pkt len: %"PRIu32"", dev->up.name, mbuf->pkt_len);
return false;
}

mbuf->l4_len = TCP_OFFSET(th->tcp_ctl) * 4;
mbuf->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
hdr_len = mbuf->l2_len + mbuf->l3_len + mbuf->l4_len;
mbuf->tso_segsz = dev->mtu - mbuf->l3_len - mbuf->l4_len;
if (OVS_UNLIKELY((hdr_len + mbuf->tso_segsz) > dev->max_packet_len)) {
VLOG_WARN_RL(&rl, "%s: Oversized TSO packet. "
"hdr: %"PRIu32", gso: %"PRIu32", max len: %"PRIu32"",
dev->up.name, hdr_len, mbuf->tso_segsz,
dev->max_packet_len);
return false;

if (mbuf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
int hdr_len = mbuf->l2_len + mbuf->l3_len + mbuf->l4_len;
if (OVS_UNLIKELY((hdr_len +
mbuf->tso_segsz) > dev->max_packet_len)) {
VLOG_WARN_RL(&rl, "%s: Oversized TSO packet. hdr: %"PRIu32", "
"gso: %"PRIu32", max len: %"PRIu32"",
dev->up.name, hdr_len, mbuf->tso_segsz,
dev->max_packet_len);
return false;
}
}

if (mbuf->ol_flags & RTE_MBUF_F_TX_IPV4) {
Expand Down Expand Up @@ -2891,6 +2901,7 @@ dpdk_copy_dp_packet_to_mbuf(struct rte_mempool *mp, struct dp_packet *pkt_orig)
mbuf_dest->packet_type = pkt_orig->mbuf.packet_type;
mbuf_dest->ol_flags |= (pkt_orig->mbuf.ol_flags &
~(RTE_MBUF_F_EXTERNAL | RTE_MBUF_F_INDIRECT));
mbuf_dest->tso_segsz = pkt_orig->mbuf.tso_segsz;

memcpy(&pkt_dest->l2_pad_size, &pkt_orig->l2_pad_size,
sizeof(struct dp_packet) - offsetof(struct dp_packet, l2_pad_size));
Expand Down Expand Up @@ -2949,11 +2960,20 @@ netdev_dpdk_common_send(struct netdev *netdev, struct dp_packet_batch *batch,
struct rte_mbuf **pkts = (struct rte_mbuf **) batch->packets;
struct netdev_dpdk *dev = netdev_dpdk_cast(netdev);
size_t cnt, pkt_cnt = dp_packet_batch_size(batch);
struct dp_packet *packet;
bool need_copy = false;

memset(stats, 0, sizeof *stats);

DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
if (packet->source != DPBUF_DPDK) {
need_copy = true;
break;
}
}

/* Copy dp-packets to mbufs. */
if (OVS_UNLIKELY(batch->packets[0]->source != DPBUF_DPDK)) {
if (OVS_UNLIKELY(need_copy)) {
cnt = dpdk_copy_batch_to_mbuf(netdev, batch);
stats->tx_failure_drops += pkt_cnt - cnt;
pkt_cnt = cnt;
Expand Down
Loading

0 comments on commit f78805f

Please sign in to comment.