Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/if.h>
0003 #include <linux/if_ether.h>
0004 #include <linux/if_link.h>
0005 #include <linux/netdevice.h>
0006 #include <linux/in.h>
0007 #include <linux/types.h>
0008 #include <linux/skbuff.h>
0009 #include <net/flow_dissector.h>
0010 #include "enic_res.h"
0011 #include "enic_clsf.h"
0012 
0013 /* enic_addfltr_5t - Add ipv4 5tuple filter
0014  *  @enic: enic struct of vnic
0015  *  @keys: flow_keys of ipv4 5tuple
0016  *  @rq: rq number to steer to
0017  *
0018  * This function returns filter_id(hardware_id) of the filter
0019  * added. In case of error it returns a negative number.
0020  */
0021 int enic_addfltr_5t(struct enic *enic, struct flow_keys *keys, u16 rq)
0022 {
0023     int res;
0024     struct filter data;
0025 
0026     switch (keys->basic.ip_proto) {
0027     case IPPROTO_TCP:
0028         data.u.ipv4.protocol = PROTO_TCP;
0029         break;
0030     case IPPROTO_UDP:
0031         data.u.ipv4.protocol = PROTO_UDP;
0032         break;
0033     default:
0034         return -EPROTONOSUPPORT;
0035     }
0036 
0037     data.type = FILTER_IPV4_5TUPLE;
0038     data.u.ipv4.src_addr = ntohl(keys->addrs.v4addrs.src);
0039     data.u.ipv4.dst_addr = ntohl(keys->addrs.v4addrs.dst);
0040     data.u.ipv4.src_port = ntohs(keys->ports.src);
0041     data.u.ipv4.dst_port = ntohs(keys->ports.dst);
0042     data.u.ipv4.flags = FILTER_FIELDS_IPV4_5TUPLE;
0043 
0044     spin_lock_bh(&enic->devcmd_lock);
0045     res = vnic_dev_classifier(enic->vdev, CLSF_ADD, &rq, &data);
0046     spin_unlock_bh(&enic->devcmd_lock);
0047     res = (res == 0) ? rq : res;
0048 
0049     return res;
0050 }
0051 
0052 /* enic_delfltr - Delete clsf filter
0053  *  @enic: enic struct of vnic
0054  *  @filter_id: filter_is(hardware_id) of filter to be deleted
0055  *
0056  * This function returns zero in case of success, negative number incase of
0057  * error.
0058  */
0059 int enic_delfltr(struct enic *enic, u16 filter_id)
0060 {
0061     int ret;
0062 
0063     spin_lock_bh(&enic->devcmd_lock);
0064     ret = vnic_dev_classifier(enic->vdev, CLSF_DEL, &filter_id, NULL);
0065     spin_unlock_bh(&enic->devcmd_lock);
0066 
0067     return ret;
0068 }
0069 
0070 /* enic_rfs_flw_tbl_init - initialize enic->rfs_h members
0071  *  @enic: enic data
0072  */
0073 void enic_rfs_flw_tbl_init(struct enic *enic)
0074 {
0075     int i;
0076 
0077     spin_lock_init(&enic->rfs_h.lock);
0078     for (i = 0; i <= ENIC_RFS_FLW_MASK; i++)
0079         INIT_HLIST_HEAD(&enic->rfs_h.ht_head[i]);
0080     enic->rfs_h.max = enic->config.num_arfs;
0081     enic->rfs_h.free = enic->rfs_h.max;
0082     enic->rfs_h.toclean = 0;
0083 }
0084 
0085 void enic_rfs_flw_tbl_free(struct enic *enic)
0086 {
0087     int i;
0088 
0089     enic_rfs_timer_stop(enic);
0090     spin_lock_bh(&enic->rfs_h.lock);
0091     for (i = 0; i < (1 << ENIC_RFS_FLW_BITSHIFT); i++) {
0092         struct hlist_head *hhead;
0093         struct hlist_node *tmp;
0094         struct enic_rfs_fltr_node *n;
0095 
0096         hhead = &enic->rfs_h.ht_head[i];
0097         hlist_for_each_entry_safe(n, tmp, hhead, node) {
0098             enic_delfltr(enic, n->fltr_id);
0099             hlist_del(&n->node);
0100             kfree(n);
0101             enic->rfs_h.free++;
0102         }
0103     }
0104     spin_unlock_bh(&enic->rfs_h.lock);
0105 }
0106 
0107 struct enic_rfs_fltr_node *htbl_fltr_search(struct enic *enic, u16 fltr_id)
0108 {
0109     int i;
0110 
0111     for (i = 0; i < (1 << ENIC_RFS_FLW_BITSHIFT); i++) {
0112         struct hlist_head *hhead;
0113         struct hlist_node *tmp;
0114         struct enic_rfs_fltr_node *n;
0115 
0116         hhead = &enic->rfs_h.ht_head[i];
0117         hlist_for_each_entry_safe(n, tmp, hhead, node)
0118             if (n->fltr_id == fltr_id)
0119                 return n;
0120     }
0121 
0122     return NULL;
0123 }
0124 
0125 #ifdef CONFIG_RFS_ACCEL
0126 void enic_flow_may_expire(struct timer_list *t)
0127 {
0128     struct enic *enic = from_timer(enic, t, rfs_h.rfs_may_expire);
0129     bool res;
0130     int j;
0131 
0132     spin_lock_bh(&enic->rfs_h.lock);
0133     for (j = 0; j < ENIC_CLSF_EXPIRE_COUNT; j++) {
0134         struct hlist_head *hhead;
0135         struct hlist_node *tmp;
0136         struct enic_rfs_fltr_node *n;
0137 
0138         hhead = &enic->rfs_h.ht_head[enic->rfs_h.toclean++];
0139         hlist_for_each_entry_safe(n, tmp, hhead, node) {
0140             res = rps_may_expire_flow(enic->netdev, n->rq_id,
0141                           n->flow_id, n->fltr_id);
0142             if (res) {
0143                 res = enic_delfltr(enic, n->fltr_id);
0144                 if (unlikely(res))
0145                     continue;
0146                 hlist_del(&n->node);
0147                 kfree(n);
0148                 enic->rfs_h.free++;
0149             }
0150         }
0151     }
0152     spin_unlock_bh(&enic->rfs_h.lock);
0153     mod_timer(&enic->rfs_h.rfs_may_expire, jiffies + HZ/4);
0154 }
0155 
0156 static struct enic_rfs_fltr_node *htbl_key_search(struct hlist_head *h,
0157                           struct flow_keys *k)
0158 {
0159     struct enic_rfs_fltr_node *tpos;
0160 
0161     hlist_for_each_entry(tpos, h, node)
0162         if (tpos->keys.addrs.v4addrs.src == k->addrs.v4addrs.src &&
0163             tpos->keys.addrs.v4addrs.dst == k->addrs.v4addrs.dst &&
0164             tpos->keys.ports.ports == k->ports.ports &&
0165             tpos->keys.basic.ip_proto == k->basic.ip_proto &&
0166             tpos->keys.basic.n_proto == k->basic.n_proto)
0167             return tpos;
0168     return NULL;
0169 }
0170 
0171 int enic_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
0172                u16 rxq_index, u32 flow_id)
0173 {
0174     struct flow_keys keys;
0175     struct enic_rfs_fltr_node *n;
0176     struct enic *enic;
0177     u16 tbl_idx;
0178     int res, i;
0179 
0180     enic = netdev_priv(dev);
0181     res = skb_flow_dissect_flow_keys(skb, &keys, 0);
0182     if (!res || keys.basic.n_proto != htons(ETH_P_IP) ||
0183         (keys.basic.ip_proto != IPPROTO_TCP &&
0184          keys.basic.ip_proto != IPPROTO_UDP))
0185         return -EPROTONOSUPPORT;
0186 
0187     tbl_idx = skb_get_hash_raw(skb) & ENIC_RFS_FLW_MASK;
0188     spin_lock_bh(&enic->rfs_h.lock);
0189     n = htbl_key_search(&enic->rfs_h.ht_head[tbl_idx], &keys);
0190 
0191     if (n) { /* entry already present  */
0192         if (rxq_index == n->rq_id) {
0193             res = -EEXIST;
0194             goto ret_unlock;
0195         }
0196 
0197         /* desired rq changed for the flow, we need to delete
0198          * old fltr and add new one
0199          *
0200          * The moment we delete the fltr, the upcoming pkts
0201          * are put it default rq based on rss. When we add
0202          * new filter, upcoming pkts are put in desired queue.
0203          * This could cause ooo pkts.
0204          *
0205          * Lets 1st try adding new fltr and then del old one.
0206          */
0207         i = --enic->rfs_h.free;
0208         /* clsf tbl is full, we have to del old fltr first*/
0209         if (unlikely(i < 0)) {
0210             enic->rfs_h.free++;
0211             res = enic_delfltr(enic, n->fltr_id);
0212             if (unlikely(res < 0))
0213                 goto ret_unlock;
0214             res = enic_addfltr_5t(enic, &keys, rxq_index);
0215             if (res < 0) {
0216                 hlist_del(&n->node);
0217                 enic->rfs_h.free++;
0218                 goto ret_unlock;
0219             }
0220         /* add new fltr 1st then del old fltr */
0221         } else {
0222             int ret;
0223 
0224             res = enic_addfltr_5t(enic, &keys, rxq_index);
0225             if (res < 0) {
0226                 enic->rfs_h.free++;
0227                 goto ret_unlock;
0228             }
0229             ret = enic_delfltr(enic, n->fltr_id);
0230             /* deleting old fltr failed. Add old fltr to list.
0231              * enic_flow_may_expire() will try to delete it later.
0232              */
0233             if (unlikely(ret < 0)) {
0234                 struct enic_rfs_fltr_node *d;
0235                 struct hlist_head *head;
0236 
0237                 head = &enic->rfs_h.ht_head[tbl_idx];
0238                 d = kmalloc(sizeof(*d), GFP_ATOMIC);
0239                 if (d) {
0240                     d->fltr_id = n->fltr_id;
0241                     INIT_HLIST_NODE(&d->node);
0242                     hlist_add_head(&d->node, head);
0243                 }
0244             } else {
0245                 enic->rfs_h.free++;
0246             }
0247         }
0248         n->rq_id = rxq_index;
0249         n->fltr_id = res;
0250         n->flow_id = flow_id;
0251     /* entry not present */
0252     } else {
0253         i = --enic->rfs_h.free;
0254         if (i <= 0) {
0255             enic->rfs_h.free++;
0256             res = -EBUSY;
0257             goto ret_unlock;
0258         }
0259 
0260         n = kmalloc(sizeof(*n), GFP_ATOMIC);
0261         if (!n) {
0262             res = -ENOMEM;
0263             enic->rfs_h.free++;
0264             goto ret_unlock;
0265         }
0266 
0267         res = enic_addfltr_5t(enic, &keys, rxq_index);
0268         if (res < 0) {
0269             kfree(n);
0270             enic->rfs_h.free++;
0271             goto ret_unlock;
0272         }
0273         n->rq_id = rxq_index;
0274         n->fltr_id = res;
0275         n->flow_id = flow_id;
0276         n->keys = keys;
0277         INIT_HLIST_NODE(&n->node);
0278         hlist_add_head(&n->node, &enic->rfs_h.ht_head[tbl_idx]);
0279     }
0280 
0281 ret_unlock:
0282     spin_unlock_bh(&enic->rfs_h.lock);
0283     return res;
0284 }
0285 
0286 #endif /* CONFIG_RFS_ACCEL */