1 #ifndef __DPDK_IFACE_H__
2 #define __DPDK_IFACE_H__
3 /*--------------------------------------------------------------------------*/
4 #include <linux/netdevice.h>
5 #include "dpdk_iface_common.h"
6 /*--------------------------------------------------------------------------*/
7 #define IFACE_PREFIX		"dpdk"
8 /*--------------------------------------------------------------------------*/
9 /**
10  * net adapter private struct
11  */
12 struct net_adapter {
13 	struct net_device *netdev;
14 	unsigned char mac_addr[ETH_ALEN];
15 	u16 bd_number;
16 	bool netdev_registered;
17 	int numa_socket;
18 	struct net_device_stats nstats;
19 	struct PciAddress pa;
20 };
21 /*--------------------------------------------------------------------------*/
22 /**
23  * stats struct passed on from user space to the driver
24  */
25 struct stats_struct {
26 	uint64_t tx_bytes;
27 	uint64_t tx_pkts;
28 	uint64_t rx_bytes;
29 	uint64_t rx_pkts;
30 	uint64_t rmiss;
31 	uint64_t rerr;
32 	uint64_t terr;
33 	uint8_t qid;
34 	uint8_t dev;
35 };
36 /*--------------------------------------------------------------------------*/
37 /* sarray declaration */
38 extern struct stats_struct sarrays[MAX_DEVICES][MAX_QID];
39 extern struct stats_struct old_sarrays[MAX_DEVICES][MAX_QID];
40 /*----------------------------------------------------------------------------*/
41 /**
42  * dummy function whenever a device is `opened'
43  */
44 static int
netdev_open(struct net_device * netdev)45 netdev_open(struct net_device *netdev)
46 {
47 	(void)netdev;
48 	return 0;
49 }
50 /*----------------------------------------------------------------------------*/
51 /**
52  * dummy function for retrieving net stats
53  */
54 static struct net_device_stats *
netdev_stats(struct net_device * netdev)55 netdev_stats(struct net_device *netdev)
56 {
57 	struct net_adapter *adapter;
58 	struct stats_struct *old_sarray = NULL;
59 	struct stats_struct *sarray = NULL;
60 	int i, ifdx;
61 
62 	adapter = netdev_priv(netdev);
63 	ifdx = adapter->bd_number;
64 
65 	if (ifdx >= MAX_DEVICES)
66 		printk(KERN_ERR "ifindex value: %d is greater than MAX_DEVICES!\n",
67 		       ifdx);
68 
69 	adapter->nstats.rx_packets = adapter->nstats.tx_packets = 0;
70 	adapter->nstats.rx_bytes = adapter->nstats.tx_bytes = 0;
71 
72 	for (i = 0; i < MAX_QID; i++) {
73 		sarray = &sarrays[ifdx][i];
74 		old_sarray = &old_sarrays[ifdx][i];
75 
76 		adapter->nstats.rx_packets += sarray->rx_pkts + old_sarray->rx_pkts;
77 		adapter->nstats.rx_bytes += sarray->rx_bytes + old_sarray->rx_bytes;
78 		adapter->nstats.tx_packets += sarray->tx_pkts + old_sarray->tx_pkts;
79 		adapter->nstats.tx_bytes += sarray->tx_bytes + old_sarray->tx_bytes;
80 		adapter->nstats.rx_missed_errors += sarray->rmiss + old_sarray->rmiss;
81 		adapter->nstats.rx_frame_errors += sarray->rerr + old_sarray->rerr;
82 		adapter->nstats.tx_errors += sarray->terr + old_sarray->terr;
83 	}
84 
85 #if 0
86 	printk(KERN_INFO "ifdx: %d, rxp: %llu, rxb: %llu, txp: %llu, txb: %llu\n",
87 	       ifdx,
88 	       (long long unsigned int)adapter->nstats.rx_packets,
89 	       (long long unsigned int)adapter->nstats.rx_bytes,
90 	       (long long unsigned int)adapter->nstats.tx_packets,
91 	       (long long unsigned int)adapter->nstats.tx_bytes);
92 #endif
93 	return &adapter->nstats;
94 }
95 /*----------------------------------------------------------------------------*/
96 /**
97  * dummy function for setting features
98  */
99 static int
netdev_set_features(struct net_device * netdev,netdev_features_t features)100 netdev_set_features(struct net_device *netdev, netdev_features_t features)
101 {
102 	(void)netdev;
103 	(void)features;
104 	return 0;
105 }
106 /*----------------------------------------------------------------------------*/
107 /**
108  * dummy function for fixing features
109  */
110 static netdev_features_t
netdev_fix_features(struct net_device * netdev,netdev_features_t features)111 netdev_fix_features(struct net_device *netdev, netdev_features_t features)
112 {
113 	(void)netdev;
114 	(void)features;
115 	return 0;
116 }
117 /*----------------------------------------------------------------------------*/
118 /**
119  * dummy function that returns void
120  */
121 static void
netdev_no_ret(struct net_device * netdev)122 netdev_no_ret(struct net_device *netdev)
123 {
124 	(void)netdev;
125 	return;
126 }
127 /*----------------------------------------------------------------------------*/
128 /**
129  * dummy tx function
130  */
131 static int
netdev_xmit(struct sk_buff * skb,struct net_device * netdev)132 netdev_xmit(struct sk_buff *skb, struct net_device *netdev) {
133 	(void)netdev;
134 	(void)skb;
135 	return 0;
136 }
137 /*----------------------------------------------------------------------------*/
138 /**
139  * A naive net_device_ops struct to get the interface visible to the OS
140  */
141 static const struct net_device_ops netdev_ops = {
142         .ndo_open               = netdev_open,
143         .ndo_stop               = netdev_open,
144         .ndo_start_xmit         = netdev_xmit,
145         .ndo_set_rx_mode        = netdev_no_ret,
146         .ndo_validate_addr      = netdev_open,
147         .ndo_set_mac_address    = NULL,
148         .ndo_change_mtu         = NULL,
149         .ndo_tx_timeout         = netdev_no_ret,
150         .ndo_vlan_rx_add_vid    = NULL,
151         .ndo_vlan_rx_kill_vid   = NULL,
152         .ndo_do_ioctl           = NULL,
153         .ndo_set_vf_mac         = NULL,
154         .ndo_set_vf_vlan        = NULL,
155 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 15, 0)
156         .ndo_set_vf_tx_rate     = NULL,
157 #else
158         .ndo_set_vf_rate        = NULL,
159 #endif
160         .ndo_set_vf_spoofchk    = NULL,
161         .ndo_get_vf_config      = NULL,
162         .ndo_get_stats          = netdev_stats,
163         .ndo_setup_tc           = NULL,
164 #ifdef CONFIG_NET_POLL_CONTROLLER
165         .ndo_poll_controller    = netdev_no_ret,
166 	.ndo_netpoll_setup	= NULL,
167 	.ndo_netpoll_cleanup	= NULL,
168 #endif
169         .ndo_set_features 	= netdev_set_features,
170         .ndo_fix_features 	= netdev_fix_features,
171         .ndo_fdb_add            = NULL,
172 };
173 /*----------------------------------------------------------------------------*/
174 /**
175  * assignment function
176  */
177 void
netdev_assign_netdev_ops(struct net_device * dev)178 netdev_assign_netdev_ops(struct net_device *dev)
179 {
180 	dev->netdev_ops = &netdev_ops;
181 }
182 /*----------------------------------------------------------------------------*/
183 #endif /* __DPDK_IFACE_H__ */
184