xref: /openbmc/linux/net/mctp/test/utils.c (revision fe7498ef)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/netdevice.h>
4 #include <linux/mctp.h>
5 #include <linux/if_arp.h>
6 
7 #include <net/mctpdevice.h>
8 #include <net/pkt_sched.h>
9 
10 #include "utils.h"
11 
12 static netdev_tx_t mctp_test_dev_tx(struct sk_buff *skb,
13 				    struct net_device *ndev)
14 {
15 	kfree(skb);
16 	return NETDEV_TX_OK;
17 }
18 
19 static const struct net_device_ops mctp_test_netdev_ops = {
20 	.ndo_start_xmit = mctp_test_dev_tx,
21 };
22 
23 static void mctp_test_dev_setup(struct net_device *ndev)
24 {
25 	ndev->type = ARPHRD_MCTP;
26 	ndev->mtu = MCTP_DEV_TEST_MTU;
27 	ndev->hard_header_len = 0;
28 	ndev->addr_len = 0;
29 	ndev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
30 	ndev->flags = IFF_NOARP;
31 	ndev->netdev_ops = &mctp_test_netdev_ops;
32 	ndev->needs_free_netdev = true;
33 }
34 
35 struct mctp_test_dev *mctp_test_create_dev(void)
36 {
37 	struct mctp_test_dev *dev;
38 	struct net_device *ndev;
39 	int rc;
40 
41 	ndev = alloc_netdev(sizeof(*dev), "mctptest%d", NET_NAME_ENUM,
42 			    mctp_test_dev_setup);
43 	if (!ndev)
44 		return NULL;
45 
46 	dev = netdev_priv(ndev);
47 	dev->ndev = ndev;
48 
49 	rc = register_netdev(ndev);
50 	if (rc) {
51 		free_netdev(ndev);
52 		return NULL;
53 	}
54 
55 	rcu_read_lock();
56 	dev->mdev = __mctp_dev_get(ndev);
57 	mctp_dev_hold(dev->mdev);
58 	rcu_read_unlock();
59 
60 	return dev;
61 }
62 
63 void mctp_test_destroy_dev(struct mctp_test_dev *dev)
64 {
65 	mctp_dev_put(dev->mdev);
66 	unregister_netdev(dev->ndev);
67 }
68