/*
* Copyright (C) 2017 Netronome Systems, Inc.
*
* This software is licensed under the GNU General License Version 2,
* June 1991 as shown in the file COPYING in the top-level directory of this
* source tree.
*
* THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
* OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
* THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
*/
#include <linux/debugfs.h>
#include <linux/etherdevice.h>
#include <linux/ethtool_netlink.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/slab.h>
#include <net/netdev_queues.h>
#include <net/netdev_rx_queue.h>
#include <net/page_pool/helpers.h>
#include <net/netlink.h>
#include <net/net_shaper.h>
#include <net/netdev_lock.h>
#include <net/pkt_cls.h>
#include <net/rtnetlink.h>
#include <net/udp_tunnel.h>
#include <net/busy_poll.h>
#include "netdevsim.h"
MODULE_IMPORT_NS("NETDEV_INTERNAL");
#define NSIM_RING_SIZE 256
static void nsim_start_peer_tx_queue(struct net_device *dev, struct nsim_rq *rq)
{
struct netdevsim *ns = netdev_priv(dev);
struct net_device *peer_dev;
struct netdevsim *peer_ns;
struct netdev_queue *txq;
u16 idx;
idx = rq->napi.index;
rcu_read_lock();
peer_ns = rcu_dereference(ns->peer);
if (!peer_ns)
goto out;
/* TX device */
peer_dev = peer_ns->netdev;
if (dev->real_num_tx_queues != peer_dev->num_rx_queues)
goto out;
txq = netdev_get_tx_queue(peer_dev, idx);
if (!netif_tx_queue_stopped(txq))
goto out;
netif_tx_wake_queue(txq);
out:
rcu_read_unlock();
}
static void nsim_stop_tx_queue(struct net_device *tx_dev,
struct net_device *rx_dev,
struct nsim_rq *rq,
u16 idx)
{
/* If different queues size, do not stop, since it is not
* easy to find which TX queue is mapped here
*/
if (rx_dev->real_num_tx_queues != tx_dev->num_rx_queues)
return;
/* rq is the queue on the receive side */
netif_subqueue_try_stop(tx_dev, idx,
NSIM_RING_SIZE - skb_queue_len(&rq->skb_queue),
NSIM_RING_SIZE / 2);
}
static int nsim_napi_rx(struct net_device *tx_dev, struct net_device *rx_dev,
struct nsim_rq *rq, struct sk_buff *skb)
{
if (skb_queue_len(&rq->skb_queue) > NSIM_RING_SIZE) {
dev_kfree_skb_any(skb);
return NET_RX_DROP;
}
skb_queue_tail(&rq->skb_queue, skb);
/* Stop the peer TX queue avoiding dropping packets later */
if (skb_queue_len(&rq->skb_queue) >= NSIM_RING_SIZE)
nsim_stop_tx_queue(tx_dev