Update primary code license to Apache 2.0.
[sliver-openvswitch.git] / datapath / datapath.h
1 /*
2  * Copyright (c) 2009 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Interface exported by openvswitch_mod. */
10
11 #ifndef DATAPATH_H
12 #define DATAPATH_H 1
13
14 #include <asm/page.h>
15 #include <linux/kernel.h>
16 #include <linux/mutex.h>
17 #include <linux/netlink.h>
18 #include <linux/netdevice.h>
19 #include <linux/workqueue.h>
20 #include <linux/skbuff.h>
21 #include "flow.h"
22 #include "brc_sysfs.h"
23
24 struct sk_buff;
25
26 /* Mask for the priority bits in a vlan header.  If we ever merge upstream
27  * then this should go into include/linux/if_vlan.h. */
28 #define VLAN_PCP_MASK 0xe000
29
30 #define DP_MAX_PORTS 256
31 #define DP_MAX_GROUPS 16
32
33 #define DP_L2_BITS (PAGE_SHIFT - ilog2(sizeof(struct sw_flow*)))
34 #define DP_L2_SIZE (1 << DP_L2_BITS)
35 #define DP_L2_SHIFT 0
36
37 #define DP_L1_BITS (PAGE_SHIFT - ilog2(sizeof(struct sw_flow**)))
38 #define DP_L1_SIZE (1 << DP_L1_BITS)
39 #define DP_L1_SHIFT DP_L2_BITS
40
41 #define DP_MAX_BUCKETS (DP_L1_SIZE * DP_L2_SIZE)
42
43 struct dp_table {
44         unsigned int n_buckets;
45         struct sw_flow ***flows[2];
46         struct rcu_head rcu;
47 };
48
49 #define DP_N_QUEUES 2
50 #define DP_MAX_QUEUE_LEN 100
51
52 struct dp_stats_percpu {
53         u64 n_frags;
54         u64 n_hit;
55         u64 n_missed;
56         u64 n_lost;
57 };
58
59 struct dp_port_group {
60         struct rcu_head rcu;
61         int n_ports;
62         u16 ports[];
63 };
64
65 struct datapath {
66         struct mutex mutex;
67         int dp_idx;
68
69 #ifdef SUPPORT_SYSFS
70         struct kobject ifobj;
71 #endif
72
73         int drop_frags;
74
75         /* Queued data. */
76         struct sk_buff_head queues[DP_N_QUEUES];
77         wait_queue_head_t waitqueue;
78
79         /* Flow table. */
80         unsigned int n_flows;
81         struct dp_table *table;
82
83         /* Port groups. */
84         struct dp_port_group *groups[DP_MAX_GROUPS];
85
86         /* Switch ports. */
87         unsigned int n_ports;
88         struct net_bridge_port *ports[DP_MAX_PORTS];
89         struct list_head port_list; /* All ports, including local_port. */
90
91         /* Stats. */
92         struct dp_stats_percpu *stats_percpu;
93 };
94
95 struct net_bridge_port {
96         u16 port_no;
97         struct datapath *dp;
98         struct net_device *dev;
99 #ifdef SUPPORT_SYSFS
100         struct kobject kobj;
101 #endif
102         struct list_head node;   /* Element in datapath.ports. */
103 };
104
105 extern struct notifier_block dp_device_notifier;
106 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
107 extern int (*dp_add_dp_hook)(struct datapath *dp);
108 extern int (*dp_del_dp_hook)(struct datapath *dp);
109 extern int (*dp_add_if_hook)(struct net_bridge_port *p);
110 extern int (*dp_del_if_hook)(struct net_bridge_port *p);
111
112 /* Flow table. */
113 struct dp_table *dp_table_create(unsigned int n_buckets);
114 void dp_table_destroy(struct dp_table *, int free_flows);
115 struct sw_flow *dp_table_lookup(struct dp_table *, const struct odp_flow_key *);
116 struct sw_flow **dp_table_lookup_for_insert(struct dp_table *, const struct odp_flow_key *);
117 int dp_table_delete(struct dp_table *, struct sw_flow *);
118 int dp_table_expand(struct datapath *);
119 int dp_table_flush(struct datapath *);
120 int dp_table_foreach(struct dp_table *table,
121                      int (*callback)(struct sw_flow *flow, void *aux),
122                      void *aux);
123
124 void dp_process_received_packet(struct sk_buff *, struct net_bridge_port *);
125 int dp_del_port(struct net_bridge_port *, struct list_head *);
126 int dp_output_port(struct datapath *, struct sk_buff *, int out_port,
127                    int ignore_no_fwd);
128 int dp_output_control(struct datapath *, struct sk_buff *, int, u32 arg);
129 void dp_set_origin(struct datapath *, u16, struct sk_buff *);
130
131 struct datapath *get_dp(int dp_idx);
132
133 static inline const char *dp_name(const struct datapath *dp)
134 {
135         return dp->ports[ODPP_LOCAL]->dev->name;
136 }
137
138 #ifdef CONFIG_XEN
139 int skb_checksum_setup(struct sk_buff *skb);
140 #else
141 static inline int skb_checksum_setup(struct sk_buff *skb)
142 {
143         return 0;
144 }
145 #endif
146
147 #endif /* datapath.h */