| 1 |
// SPDX-License-Identifier: (GPL-2.0 OR MIT) |
| 2 |
/* Google virtual Ethernet (gve) driver |
| 3 |
* |
| 4 |
* Copyright (C) 2015-2021 Google, Inc. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "gve.h" |
| 8 |
#include "gve_dqo.h" |
| 9 |
#include "gve_adminq.h" |
| 10 |
#include "gve_utils.h" |
| 11 |
#include <linux/bpf.h> |
| 12 |
#include <linux/ip.h> |
| 13 |
#include <linux/ipv6.h> |
| 14 |
#include <linux/skbuff.h> |
| 15 |
#include <linux/slab.h> |
| 16 |
#include <net/ip6_checksum.h> |
| 17 |
#include <net/ipv6.h> |
| 18 |
#include <net/tcp.h> |
| 19 |
#include <net/xdp_sock_drv.h> |
| 20 |
|
| 21 |
static void gve_rx_free_hdr_bufs(struct gve_priv *priv, struct gve_rx_ring *rx) |
| 22 |
{ |
| 23 |
struct device *hdev = &priv->pdev->dev; |
| 24 |
int buf_count = rx->dqo.bufq.mask + 1; |
| 25 |
|
| 26 |
if (rx->dqo.hdr_bufs.data) { |
| 27 |
dma_free_coherent(hdev, priv->header_buf_size * buf_count, |
| 28 |
rx->dqo.hdr_bufs.data, rx->dqo.hdr_bufs.addr); |
| 29 |
rx->dqo.hdr_bufs.data = NULL; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
static void gve_rx_init_ring_state_dqo(struct gve_rx_ring *rx, |
| 34 |
const u32 buffer_queue_slots, |
| 35 |
const u32 completion_queue_slots) |
| 36 |
{ |
| 37 |
int i; |
| 38 |
|
| 39 |
/* Set buffer queue state */ |
| 40 |
rx->dqo.bufq.mask = buffer_queue_slots - 1; |
| 41 |
rx->dqo.bufq.head = 0; |
| 42 |
rx->dqo.bufq.tail = 0; |
| 43 |
|
| 44 |
/* Set completion queue state */ |
| 45 |
rx->dqo.complq.num_free_slots = completion_queue_slots; |
| 46 |
rx->dqo.complq.mask = completion_queue_slots - 1; |
| 47 |
rx->dqo.complq.cur_gen_bit = 0; |
| 48 |
rx->dqo.complq.head = 0; |
| 49 |
|
| 50 |
/* Set RX SKB context */ |
| 51 |
rx->ctx.skb_head = NULL; |
| 52 |
rx->ctx.skb_tail = NULL; |
| 53 |
|
| 54 |
/* Set up linked list of buffer IDs */ |
| 55 |
if (rx->dqo.buf_states) { |
| 56 |
for (i = 0; i < rx->dqo.num_buf_states - 1; i++) |
| 57 |
rx->dqo.buf_states[i].next = i + 1; |
| 58 |
rx->dqo.buf_states[rx->dqo.num_buf_states - 1].next = -1; |
| 59 |
} |
| 60 |
|
| 61 |
rx->dqo.free_buf_states = 0; |
| 62 |
rx->dqo.recycled_buf_states.head = -1; |
| 63 |
rx->dqo.recycled_buf_states.tail = -1; |
| 64 |
rx->dqo.used_buf_states.head = -1; |
| 65 |
rx->dqo.used_buf_states.tail = -1; |
| 66 |
} |
| 67 |
|
| 68 |
static void gve_rx_reset_ring_dqo(struct gve_priv *priv, int idx) |
| 69 |
{ |
| 70 |
struct gve_rx_ring *rx = &priv->rx[idx]; |
| 71 |
size_t size; |
| 72 |
int i; |
| 73 |
|
| 74 |
const u32 buffer_queue_slots = priv->rx_desc_cnt; |
| 75 |
const u32 completion_queue_slots = priv->rx_desc_cnt; |
| 76 |
|
| 77 |
/* Reset buffer queue */ |
| 78 |
if (rx->dqo.bufq.desc_ring) { |
| 79 |
size = sizeof(rx->dqo.bufq.desc_ring[0]) * |
| 80 |
buffer_queue_slots; |
| 81 |
memset(rx->dqo.bufq.desc_ring, 0, size); |
| 82 |
} |
| 83 |
|
| 84 |
/* Reset completion queue */ |
| 85 |
if (rx->dqo.complq.desc_ring) { |
| 86 |
size = sizeof(rx->dqo.complq.desc_ring[0]) * |
| 87 |
completion_queue_slots; |
| 88 |
memset(rx->dqo.complq.desc_ring, 0, size); |
| 89 |
} |
| 90 |
|
| 91 |
/* Reset q_resources */ |
| 92 |
if (rx->q_resources) |
| 93 |
memset(rx->q_resources, 0, sizeof(*rx->q_resources)); |
| 94 |
|
| 95 |
/* Reset buf states */ |
| 96 |
if (rx->dqo.buf_states) { |
| 97 |
for (i = 0; i < rx->dqo.num_buf_states; i++) { |
| 98 |
struct gve_rx_buf_state_dqo *bs = &rx->dqo.buf_states[i]; |
| 99 |
|
| 100 |
if (rx->dqo.page_pool) |
| 101 |
gve_free_to_page_pool(rx, bs, false); |
| 102 |
else |
| 103 |
gve_free_qpl_page_dqo(bs); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
gve_rx_init_ring_state_dqo(rx, buffer_queue_slots, |
| 108 |
completion_queue_slots); |
| 109 |
} |
| 110 |
|
| 111 |
void gve_rx_stop_ring_dqo(struct gve_priv *priv, int idx) |
| 112 |
{ |
| 113 |
int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx); |
| 114 |
struct gve_rx_ring *rx = &priv->rx[idx]; |
| 115 |
|
| 116 |
if (!gve_rx_was_added_to_block(priv, idx)) |
| 117 |
return; |
| 118 |
|
| 119 |
if (rx->dqo.page_pool) |
| 120 |
page_pool_disable_direct_recycling(rx->dqo.page_pool); |
| 121 |
gve_remove_napi(priv, ntfy_idx); |
| 122 |
gve_rx_remove_from_block(priv, idx); |
| 123 |
gve_rx_reset_ring_dqo(priv, idx); |
| 124 |
} |
| 125 |
|
| 126 |
void gve_rx_free_ring_dqo(struct gve_priv *priv, struct gve_rx_ring *rx, |
| 127 |
struct gve_rx_alloc_rings_cfg *cfg) |
| 128 |
{ |
| 129 |
struct device *hdev = &priv->pdev->dev; |
| 130 |
size_t completion_queue_slots; |
| 131 |
size_t buffer_queue_slots; |
| 132 |
int idx = rx->q_num; |
| 133 |
size_t size; |
| 134 |
u32 qpl_id; |
| 135 |
int i; |
| 136 |
|
| 137 |
completion_queue_slots = rx->dqo.complq.mask + 1; |
| 138 |
buffer_queue_slots = rx->dqo.bufq.mask + 1; |
| 139 |
|
| 140 |
if (rx->q_resources) { |
| 141 |
dma_free_coherent(hdev, sizeof(*rx->q_resources), |
| 142 |
rx->q_resources, rx->q_resources_bus); |
| 143 |
rx->q_resources = NULL; |
| 144 |
} |
| 145 |
|
| 146 |
for (i = 0; i < rx->dqo.num_buf_states; i++) { |
| 147 |
struct gve_rx_buf_state_dqo *bs = &rx->dqo.buf_states[i]; |
| 148 |
|
| 149 |
if (rx->dqo.page_pool) |
| 150 |
gve_free_to_page_pool(rx, bs, false); |
| 151 |
else |
| 152 |
gve_free_qpl_page_dqo(bs); |
| 153 |
if (gve_buf_state_is_allocated(rx, bs) && bs->xsk_buff) { |
| 154 |
xsk_buff_free(bs->xsk_buff); |
| 155 |
bs->xsk_buff = NULL; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if (rx->dqo.qpl) { |
| 160 |
qpl_id = gve_get_rx_qpl_id(cfg->qcfg_tx, rx->q_num); |
| 161 |
gve_free_queue_page_list(priv, rx->dqo.qpl, qpl_id); |
| 162 |
rx->dqo.qpl = NULL; |
| 163 |
} |
| 164 |
|
| 165 |
if (rx->dqo.bufq.desc_ring) { |
| 166 |
size = sizeof(rx->dqo.bufq.desc_ring[0]) * buffer_queue_slots; |
| 167 |
dma_free_coherent(hdev, size, rx->dqo.bufq.desc_ring, |
| 168 |
rx->dqo.bufq.bus); |
| 169 |
rx->dqo.bufq.desc_ring = NULL; |
| 170 |
} |
| 171 |
|
| 172 |
if (rx->dqo.complq.desc_ring) { |
| 173 |
size = sizeof(rx->dqo.complq.desc_ring[0]) * |
| 174 |
completion_queue_slots; |
| 175 |
dma_free_coherent(hdev, size, rx->dqo.complq.desc_ring, |
| 176 |
rx->dqo.complq.bus); |
| 177 |
rx->dqo.complq.desc_ring = NULL; |
| 178 |
} |
| 179 |
|
| 180 |
kvfree(rx->dqo.buf_states); |
| 181 |
rx->dqo.buf_states = NULL; |
| 182 |
|
| 183 |
if (rx->dqo.page_pool) { |
| 184 |
page_pool_destroy(rx->dqo.page_pool); |
| 185 |
rx->dqo.page_pool = NULL; |
| 186 |
} |
| 187 |
|
| 188 |
gve_rx_free_hdr_bufs(priv, rx); |
| 189 |
|
| 190 |
netif_dbg(priv, drv, priv->dev, "freed rx ring %d\n", idx); |
| 191 |
} |
| 192 |
|
| 193 |
static int gve_rx_alloc_hdr_bufs(struct gve_priv *priv, struct gve_rx_ring *rx, |
| 194 |
const u32 buf_count) |
| 195 |
{ |
| 196 |
struct device *hdev = &priv->pdev->dev; |
| 197 |
|
| 198 |
rx->dqo.hdr_bufs.data = dma_alloc_coherent(hdev, priv->header_buf_size * buf_count, |
| 199 |
&rx->dqo.hdr_bufs.addr, GFP_KERNEL); |
| 200 |
if (!rx->dqo.hdr_bufs.data) |
| 201 |
return -ENOMEM; |
| 202 |
|
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
void gve_rx_start_ring_dqo(struct gve_priv *priv, int idx) |
| 207 |
{ |
| 208 |
int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx); |
| 209 |
|
| 210 |
gve_rx_add_to_block(priv, idx); |
| 211 |
gve_add_napi(priv, ntfy_idx, gve_napi_poll_dqo); |
| 212 |
} |
| 213 |
|
| 214 |
int gve_rx_alloc_ring_dqo(struct gve_priv *priv, |
| 215 |
struct gve_rx_alloc_rings_cfg *cfg, |
| 216 |
struct gve_rx_ring *rx, |
| 217 |
int idx) |
| 218 |
{ |
| 219 |
struct device *hdev = &priv->pdev->dev; |
| 220 |
struct page_pool *pool; |
| 221 |
int qpl_page_cnt; |
| 222 |
size_t size; |
| 223 |
u32 qpl_id; |
| 224 |
|
| 225 |
const u32 buffer_queue_slots = cfg->ring_size; |
| 226 |
const u32 completion_queue_slots = cfg->ring_size; |
| 227 |
|
| 228 |
netif_dbg(priv, drv, priv->dev, "allocating rx ring DQO\n"); |
| 229 |
|
| 230 |
memset(rx, 0, sizeof(*rx)); |
| 231 |
rx->gve = priv; |
| 232 |
rx->q_num = idx; |
| 233 |
rx->packet_buffer_size = cfg->packet_buffer_size; |
| 234 |
|
| 235 |
if (cfg->xdp) { |
| 236 |
rx->packet_buffer_truesize = GVE_XDP_RX_BUFFER_SIZE_DQO; |
| 237 |
rx->rx_headroom = XDP_PACKET_HEADROOM; |
| 238 |
} else { |
| 239 |
rx->packet_buffer_truesize = rx->packet_buffer_size; |
| 240 |
rx->rx_headroom = 0; |
| 241 |
} |
| 242 |
|
| 243 |
/* struct gve_xdp_buff is overlaid on struct xdp_buff_xsk and utilizes |
| 244 |
* the 24 byte field cb to store gve specific data. |
| 245 |
*/ |
| 246 |
XSK_CHECK_PRIV_TYPE(struct gve_xdp_buff); |
| 247 |
|
| 248 |
rx->dqo.num_buf_states = cfg->raw_addressing ? buffer_queue_slots : |
| 249 |
gve_get_rx_pages_per_qpl_dqo(cfg->ring_size); |
| 250 |
rx->dqo.buf_states = kvcalloc_node(rx->dqo.num_buf_states, |
| 251 |
sizeof(rx->dqo.buf_states[0]), |
| 252 |
GFP_KERNEL, priv->numa_node); |
| 253 |
if (!rx->dqo.buf_states) |
| 254 |
return -ENOMEM; |
| 255 |
|
| 256 |
/* Allocate header buffers for header-split */ |
| 257 |
if (cfg->enable_header_split) |
| 258 |
if (gve_rx_alloc_hdr_bufs(priv, rx, buffer_queue_slots)) |
| 259 |
goto err; |
| 260 |
|
| 261 |
/* Allocate RX completion queue */ |
| 262 |
size = sizeof(rx->dqo.complq.desc_ring[0]) * |
| 263 |
completion_queue_slots; |
| 264 |
rx->dqo.complq.desc_ring = |
| 265 |
dma_alloc_coherent(hdev, size, &rx->dqo.complq.bus, GFP_KERNEL); |
| 266 |
if (!rx->dqo.complq.desc_ring) |
| 267 |
goto err; |
| 268 |
|
| 269 |
/* Allocate RX buffer queue */ |
| 270 |
size = sizeof(rx->dqo.bufq.desc_ring[0]) * buffer_queue_slots; |
| 271 |
rx->dqo.bufq.desc_ring = |
| 272 |
dma_alloc_coherent(hdev, size, &rx->dqo.bufq.bus, GFP_KERNEL); |
| 273 |
if (!rx->dqo.bufq.desc_ring) |
| 274 |
goto err; |
| 275 |
|
| 276 |
if (cfg->raw_addressing) { |
| 277 |
pool = gve_rx_create_page_pool(priv, rx, cfg->xdp); |
| 278 |
if (IS_ERR(pool)) |
| 279 |
goto err; |
| 280 |
|
| 281 |
rx->dqo.page_pool = pool; |
| 282 |
} else { |
| 283 |
qpl_id = gve_get_rx_qpl_id(cfg->qcfg_tx, rx->q_num); |
| 284 |
qpl_page_cnt = gve_get_rx_pages_per_qpl_dqo(cfg->ring_size); |
| 285 |
|
| 286 |
rx->dqo.qpl = gve_alloc_queue_page_list(priv, qpl_id, |
| 287 |
qpl_page_cnt); |
| 288 |
if (!rx->dqo.qpl) |
| 289 |
goto err; |
| 290 |
rx->dqo.next_qpl_page_idx = 0; |
| 291 |
} |
| 292 |
|
| 293 |
rx->q_resources = dma_alloc_coherent(hdev, sizeof(*rx->q_resources), |
| 294 |
&rx->q_resources_bus, GFP_KERNEL); |
| 295 |
if (!rx->q_resources) |
| 296 |
goto err; |
| 297 |
|
| 298 |
gve_rx_init_ring_state_dqo(rx, buffer_queue_slots, |
| 299 |
completion_queue_slots); |
| 300 |
|
| 301 |
return 0; |
| 302 |
|
| 303 |
err: |
| 304 |
gve_rx_free_ring_dqo(priv, rx, cfg); |
| 305 |
return -ENOMEM; |
| 306 |
} |
| 307 |
|
| 308 |
void gve_rx_write_doorbell_dqo(const struct gve_priv *priv, int queue_idx) |
| 309 |
{ |
| 310 |
const struct gve_rx_ring *rx = &priv->rx[queue_idx]; |
| 311 |
u64 index = be32_to_cpu(rx->q_resources->db_index); |
| 312 |
|
| 313 |
iowrite32(rx->dqo.bufq.tail, &priv->db_bar2[index]); |
| 314 |
} |
| 315 |
|
| 316 |
int gve_rx_alloc_rings_dqo(struct gve_priv *priv, |
| 317 |
struct gve_rx_alloc_rings_cfg *cfg) |
| 318 |
{ |
| 319 |
struct gve_rx_ring *rx; |
| 320 |
int err; |
| 321 |
int i; |
| 322 |
|
| 323 |
rx = kvzalloc_objs(struct gve_rx_ring, cfg->qcfg_rx->max_queues); |
| 324 |
if (!rx) |
| 325 |
return -ENOMEM; |
| 326 |
|
| 327 |
for (i = 0; i < cfg->qcfg_rx->num_queues; i++) { |
| 328 |
err = gve_rx_alloc_ring_dqo(priv, cfg, &rx[i], i); |
| 329 |
if (err) { |
| 330 |
netif_err(priv, drv, priv->dev, |
| 331 |
"Failed to alloc rx ring=%d: err=%d\n", |
| 332 |
i, err); |
| 333 |
goto err; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
cfg->rx = rx; |
| 338 |
return 0; |
| 339 |
|
| 340 |
err: |
| 341 |
for (i--; i >= 0; i--) |
| 342 |
gve_rx_free_ring_dqo(priv, &rx[i], cfg); |
| 343 |
kvfree(rx); |
| 344 |
return err; |
| 345 |
} |
| 346 |
|
| 347 |
void gve_rx_free_rings_dqo(struct gve_priv *priv, |
| 348 |
struct gve_rx_alloc_rings_cfg *cfg) |
| 349 |
{ |
| 350 |
struct gve_rx_ring *rx = cfg->rx; |
| 351 |
int i; |
| 352 |
|
| 353 |
if (!rx) |
| 354 |
return; |
| 355 |
|
| 356 |
for (i = 0; i < cfg->qcfg_rx->num_queues; i++) |
| 357 |
gve_rx_free_ring_dqo(priv, &rx[i], cfg); |
| 358 |
|
| 359 |
kvfree(rx); |
| 360 |
cfg->rx = NULL; |
| 361 |
} |
| 362 |
|
| 363 |
void gve_rx_post_buffers_dqo(struct gve_rx_ring *rx) |
| 364 |
{ |
| 365 |
struct gve_rx_compl_queue_dqo *complq = &rx->dqo.complq; |
| 366 |
struct gve_rx_buf_queue_dqo *bufq = &rx->dqo.bufq; |
| 367 |
struct gve_priv *priv = rx->gve; |
| 368 |
u32 num_avail_slots; |
| 369 |
u32 num_full_slots; |
| 370 |
u32 num_posted = 0; |
| 371 |
|
| 372 |
num_full_slots = (bufq->tail - bufq->head) & bufq->mask; |
| 373 |
num_avail_slots = bufq->mask - num_full_slots; |
| 374 |
|
| 375 |
num_avail_slots = min_t(u32, num_avail_slots, complq->num_free_slots); |
| 376 |
while (num_posted < num_avail_slots) { |
| 377 |
struct gve_rx_desc_dqo *desc = &bufq->desc_ring[bufq->tail]; |
| 378 |
|
| 379 |
if (unlikely(gve_alloc_buffer(rx, desc))) { |
| 380 |
u64_stats_update_begin(&rx->statss); |
| 381 |
rx->rx_buf_alloc_fail++; |
| 382 |
u64_stats_update_end(&rx->statss); |
| 383 |
break; |
| 384 |
} |
| 385 |
|
| 386 |
if (rx->dqo.hdr_bufs.data) |
| 387 |
desc->header_buf_addr = |
| 388 |
cpu_to_le64(rx->dqo.hdr_bufs.addr + |
| 389 |
priv->header_buf_size * bufq->tail); |
| 390 |
|
| 391 |
bufq->tail = (bufq->tail + 1) & bufq->mask; |
| 392 |
complq->num_free_slots--; |
| 393 |
num_posted++; |
| 394 |
|
| 395 |
if ((bufq->tail & (GVE_RX_BUF_THRESH_DQO - 1)) == 0) |
| 396 |
gve_rx_write_doorbell_dqo(priv, rx->q_num); |
| 397 |
} |
| 398 |
|
| 399 |
rx->fill_cnt += num_posted; |
| 400 |
} |
| 401 |
|
| 402 |
static void gve_rx_skb_csum(struct sk_buff *skb, |
| 403 |
const struct gve_rx_compl_desc_dqo *desc, |
| 404 |
struct gve_ptype ptype) |
| 405 |
{ |
| 406 |
skb->ip_summed = CHECKSUM_NONE; |
| 407 |
|
| 408 |
/* HW did not identify and process L3 and L4 headers. */ |
| 409 |
if (unlikely(!desc->l3_l4_processed)) |
| 410 |
return; |
| 411 |
|
| 412 |
if (ptype.l3_type == GVE_L3_TYPE_IPV4) { |
| 413 |
if (unlikely(desc->csum_ip_err || desc->csum_external_ip_err)) |
| 414 |
return; |
| 415 |
} else if (ptype.l3_type == GVE_L3_TYPE_IPV6) { |
| 416 |
/* Checksum should be skipped if this flag is set. */ |
| 417 |
if (unlikely(desc->ipv6_ex_add)) |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
if (unlikely(desc->csum_l4_err)) |
| 422 |
return; |
| 423 |
|
| 424 |
switch (ptype.l4_type) { |
| 425 |
case GVE_L4_TYPE_TCP: |
| 426 |
case GVE_L4_TYPE_UDP: |
| 427 |
case GVE_L4_TYPE_ICMP: |
| 428 |
case GVE_L4_TYPE_SCTP: |
| 429 |
skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 430 |
break; |
| 431 |
default: |
| 432 |
break; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
static void gve_rx_skb_hash(struct sk_buff *skb, |
| 437 |
const struct gve_rx_compl_desc_dqo *compl_desc, |
| 438 |
struct gve_ptype ptype) |
| 439 |
{ |
| 440 |
enum pkt_hash_types hash_type = PKT_HASH_TYPE_L2; |
| 441 |
|
| 442 |
if (ptype.l4_type != GVE_L4_TYPE_UNKNOWN) |
| 443 |
hash_type = PKT_HASH_TYPE_L4; |
| 444 |
else if (ptype.l3_type != GVE_L3_TYPE_UNKNOWN) |
| 445 |
hash_type = PKT_HASH_TYPE_L3; |
| 446 |
|
| 447 |
skb_set_hash(skb, le32_to_cpu(compl_desc->hash), hash_type); |
| 448 |
} |
| 449 |
|
| 450 |
/* Expand the hardware timestamp to the full 64 bits of width, and add it to the |
| 451 |
* skb. |
| 452 |
* |
| 453 |
* This algorithm works by using the passed hardware timestamp to generate a |
| 454 |
* diff relative to the last read of the nic clock. This diff can be positive or |
| 455 |
* negative, as it is possible that we have read the clock more recently than |
| 456 |
* the hardware has received this packet. To detect this, we use the high bit of |
| 457 |
* the diff, and assume that the read is more recent if the high bit is set. In |
| 458 |
* this case we invert the process. |
| 459 |
* |
| 460 |
* Note that this means if the time delta between packet reception and the last |
| 461 |
* clock read is greater than ~2 seconds, this will provide invalid results. |
| 462 |
*/ |
| 463 |
static ktime_t gve_rx_get_hwtstamp(struct gve_priv *gve, u32 hwts) |
| 464 |
{ |
| 465 |
u64 last_read = READ_ONCE(gve->last_sync_nic_counter); |
| 466 |
u32 low = (u32)last_read; |
| 467 |
s32 diff = hwts - low; |
| 468 |
|
| 469 |
return ns_to_ktime(last_read + diff); |
| 470 |
} |
| 471 |
|
| 472 |
static void gve_rx_skb_hwtstamp(struct gve_rx_ring *rx, |
| 473 |
const struct gve_rx_compl_desc_dqo *desc) |
| 474 |
{ |
| 475 |
struct sk_buff *skb = rx->ctx.skb_head; |
| 476 |
|
| 477 |
if (desc->ts_sub_nsecs_low & GVE_DQO_RX_HWTSTAMP_VALID) |
| 478 |
skb_hwtstamps(skb)->hwtstamp = |
| 479 |
gve_rx_get_hwtstamp(rx->gve, le32_to_cpu(desc->ts)); |
| 480 |
} |
| 481 |
|
| 482 |
int gve_xdp_rx_timestamp(const struct xdp_md *_ctx, u64 *timestamp) |
| 483 |
{ |
| 484 |
const struct gve_xdp_buff *ctx = (void *)_ctx; |
| 485 |
|
| 486 |
if (!gve_is_clock_enabled(ctx->gve)) |
| 487 |
return -ENODATA; |
| 488 |
|
| 489 |
if (!(ctx->compl_desc->ts_sub_nsecs_low & GVE_DQO_RX_HWTSTAMP_VALID)) |
| 490 |
return -ENODATA; |
| 491 |
|
| 492 |
*timestamp = gve_rx_get_hwtstamp(ctx->gve, |
| 493 |
le32_to_cpu(ctx->compl_desc->ts)); |
| 494 |
return 0; |
| 495 |
} |
| 496 |
|
| 497 |
static void gve_rx_free_skb(struct napi_struct *napi, struct gve_rx_ring *rx) |
| 498 |
{ |
| 499 |
if (!rx->ctx.skb_head) |
| 500 |
return; |
| 501 |
|
| 502 |
if (rx->ctx.skb_head == napi->skb) |
| 503 |
napi->skb = NULL; |
| 504 |
dev_kfree_skb_any(rx->ctx.skb_head); |
| 505 |
rx->ctx.skb_head = NULL; |
| 506 |
rx->ctx.skb_tail = NULL; |
| 507 |
} |
| 508 |
|
| 509 |
static bool gve_rx_should_trigger_copy_ondemand(struct gve_rx_ring *rx) |
| 510 |
{ |
| 511 |
if (!rx->dqo.qpl) |
| 512 |
return false; |
| 513 |
if (rx->dqo.used_buf_states_cnt < |
| 514 |
(rx->dqo.num_buf_states - |
| 515 |
GVE_DQO_QPL_ONDEMAND_ALLOC_THRESHOLD)) |
| 516 |
return false; |
| 517 |
return true; |
| 518 |
} |
| 519 |
|
| 520 |
static int gve_rx_copy_ondemand(struct gve_rx_ring *rx, |
| 521 |
struct gve_rx_buf_state_dqo *buf_state, |
| 522 |
u16 buf_len) |
| 523 |
{ |
| 524 |
struct page *page = alloc_pages_node(rx->gve->numa_node, GFP_ATOMIC, 0); |
| 525 |
int num_frags; |
| 526 |
|
| 527 |
if (!page) |
| 528 |
return -ENOMEM; |
| 529 |
|
| 530 |
memcpy(page_address(page), |
| 531 |
buf_state->page_info.page_address + |
| 532 |
buf_state->page_info.page_offset, |
| 533 |
buf_len); |
| 534 |
num_frags = skb_shinfo(rx->ctx.skb_tail)->nr_frags; |
| 535 |
skb_add_rx_frag(rx->ctx.skb_tail, num_frags, page, |
| 536 |
0, buf_len, PAGE_SIZE); |
| 537 |
|
| 538 |
u64_stats_update_begin(&rx->statss); |
| 539 |
rx->rx_frag_alloc_cnt++; |
| 540 |
u64_stats_update_end(&rx->statss); |
| 541 |
/* Return unused buffer. */ |
| 542 |
gve_enqueue_buf_state(rx, &rx->dqo.recycled_buf_states, buf_state); |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
static void gve_skb_add_rx_frag(struct gve_rx_ring *rx, |
| 547 |
struct gve_rx_buf_state_dqo *buf_state, |
| 548 |
int num_frags, u16 buf_len) |
| 549 |
{ |
| 550 |
if (rx->dqo.page_pool) { |
| 551 |
skb_add_rx_frag_netmem(rx->ctx.skb_tail, num_frags, |
| 552 |
buf_state->page_info.netmem, |
| 553 |
buf_state->page_info.page_offset + |
| 554 |
buf_state->page_info.pad, buf_len, |
| 555 |
buf_state->page_info.buf_size); |
| 556 |
} else { |
| 557 |
skb_add_rx_frag(rx->ctx.skb_tail, num_frags, |
| 558 |
buf_state->page_info.page, |
| 559 |
buf_state->page_info.page_offset + |
| 560 |
buf_state->page_info.pad, buf_len, |
| 561 |
buf_state->page_info.buf_size); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/* Chains multi skbs for single rx packet. |
| 566 |
* Returns 0 if buffer is appended, -1 otherwise. |
| 567 |
*/ |
| 568 |
static int gve_rx_append_frags(struct napi_struct *napi, |
| 569 |
struct gve_rx_buf_state_dqo *buf_state, |
| 570 |
u16 buf_len, struct gve_rx_ring *rx, |
| 571 |
struct gve_priv *priv) |
| 572 |
{ |
| 573 |
int num_frags = skb_shinfo(rx->ctx.skb_tail)->nr_frags; |
| 574 |
|
| 575 |
if (unlikely(num_frags == MAX_SKB_FRAGS)) { |
| 576 |
struct sk_buff *skb; |
| 577 |
|
| 578 |
skb = napi_alloc_skb(napi, 0); |
| 579 |
if (!skb) |
| 580 |
return -1; |
| 581 |
|
| 582 |
if (rx->dqo.page_pool) |
| 583 |
skb_mark_for_recycle(skb); |
| 584 |
|
| 585 |
if (rx->ctx.skb_tail == rx->ctx.skb_head) |
| 586 |
skb_shinfo(rx->ctx.skb_head)->frag_list = skb; |
| 587 |
else |
| 588 |
rx->ctx.skb_tail->next = skb; |
| 589 |
rx->ctx.skb_tail = skb; |
| 590 |
num_frags = 0; |
| 591 |
} |
| 592 |
if (rx->ctx.skb_tail != rx->ctx.skb_head) { |
| 593 |
rx->ctx.skb_head->len += buf_len; |
| 594 |
rx->ctx.skb_head->data_len += buf_len; |
| 595 |
rx->ctx.skb_head->truesize += buf_state->page_info.buf_size; |
| 596 |
} |
| 597 |
|
| 598 |
/* Trigger ondemand page allocation if we are running low on buffers */ |
| 599 |
if (gve_rx_should_trigger_copy_ondemand(rx)) |
| 600 |
return gve_rx_copy_ondemand(rx, buf_state, buf_len); |
| 601 |
|
| 602 |
gve_skb_add_rx_frag(rx, buf_state, num_frags, buf_len); |
| 603 |
gve_reuse_buffer(rx, buf_state); |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
static int gve_xdp_tx_dqo(struct gve_priv *priv, struct gve_rx_ring *rx, |
| 608 |
struct xdp_buff *xdp) |
| 609 |
{ |
| 610 |
struct gve_tx_ring *tx; |
| 611 |
struct xdp_frame *xdpf; |
| 612 |
u32 tx_qid; |
| 613 |
int err; |
| 614 |
|
| 615 |
xdpf = xdp_convert_buff_to_frame(xdp); |
| 616 |
if (unlikely(!xdpf)) { |
| 617 |
if (rx->xsk_pool) |
| 618 |
xsk_buff_free(xdp); |
| 619 |
return -ENOSPC; |
| 620 |
} |
| 621 |
|
| 622 |
tx_qid = gve_xdp_tx_queue_id(priv, rx->q_num); |
| 623 |
tx = &priv->tx[tx_qid]; |
| 624 |
spin_lock(&tx->dqo_tx.xdp_lock); |
| 625 |
err = gve_xdp_xmit_one_dqo(priv, tx, xdpf); |
| 626 |
spin_unlock(&tx->dqo_tx.xdp_lock); |
| 627 |
|
| 628 |
return err; |
| 629 |
} |
| 630 |
|
| 631 |
static void gve_xsk_done_dqo(struct gve_priv *priv, struct gve_rx_ring *rx, |
| 632 |
struct xdp_buff *xdp, struct bpf_prog *xprog, |
| 633 |
int xdp_act) |
| 634 |
{ |
| 635 |
switch (xdp_act) { |
| 636 |
case XDP_ABORTED: |
| 637 |
case XDP_DROP: |
| 638 |
default: |
| 639 |
xsk_buff_free(xdp); |
| 640 |
break; |
| 641 |
case XDP_TX: |
| 642 |
if (unlikely(gve_xdp_tx_dqo(priv, rx, xdp))) |
| 643 |
goto err; |
| 644 |
break; |
| 645 |
case XDP_REDIRECT: |
| 646 |
if (unlikely(xdp_do_redirect(priv->dev, xdp, xprog))) |
| 647 |
goto err; |
| 648 |
break; |
| 649 |
} |
| 650 |
|
| 651 |
u64_stats_update_begin(&rx->statss); |
| 652 |
if ((u32)xdp_act < GVE_XDP_ACTIONS) |
| 653 |
rx->xdp_actions[xdp_act]++; |
| 654 |
u64_stats_update_end(&rx->statss); |
| 655 |
return; |
| 656 |
|
| 657 |
err: |
| 658 |
u64_stats_update_begin(&rx->statss); |
| 659 |
if (xdp_act == XDP_TX) |
| 660 |
rx->xdp_tx_errors++; |
| 661 |
if (xdp_act == XDP_REDIRECT) |
| 662 |
rx->xdp_redirect_errors++; |
| 663 |
u64_stats_update_end(&rx->statss); |
| 664 |
} |
| 665 |
|
| 666 |
static void gve_xdp_done_dqo(struct gve_priv *priv, struct gve_rx_ring *rx, |
| 667 |
struct xdp_buff *xdp, struct bpf_prog *xprog, |
| 668 |
int xdp_act, |
| 669 |
struct gve_rx_buf_state_dqo *buf_state) |
| 670 |
{ |
| 671 |
int err; |
| 672 |
switch (xdp_act) { |
| 673 |
case XDP_ABORTED: |
| 674 |
case XDP_DROP: |
| 675 |
default: |
| 676 |
gve_free_buffer(rx, buf_state); |
| 677 |
break; |
| 678 |
case XDP_TX: |
| 679 |
err = gve_xdp_tx_dqo(priv, rx, xdp); |
| 680 |
if (unlikely(err)) |
| 681 |
goto err; |
| 682 |
gve_reuse_buffer(rx, buf_state); |
| 683 |
break; |
| 684 |
case XDP_REDIRECT: |
| 685 |
err = xdp_do_redirect(priv->dev, xdp, xprog); |
| 686 |
if (unlikely(err)) |
| 687 |
goto err; |
| 688 |
gve_reuse_buffer(rx, buf_state); |
| 689 |
break; |
| 690 |
} |
| 691 |
u64_stats_update_begin(&rx->statss); |
| 692 |
if ((u32)xdp_act < GVE_XDP_ACTIONS) |
| 693 |
rx->xdp_actions[xdp_act]++; |
| 694 |
u64_stats_update_end(&rx->statss); |
| 695 |
return; |
| 696 |
err: |
| 697 |
u64_stats_update_begin(&rx->statss); |
| 698 |
if (xdp_act == XDP_TX) |
| 699 |
rx->xdp_tx_errors++; |
| 700 |
else if (xdp_act == XDP_REDIRECT) |
| 701 |
rx->xdp_redirect_errors++; |
| 702 |
u64_stats_update_end(&rx->statss); |
| 703 |
gve_free_buffer(rx, buf_state); |
| 704 |
return; |
| 705 |
} |
| 706 |
|
| 707 |
static int gve_rx_xsk_dqo(struct napi_struct *napi, struct gve_rx_ring *rx, |
| 708 |
const struct gve_rx_compl_desc_dqo *compl_desc, |
| 709 |
struct gve_rx_buf_state_dqo *buf_state, |
| 710 |
struct bpf_prog *xprog) |
| 711 |
{ |
| 712 |
struct xdp_buff *xdp = buf_state->xsk_buff; |
| 713 |
int buf_len = compl_desc->packet_len; |
| 714 |
struct gve_priv *priv = rx->gve; |
| 715 |
struct gve_xdp_buff *gve_xdp; |
| 716 |
int xdp_act; |
| 717 |
|
| 718 |
xdp->data_end = xdp->data + buf_len; |
| 719 |
xsk_buff_dma_sync_for_cpu(xdp); |
| 720 |
|
| 721 |
gve_xdp = (void *)xdp; |
| 722 |
gve_xdp->gve = priv; |
| 723 |
gve_xdp->compl_desc = compl_desc; |
| 724 |
|
| 725 |
if (xprog) { |
| 726 |
xdp_act = bpf_prog_run_xdp(xprog, xdp); |
| 727 |
buf_len = xdp->data_end - xdp->data; |
| 728 |
if (xdp_act != XDP_PASS) { |
| 729 |
gve_xsk_done_dqo(priv, rx, xdp, xprog, xdp_act); |
| 730 |
gve_free_buf_state(rx, buf_state); |
| 731 |
return 0; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
/* Copy the data to skb */ |
| 736 |
rx->ctx.skb_head = gve_rx_copy_data(priv->dev, napi, |
| 737 |
xdp->data, buf_len); |
| 738 |
if (unlikely(!rx->ctx.skb_head)) { |
| 739 |
xsk_buff_free(xdp); |
| 740 |
gve_free_buf_state(rx, buf_state); |
| 741 |
return -ENOMEM; |
| 742 |
} |
| 743 |
rx->ctx.skb_tail = rx->ctx.skb_head; |
| 744 |
|
| 745 |
/* Free XSK buffer and Buffer state */ |
| 746 |
xsk_buff_free(xdp); |
| 747 |
gve_free_buf_state(rx, buf_state); |
| 748 |
|
| 749 |
/* Update Stats */ |
| 750 |
u64_stats_update_begin(&rx->statss); |
| 751 |
rx->xdp_actions[XDP_PASS]++; |
| 752 |
u64_stats_update_end(&rx->statss); |
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
static void gve_dma_sync(struct gve_priv *priv, struct gve_rx_ring *rx, |
| 757 |
struct gve_rx_buf_state_dqo *buf_state, u16 buf_len) |
| 758 |
{ |
| 759 |
struct gve_rx_slot_page_info *page_info = &buf_state->page_info; |
| 760 |
|
| 761 |
if (rx->dqo.page_pool) { |
| 762 |
page_pool_dma_sync_netmem_for_cpu(rx->dqo.page_pool, |
| 763 |
page_info->netmem, |
| 764 |
page_info->page_offset, |
| 765 |
buf_len); |
| 766 |
} else { |
| 767 |
dma_sync_single_range_for_cpu(&priv->pdev->dev, buf_state->addr, |
| 768 |
page_info->page_offset + |
| 769 |
page_info->pad, |
| 770 |
buf_len, DMA_FROM_DEVICE); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
/* Returns 0 if descriptor is completed successfully. |
| 775 |
* Returns -EINVAL if descriptor is invalid. |
| 776 |
* Returns -ENOMEM if data cannot be copied to skb. |
| 777 |
*/ |
| 778 |
static int gve_rx_dqo(struct napi_struct *napi, struct gve_rx_ring *rx, |
| 779 |
const struct gve_rx_compl_desc_dqo *compl_desc, |
| 780 |
u32 desc_idx, int queue_idx) |
| 781 |
{ |
| 782 |
const u16 buffer_id = le16_to_cpu(compl_desc->buf_id); |
| 783 |
const bool hbo = compl_desc->header_buffer_overflow; |
| 784 |
const bool eop = compl_desc->end_of_packet != 0; |
| 785 |
const bool hsplit = compl_desc->split_header; |
| 786 |
struct gve_rx_buf_state_dqo *buf_state; |
| 787 |
struct gve_priv *priv = rx->gve; |
| 788 |
struct bpf_prog *xprog; |
| 789 |
u16 buf_len; |
| 790 |
u16 hdr_len; |
| 791 |
|
| 792 |
if (unlikely(buffer_id >= rx->dqo.num_buf_states)) { |
| 793 |
net_err_ratelimited("%s: Invalid RX buffer_id=%u\n", |
| 794 |
priv->dev->name, buffer_id); |
| 795 |
return -EINVAL; |
| 796 |
} |
| 797 |
buf_state = &rx->dqo.buf_states[buffer_id]; |
| 798 |
if (unlikely(!gve_buf_state_is_allocated(rx, buf_state))) { |
| 799 |
net_err_ratelimited("%s: RX buffer_id is not allocated: %u\n", |
| 800 |
priv->dev->name, buffer_id); |
| 801 |
return -EINVAL; |
| 802 |
} |
| 803 |
|
| 804 |
if (unlikely(compl_desc->rx_error)) { |
| 805 |
gve_free_buffer(rx, buf_state); |
| 806 |
return -EINVAL; |
| 807 |
} |
| 808 |
|
| 809 |
buf_len = compl_desc->packet_len; |
| 810 |
hdr_len = compl_desc->header_len; |
| 811 |
|
| 812 |
xprog = READ_ONCE(priv->xdp_prog); |
| 813 |
if (buf_state->xsk_buff) |
| 814 |
return gve_rx_xsk_dqo(napi, rx, compl_desc, buf_state, xprog); |
| 815 |
|
| 816 |
/* Page might have not been used for awhile and was likely last written |
| 817 |
* by a different thread. |
| 818 |
*/ |
| 819 |
if (rx->dqo.page_pool) { |
| 820 |
if (!netmem_is_net_iov(buf_state->page_info.netmem)) |
| 821 |
prefetch(netmem_to_page(buf_state->page_info.netmem)); |
| 822 |
} else { |
| 823 |
prefetch(buf_state->page_info.page); |
| 824 |
} |
| 825 |
|
| 826 |
/* Copy the header into the skb in the case of header split */ |
| 827 |
if (hsplit) { |
| 828 |
int unsplit = 0; |
| 829 |
|
| 830 |
if (hdr_len && !hbo) { |
| 831 |
rx->ctx.skb_head = gve_rx_copy_data(priv->dev, napi, |
| 832 |
rx->dqo.hdr_bufs.data + |
| 833 |
desc_idx * priv->header_buf_size, |
| 834 |
hdr_len); |
| 835 |
if (unlikely(!rx->ctx.skb_head)) |
| 836 |
goto error; |
| 837 |
rx->ctx.skb_tail = rx->ctx.skb_head; |
| 838 |
|
| 839 |
if (rx->dqo.page_pool) |
| 840 |
skb_mark_for_recycle(rx->ctx.skb_head); |
| 841 |
} else { |
| 842 |
unsplit = 1; |
| 843 |
} |
| 844 |
u64_stats_update_begin(&rx->statss); |
| 845 |
rx->rx_hsplit_pkt++; |
| 846 |
rx->rx_hsplit_unsplit_pkt += unsplit; |
| 847 |
rx->rx_hsplit_bytes += hdr_len; |
| 848 |
u64_stats_update_end(&rx->statss); |
| 849 |
} else if (!rx->ctx.skb_head && rx->dqo.page_pool && |
| 850 |
netmem_is_net_iov(buf_state->page_info.netmem)) { |
| 851 |
/* when header split is disabled, the header went to the packet |
| 852 |
* buffer. If the packet buffer is a net_iov, those can't be |
| 853 |
* easily mapped into the kernel space to access the header |
| 854 |
* required to process the packet. |
| 855 |
*/ |
| 856 |
goto error; |
| 857 |
} |
| 858 |
|
| 859 |
/* Sync the portion of dma buffer for CPU to read. */ |
| 860 |
gve_dma_sync(priv, rx, buf_state, buf_len); |
| 861 |
|
| 862 |
/* Append to current skb if one exists. */ |
| 863 |
if (rx->ctx.skb_head) { |
| 864 |
if (unlikely(gve_rx_append_frags(napi, buf_state, buf_len, rx, |
| 865 |
priv)) != 0) { |
| 866 |
goto error; |
| 867 |
} |
| 868 |
return 0; |
| 869 |
} |
| 870 |
|
| 871 |
if (xprog) { |
| 872 |
struct gve_xdp_buff gve_xdp; |
| 873 |
void *old_data; |
| 874 |
int xdp_act; |
| 875 |
|
| 876 |
xdp_init_buff(&gve_xdp.xdp, buf_state->page_info.buf_size, |
| 877 |
&rx->xdp_rxq); |
| 878 |
xdp_prepare_buff(&gve_xdp.xdp, |
| 879 |
buf_state->page_info.page_address + |
| 880 |
buf_state->page_info.page_offset, |
| 881 |
buf_state->page_info.pad, |
| 882 |
buf_len, false); |
| 883 |
gve_xdp.gve = priv; |
| 884 |
gve_xdp.compl_desc = compl_desc; |
| 885 |
|
| 886 |
old_data = gve_xdp.xdp.data; |
| 887 |
xdp_act = bpf_prog_run_xdp(xprog, &gve_xdp.xdp); |
| 888 |
buf_state->page_info.pad += gve_xdp.xdp.data - old_data; |
| 889 |
buf_len = gve_xdp.xdp.data_end - gve_xdp.xdp.data; |
| 890 |
if (xdp_act != XDP_PASS) { |
| 891 |
gve_xdp_done_dqo(priv, rx, &gve_xdp.xdp, xprog, xdp_act, |
| 892 |
buf_state); |
| 893 |
return 0; |
| 894 |
} |
| 895 |
|
| 896 |
u64_stats_update_begin(&rx->statss); |
| 897 |
rx->xdp_actions[XDP_PASS]++; |
| 898 |
u64_stats_update_end(&rx->statss); |
| 899 |
} |
| 900 |
|
| 901 |
if (eop && buf_len <= priv->rx_copybreak && |
| 902 |
!(rx->dqo.page_pool && |
| 903 |
netmem_is_net_iov(buf_state->page_info.netmem))) { |
| 904 |
rx->ctx.skb_head = gve_rx_copy(priv->dev, napi, |
| 905 |
&buf_state->page_info, buf_len); |
| 906 |
if (unlikely(!rx->ctx.skb_head)) |
| 907 |
goto error; |
| 908 |
rx->ctx.skb_tail = rx->ctx.skb_head; |
| 909 |
|
| 910 |
u64_stats_update_begin(&rx->statss); |
| 911 |
rx->rx_copied_pkt++; |
| 912 |
rx->rx_copybreak_pkt++; |
| 913 |
u64_stats_update_end(&rx->statss); |
| 914 |
|
| 915 |
gve_free_buffer(rx, buf_state); |
| 916 |
return 0; |
| 917 |
} |
| 918 |
|
| 919 |
rx->ctx.skb_head = napi_get_frags(napi); |
| 920 |
if (unlikely(!rx->ctx.skb_head)) |
| 921 |
goto error; |
| 922 |
rx->ctx.skb_tail = rx->ctx.skb_head; |
| 923 |
|
| 924 |
if (gve_rx_should_trigger_copy_ondemand(rx)) { |
| 925 |
if (gve_rx_copy_ondemand(rx, buf_state, buf_len) < 0) |
| 926 |
goto error; |
| 927 |
return 0; |
| 928 |
} |
| 929 |
|
| 930 |
if (rx->dqo.page_pool) |
| 931 |
skb_mark_for_recycle(rx->ctx.skb_head); |
| 932 |
|
| 933 |
gve_skb_add_rx_frag(rx, buf_state, 0, buf_len); |
| 934 |
gve_reuse_buffer(rx, buf_state); |
| 935 |
return 0; |
| 936 |
|
| 937 |
error: |
| 938 |
gve_free_buffer(rx, buf_state); |
| 939 |
return -ENOMEM; |
| 940 |
} |
| 941 |
|
| 942 |
static int gve_rx_complete_rsc(struct sk_buff *skb, |
| 943 |
const struct gve_rx_compl_desc_dqo *desc, |
| 944 |
struct gve_ptype ptype) |
| 945 |
{ |
| 946 |
struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 947 |
|
| 948 |
/* Only TCP is supported right now. */ |
| 949 |
if (ptype.l4_type != GVE_L4_TYPE_TCP) |
| 950 |
return -EINVAL; |
| 951 |
|
| 952 |
switch (ptype.l3_type) { |
| 953 |
case GVE_L3_TYPE_IPV4: |
| 954 |
shinfo->gso_type = SKB_GSO_TCPV4; |
| 955 |
break; |
| 956 |
case GVE_L3_TYPE_IPV6: |
| 957 |
shinfo->gso_type = SKB_GSO_TCPV6; |
| 958 |
break; |
| 959 |
default: |
| 960 |
return -EINVAL; |
| 961 |
} |
| 962 |
|
| 963 |
shinfo->gso_size = le16_to_cpu(desc->rsc_seg_len); |
| 964 |
return 0; |
| 965 |
} |
| 966 |
|
| 967 |
/* Returns 0 if skb is completed successfully, -1 otherwise. */ |
| 968 |
static int gve_rx_complete_skb(struct gve_rx_ring *rx, struct napi_struct *napi, |
| 969 |
const struct gve_rx_compl_desc_dqo *desc, |
| 970 |
netdev_features_t feat) |
| 971 |
{ |
| 972 |
struct gve_ptype ptype = |
| 973 |
rx->gve->ptype_lut_dqo->ptypes[desc->packet_type]; |
| 974 |
int err; |
| 975 |
|
| 976 |
skb_record_rx_queue(rx->ctx.skb_head, rx->q_num); |
| 977 |
|
| 978 |
if (feat & NETIF_F_RXHASH) |
| 979 |
gve_rx_skb_hash(rx->ctx.skb_head, desc, ptype); |
| 980 |
|
| 981 |
if (feat & NETIF_F_RXCSUM) |
| 982 |
gve_rx_skb_csum(rx->ctx.skb_head, desc, ptype); |
| 983 |
|
| 984 |
if (rx->gve->ts_config.rx_filter == HWTSTAMP_FILTER_ALL) |
| 985 |
gve_rx_skb_hwtstamp(rx, desc); |
| 986 |
|
| 987 |
/* RSC packets must set gso_size otherwise the TCP stack will complain |
| 988 |
* that packets are larger than MTU. |
| 989 |
*/ |
| 990 |
if (desc->rsc) { |
| 991 |
err = gve_rx_complete_rsc(rx->ctx.skb_head, desc, ptype); |
| 992 |
if (err < 0) |
| 993 |
return err; |
| 994 |
} |
| 995 |
|
| 996 |
if (skb_headlen(rx->ctx.skb_head) == 0) |
| 997 |
napi_gro_frags(napi); |
| 998 |
else |
| 999 |
napi_gro_receive(napi, rx->ctx.skb_head); |
| 1000 |
|
| 1001 |
return 0; |
| 1002 |
} |
| 1003 |
|
| 1004 |
int gve_rx_poll_dqo(struct gve_notify_block *block, int budget) |
| 1005 |
{ |
| 1006 |
struct gve_rx_compl_queue_dqo *complq; |
| 1007 |
struct napi_struct *napi; |
| 1008 |
netdev_features_t feat; |
| 1009 |
struct gve_rx_ring *rx; |
| 1010 |
struct gve_priv *priv; |
| 1011 |
u64 xdp_redirects; |
| 1012 |
u32 work_done = 0; |
| 1013 |
u64 bytes = 0; |
| 1014 |
u64 xdp_txs; |
| 1015 |
int err; |
| 1016 |
|
| 1017 |
napi = &block->napi; |
| 1018 |
feat = napi->dev->features; |
| 1019 |
|
| 1020 |
rx = block->rx; |
| 1021 |
priv = rx->gve; |
| 1022 |
complq = &rx->dqo.complq; |
| 1023 |
|
| 1024 |
xdp_redirects = rx->xdp_actions[XDP_REDIRECT]; |
| 1025 |
xdp_txs = rx->xdp_actions[XDP_TX]; |
| 1026 |
|
| 1027 |
while (work_done < budget) { |
| 1028 |
struct gve_rx_compl_desc_dqo *compl_desc = |
| 1029 |
&complq->desc_ring[complq->head]; |
| 1030 |
u32 pkt_bytes; |
| 1031 |
|
| 1032 |
/* No more new packets */ |
| 1033 |
if (compl_desc->generation == complq->cur_gen_bit) |
| 1034 |
break; |
| 1035 |
|
| 1036 |
/* Prefetch the next two descriptors. */ |
| 1037 |
prefetch(&complq->desc_ring[(complq->head + 1) & complq->mask]); |
| 1038 |
prefetch(&complq->desc_ring[(complq->head + 2) & complq->mask]); |
| 1039 |
|
| 1040 |
/* Do not read data until we own the descriptor */ |
| 1041 |
dma_rmb(); |
| 1042 |
|
| 1043 |
err = gve_rx_dqo(napi, rx, compl_desc, complq->head, rx->q_num); |
| 1044 |
if (err < 0) { |
| 1045 |
gve_rx_free_skb(napi, rx); |
| 1046 |
u64_stats_update_begin(&rx->statss); |
| 1047 |
if (err == -ENOMEM) |
| 1048 |
rx->rx_skb_alloc_fail++; |
| 1049 |
else if (err == -EINVAL) |
| 1050 |
rx->rx_desc_err_dropped_pkt++; |
| 1051 |
u64_stats_update_end(&rx->statss); |
| 1052 |
} |
| 1053 |
|
| 1054 |
complq->head = (complq->head + 1) & complq->mask; |
| 1055 |
complq->num_free_slots++; |
| 1056 |
|
| 1057 |
/* When the ring wraps, the generation bit is flipped. */ |
| 1058 |
complq->cur_gen_bit ^= (complq->head == 0); |
| 1059 |
|
| 1060 |
/* Receiving a completion means we have space to post another |
| 1061 |
* buffer on the buffer queue. |
| 1062 |
*/ |
| 1063 |
{ |
| 1064 |
struct gve_rx_buf_queue_dqo *bufq = &rx->dqo.bufq; |
| 1065 |
|
| 1066 |
bufq->head = (bufq->head + 1) & bufq->mask; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* Free running counter of completed descriptors */ |
| 1070 |
rx->cnt++; |
| 1071 |
|
| 1072 |
if (!rx->ctx.skb_head) |
| 1073 |
continue; |
| 1074 |
|
| 1075 |
if (!compl_desc->end_of_packet) |
| 1076 |
continue; |
| 1077 |
|
| 1078 |
work_done++; |
| 1079 |
pkt_bytes = rx->ctx.skb_head->len; |
| 1080 |
/* The ethernet header (first ETH_HLEN bytes) is snipped off |
| 1081 |
* by eth_type_trans. |
| 1082 |
*/ |
| 1083 |
if (skb_headlen(rx->ctx.skb_head)) |
| 1084 |
pkt_bytes += ETH_HLEN; |
| 1085 |
|
| 1086 |
/* gve_rx_complete_skb() will consume skb if successful */ |
| 1087 |
if (gve_rx_complete_skb(rx, napi, compl_desc, feat) != 0) { |
| 1088 |
gve_rx_free_skb(napi, rx); |
| 1089 |
u64_stats_update_begin(&rx->statss); |
| 1090 |
rx->rx_desc_err_dropped_pkt++; |
| 1091 |
u64_stats_update_end(&rx->statss); |
| 1092 |
continue; |
| 1093 |
} |
| 1094 |
|
| 1095 |
bytes += pkt_bytes; |
| 1096 |
rx->ctx.skb_head = NULL; |
| 1097 |
rx->ctx.skb_tail = NULL; |
| 1098 |
} |
| 1099 |
|
| 1100 |
if (xdp_txs != rx->xdp_actions[XDP_TX]) |
| 1101 |
gve_xdp_tx_flush_dqo(priv, rx->q_num); |
| 1102 |
|
| 1103 |
if (xdp_redirects != rx->xdp_actions[XDP_REDIRECT]) |
| 1104 |
xdp_do_flush(); |
| 1105 |
|
| 1106 |
gve_rx_post_buffers_dqo(rx); |
| 1107 |
|
| 1108 |
u64_stats_update_begin(&rx->statss); |
| 1109 |
rx->rpackets += work_done; |
| 1110 |
rx->rbytes += bytes; |
| 1111 |
u64_stats_update_end(&rx->statss); |
| 1112 |
|
| 1113 |
return work_done; |
| 1114 |
} |