ee6acbf02ef0fa6f29b624c5855a93b1e9a38621
[cascardo/linux.git] / drivers / net / ethernet / cisco / enic / enic_clsf.c
1 #include <linux/if.h>
2 #include <linux/if_ether.h>
3 #include <linux/if_link.h>
4 #include <linux/netdevice.h>
5 #include <linux/in.h>
6 #include <linux/types.h>
7 #include <linux/skbuff.h>
8 #include <net/flow_keys.h>
9 #include "enic_res.h"
10 #include "enic_clsf.h"
11
12 /* enic_addfltr_5t - Add ipv4 5tuple filter
13  *      @enic: enic struct of vnic
14  *      @keys: flow_keys of ipv4 5tuple
15  *      @rq: rq number to steer to
16  *
17  * This function returns filter_id(hardware_id) of the filter
18  * added. In case of error it returns an negative number.
19  */
20 int enic_addfltr_5t(struct enic *enic, struct flow_keys *keys, u16 rq)
21 {
22         int res;
23         struct filter data;
24
25         switch (keys->ip_proto) {
26         case IPPROTO_TCP:
27                 data.u.ipv4.protocol = PROTO_TCP;
28                 break;
29         case IPPROTO_UDP:
30                 data.u.ipv4.protocol = PROTO_UDP;
31                 break;
32         default:
33                 return -EPROTONOSUPPORT;
34         };
35         data.type = FILTER_IPV4_5TUPLE;
36         data.u.ipv4.src_addr = ntohl(keys->src);
37         data.u.ipv4.dst_addr = ntohl(keys->dst);
38         data.u.ipv4.src_port = ntohs(keys->port16[0]);
39         data.u.ipv4.dst_port = ntohs(keys->port16[1]);
40         data.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
41
42         spin_lock_bh(&enic->devcmd_lock);
43         res = vnic_dev_classifier(enic->vdev, CLSF_ADD, &rq, &data);
44         spin_unlock_bh(&enic->devcmd_lock);
45         res = (res == 0) ? rq : res;
46
47         return res;
48 }
49
50 /* enic_delfltr - Delete clsf filter
51  *      @enic: enic struct of vnic
52  *      @filter_id: filter_is(hardware_id) of filter to be deleted
53  *
54  * This function returns zero in case of success, negative number incase of
55  * error.
56  */
57 int enic_delfltr(struct enic *enic, u16 filter_id)
58 {
59         int ret;
60
61         spin_lock_bh(&enic->devcmd_lock);
62         ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL);
63         spin_unlock_bh(&enic->devcmd_lock);
64
65         return ret;
66 }
67
68 /* enic_rfs_flw_tbl_init - initialize enic->rfs_h members
69  *      @enic: enic data
70  */
71 void enic_rfs_flw_tbl_init(struct enic *enic)
72 {
73         int i;
74
75         spin_lock_init(&enic->rfs_h.lock);
76         for (i = 0; i <= ENIC_RFS_FLW_MASK; i++)
77                 INIT_HLIST_HEAD(&enic->rfs_h.ht_head[i]);
78         enic->rfs_h.max = enic->config.num_arfs;
79         enic->rfs_h.free = enic->rfs_h.max;
80         enic->rfs_h.toclean = 0;
81         enic_rfs_timer_start(enic);
82 }
83
84 void enic_rfs_flw_tbl_free(struct enic *enic)
85 {
86         int i;
87
88         enic_rfs_timer_stop(enic);
89         spin_lock(&enic->rfs_h.lock);
90         enic->rfs_h.free = 0;
91         for (i = 0; i < (1 << ENIC_RFS_FLW_BITSHIFT); i++) {
92                 struct hlist_head *hhead;
93                 struct hlist_node *tmp;
94                 struct enic_rfs_fltr_node *n;
95
96                 hhead = &enic->rfs_h.ht_head[i];
97                 hlist_for_each_entry_safe(n, tmp, hhead, node) {
98                         enic_delfltr(enic, n->fltr_id);
99                         hlist_del(&n->node);
100                         kfree(n);
101                 }
102         }
103         spin_unlock(&enic->rfs_h.lock);
104 }
105
106 #ifdef CONFIG_RFS_ACCEL
107 void enic_flow_may_expire(unsigned long data)
108 {
109         struct enic *enic = (struct enic *)data;
110         bool res;
111         int j;
112
113         spin_lock(&enic->rfs_h.lock);
114         for (j = 0; j < ENIC_CLSF_EXPIRE_COUNT; j++) {
115                 struct hlist_head *hhead;
116                 struct hlist_node *tmp;
117                 struct enic_rfs_fltr_node *n;
118
119                 hhead = &enic->rfs_h.ht_head[enic->rfs_h.toclean++];
120                 hlist_for_each_entry_safe(n, tmp, hhead, node) {
121                         res = rps_may_expire_flow(enic->netdev, n->rq_id,
122                                                   n->flow_id, n->fltr_id);
123                         if (res) {
124                                 res = enic_delfltr(enic, n->fltr_id);
125                                 if (unlikely(res))
126                                         continue;
127                                 hlist_del(&n->node);
128                                 kfree(n);
129                                 enic->rfs_h.free++;
130                         }
131                 }
132         }
133         spin_unlock(&enic->rfs_h.lock);
134         mod_timer(&enic->rfs_h.rfs_may_expire, jiffies + HZ/4);
135 }
136
137 static struct enic_rfs_fltr_node *htbl_key_search(struct hlist_head *h,
138                                                   struct flow_keys *k)
139 {
140         struct enic_rfs_fltr_node *tpos;
141
142         hlist_for_each_entry(tpos, h, node)
143                 if (tpos->keys.src == k->src &&
144                     tpos->keys.dst == k->dst &&
145                     tpos->keys.ports == k->ports &&
146                     tpos->keys.ip_proto == k->ip_proto &&
147                     tpos->keys.n_proto == k->n_proto)
148                         return tpos;
149         return NULL;
150 }
151
152 int enic_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
153                        u16 rxq_index, u32 flow_id)
154 {
155         struct flow_keys keys;
156         struct enic_rfs_fltr_node *n;
157         struct enic *enic;
158         u16 tbl_idx;
159         int res, i;
160
161         enic = netdev_priv(dev);
162         res = skb_flow_dissect(skb, &keys);
163         if (!res || keys.n_proto != htons(ETH_P_IP) ||
164             (keys.ip_proto != IPPROTO_TCP && keys.ip_proto != IPPROTO_UDP))
165                 return -EPROTONOSUPPORT;
166
167         tbl_idx = skb_get_hash_raw(skb) & ENIC_RFS_FLW_MASK;
168         spin_lock(&enic->rfs_h.lock);
169         n = htbl_key_search(&enic->rfs_h.ht_head[tbl_idx], &keys);
170
171         if (n) { /* entry already present  */
172                 if (rxq_index == n->rq_id) {
173                         res = -EEXIST;
174                         goto ret_unlock;
175                 }
176
177                 /* desired rq changed for the flow, we need to delete
178                  * old fltr and add new one
179                  *
180                  * The moment we delete the fltr, the upcoming pkts
181                  * are put it default rq based on rss. When we add
182                  * new filter, upcoming pkts are put in desired queue.
183                  * This could cause ooo pkts.
184                  *
185                  * Lets 1st try adding new fltr and then del old one.
186                  */
187                 i = --enic->rfs_h.free;
188                 /* clsf tbl is full, we have to del old fltr first*/
189                 if (unlikely(i < 0)) {
190                         enic->rfs_h.free++;
191                         res = enic_delfltr(enic, n->fltr_id);
192                         if (unlikely(res < 0))
193                                 goto ret_unlock;
194                         res = enic_addfltr_5t(enic, &keys, rxq_index);
195                         if (res < 0) {
196                                 hlist_del(&n->node);
197                                 enic->rfs_h.free++;
198                                 goto ret_unlock;
199                         }
200                 /* add new fltr 1st then del old fltr */
201                 } else {
202                         int ret;
203
204                         res = enic_addfltr_5t(enic, &keys, rxq_index);
205                         if (res < 0) {
206                                 enic->rfs_h.free++;
207                                 goto ret_unlock;
208                         }
209                         ret = enic_delfltr(enic, n->fltr_id);
210                         /* deleting old fltr failed. Add old fltr to list.
211                          * enic_flow_may_expire() will try to delete it later.
212                          */
213                         if (unlikely(ret < 0)) {
214                                 struct enic_rfs_fltr_node *d;
215                                 struct hlist_head *head;
216
217                                 head = &enic->rfs_h.ht_head[tbl_idx];
218                                 d = kmalloc(sizeof(*d), GFP_ATOMIC);
219                                 if (d) {
220                                         d->fltr_id = n->fltr_id;
221                                         INIT_HLIST_NODE(&d->node);
222                                         hlist_add_head(&d->node, head);
223                                 }
224                         } else {
225                                 enic->rfs_h.free++;
226                         }
227                 }
228                 n->rq_id = rxq_index;
229                 n->fltr_id = res;
230                 n->flow_id = flow_id;
231         /* entry not present */
232         } else {
233                 i = --enic->rfs_h.free;
234                 if (i <= 0) {
235                         enic->rfs_h.free++;
236                         res = -EBUSY;
237                         goto ret_unlock;
238                 }
239
240                 n = kmalloc(sizeof(*n), GFP_ATOMIC);
241                 if (!n) {
242                         res = -ENOMEM;
243                         enic->rfs_h.free++;
244                         goto ret_unlock;
245                 }
246
247                 res = enic_addfltr_5t(enic, &keys, rxq_index);
248                 if (res < 0) {
249                         kfree(n);
250                         enic->rfs_h.free++;
251                         goto ret_unlock;
252                 }
253                 n->rq_id = rxq_index;
254                 n->fltr_id = res;
255                 n->flow_id = flow_id;
256                 n->keys = keys;
257                 INIT_HLIST_NODE(&n->node);
258                 hlist_add_head(&n->node, &enic->rfs_h.ht_head[tbl_idx]);
259         }
260
261 ret_unlock:
262         spin_unlock(&enic->rfs_h.lock);
263         return res;
264 }
265
266 #endif /* CONFIG_RFS_ACCEL */