blob: cd2f692b8074e6b543a511b5f76a775a7ab17f47 [file] [log] [blame]
Jiri Pirko753f9932013-03-06 01:31:13 +00001/*
2 * drivers/net/team/team_mode_random.c - Random mode for team
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/skbuff.h>
Jiri Pirko753f9932013-03-06 01:31:13 +000016#include <linux/if_team.h>
17
Jiri Pirko753f9932013-03-06 01:31:13 +000018static bool rnd_transmit(struct team *team, struct sk_buff *skb)
19{
20 struct team_port *port;
21 int port_index;
22
Daniel Borkmannf337db62014-01-22 02:29:39 +010023 port_index = prandom_u32_max(team->en_port_count);
Jiri Pirko753f9932013-03-06 01:31:13 +000024 port = team_get_port_by_index_rcu(team, port_index);
Jiri Pirko76c455d2013-06-08 15:00:53 +020025 if (unlikely(!port))
26 goto drop;
Jiri Pirko753f9932013-03-06 01:31:13 +000027 port = team_get_first_port_txable_rcu(team, port);
28 if (unlikely(!port))
29 goto drop;
30 if (team_dev_queue_xmit(team, port, skb))
31 return false;
32 return true;
33
34drop:
35 dev_kfree_skb_any(skb);
36 return false;
37}
38
39static const struct team_mode_ops rnd_mode_ops = {
40 .transmit = rnd_transmit,
41 .port_enter = team_modeop_port_enter,
42 .port_change_dev_addr = team_modeop_port_change_dev_addr,
43};
44
45static const struct team_mode rnd_mode = {
46 .kind = "random",
47 .owner = THIS_MODULE,
48 .ops = &rnd_mode_ops,
49};
50
51static int __init rnd_init_module(void)
52{
53 return team_mode_register(&rnd_mode);
54}
55
56static void __exit rnd_cleanup_module(void)
57{
58 team_mode_unregister(&rnd_mode);
59}
60
61module_init(rnd_init_module);
62module_exit(rnd_cleanup_module);
63
64MODULE_LICENSE("GPL v2");
65MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
66MODULE_DESCRIPTION("Random mode for team");
67MODULE_ALIAS("team-mode-random");