1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/drivers/net/ethernet/netronome/nfp/nfp_net_repr.h
Gustavo A. R. Silva 3f6e963305 netronome: Replace zero-length array with flexible-array member
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:

struct foo {
        int stuff;
        struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.

Also, notice that, dynamic memory allocations won't be affected by
this change:

"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]

This issue was found with the help of Coccinelle.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 7649773293 ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2020-02-24 15:26:17 -08:00

114 lines
3 KiB
C

/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
#ifndef NFP_NET_REPR_H
#define NFP_NET_REPR_H
struct metadata_dst;
struct nfp_app;
struct nfp_net;
struct nfp_port;
#include <net/dst_metadata.h>
/**
* struct nfp_reprs - container for representor netdevs
* @num_reprs: Number of elements in reprs array
* @reprs: Array of representor netdevs
*/
struct nfp_reprs {
unsigned int num_reprs;
struct net_device __rcu *reprs[];
};
/**
* struct nfp_repr_pcpu_stats
* @rx_packets: Received packets
* @rx_bytes: Received bytes
* @tx_packets: Transmitted packets
* @tx_bytes: Transmitted dropped
* @tx_drops: Packets dropped on transmit
* @syncp: Reference count
*/
struct nfp_repr_pcpu_stats {
u64 rx_packets;
u64 rx_bytes;
u64 tx_packets;
u64 tx_bytes;
u64 tx_drops;
struct u64_stats_sync syncp;
};
/**
* struct nfp_repr - priv data for representor netdevs
* @netdev: Back pointer to netdev
* @dst: Destination for packet TX
* @port: Port of representor
* @app: APP handle
* @stats: Statistic of packets hitting CPU
* @app_priv: Pointer for APP data
*/
struct nfp_repr {
struct net_device *netdev;
struct metadata_dst *dst;
struct nfp_port *port;
struct nfp_app *app;
struct nfp_repr_pcpu_stats __percpu *stats;
void *app_priv;
};
/**
* enum nfp_repr_type - type of representor
* @NFP_REPR_TYPE_PHYS_PORT: external NIC port
* @NFP_REPR_TYPE_PF: physical function
* @NFP_REPR_TYPE_VF: virtual function
* @__NFP_REPR_TYPE_MAX: number of representor types
*/
enum nfp_repr_type {
NFP_REPR_TYPE_PHYS_PORT,
NFP_REPR_TYPE_PF,
NFP_REPR_TYPE_VF,
__NFP_REPR_TYPE_MAX,
};
#define NFP_REPR_TYPE_MAX (__NFP_REPR_TYPE_MAX - 1)
extern const struct net_device_ops nfp_repr_netdev_ops;
static inline bool nfp_netdev_is_nfp_repr(struct net_device *netdev)
{
return netdev->netdev_ops == &nfp_repr_netdev_ops;
}
static inline int nfp_repr_get_port_id(struct net_device *netdev)
{
struct nfp_repr *priv = netdev_priv(netdev);
return priv->dst->u.port_info.port_id;
}
struct net_device *
nfp_repr_get_locked(struct nfp_app *app, struct nfp_reprs *set,
unsigned int id);
void nfp_repr_inc_rx_stats(struct net_device *netdev, unsigned int len);
void
nfp_repr_transfer_features(struct net_device *netdev, struct net_device *lower);
int nfp_repr_init(struct nfp_app *app, struct net_device *netdev,
u32 cmsg_port_id, struct nfp_port *port,
struct net_device *pf_netdev);
void nfp_repr_free(struct net_device *netdev);
struct net_device *
nfp_repr_alloc_mqs(struct nfp_app *app, unsigned int txqs, unsigned int rxqs);
void nfp_repr_clean_and_free(struct nfp_repr *repr);
void nfp_reprs_clean_and_free(struct nfp_app *app, struct nfp_reprs *reprs);
void nfp_reprs_clean_and_free_by_type(struct nfp_app *app,
enum nfp_repr_type type);
struct nfp_reprs *nfp_reprs_alloc(unsigned int num_reprs);
int nfp_reprs_resync_phys_ports(struct nfp_app *app);
static inline struct net_device *nfp_repr_alloc(struct nfp_app *app)
{
return nfp_repr_alloc_mqs(app, 1, 1);
}
#endif /* NFP_NET_REPR_H */