Home | History | Annotate | Line # | Download | only in ixgbe
if_fdir.c revision 1.4
      1 /* $NetBSD: if_fdir.c,v 1.4 2021/04/30 06:41:36 msaitoh Exp $ */
      2 /******************************************************************************
      3 
      4   Copyright (c) 2001-2017, Intel Corporation
      5   All rights reserved.
      6 
      7   Redistribution and use in source and binary forms, with or without
      8   modification, are permitted provided that the following conditions are met:
      9 
     10    1. Redistributions of source code must retain the above copyright notice,
     11       this list of conditions and the following disclaimer.
     12 
     13    2. Redistributions in binary form must reproduce the above copyright
     14       notice, this list of conditions and the following disclaimer in the
     15       documentation and/or other materials provided with the distribution.
     16 
     17    3. Neither the name of the Intel Corporation nor the names of its
     18       contributors may be used to endorse or promote products derived from
     19       this software without specific prior written permission.
     20 
     21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     22   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     25   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   POSSIBILITY OF SUCH DAMAGE.
     32 
     33 ******************************************************************************/
     34 /*$FreeBSD: head/sys/dev/ixgbe/if_fdir.c 327031 2017-12-20 18:15:06Z erj $*/
     35 
     36 #include "ixgbe.h"
     37 
     38 #ifdef IXGBE_FDIR
     39 
     40 void
     41 ixgbe_init_fdir(struct adapter *adapter)
     42 {
     43 	u32 hdrm = 32 << fdir_pballoc;
     44 
     45 	if (!(adapter->feat_en & IXGBE_FEATURE_FDIR))
     46 		return;
     47 
     48 	adapter->hw.mac.ops.setup_rxpba(&adapter->hw, 0, hdrm,
     49 	    PBA_STRATEGY_EQUAL);
     50 	ixgbe_init_fdir_signature_82599(&adapter->hw, fdir_pballoc);
     51 } /* ixgbe_init_fdir */
     52 
     53 void
     54 ixgbe_reinit_fdir(void *context)
     55 {
     56 	struct adapter *adapter = context;
     57 	struct ifnet   *ifp = adapter->ifp;
     58 
     59 	KASSERT(mutex_owned(&adapter->core_mtx));
     60 
     61 	if (!(adapter->feat_en & IXGBE_FEATURE_FDIR))
     62 		return;
     63 	if (adapter->fdir_reinit != 1) /* Shouldn't happen */
     64 		return;
     65 	ixgbe_reinit_fdir_tables_82599(&adapter->hw);
     66 	adapter->fdir_reinit = 0;
     67 	/* re-enable flow director interrupts */
     68 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
     69 	/* Restart the interface */
     70 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
     71 } /* ixgbe_reinit_fdir */
     72 
     73 /************************************************************************
     74  * ixgbe_atr
     75  *
     76  *   Parse packet headers so that Flow Director can make
     77  *   a hashed filter table entry allowing traffic flows
     78  *   to be identified and kept on the same cpu.  This
     79  *   would be a performance hit, but we only do it at
     80  *   IXGBE_FDIR_RATE of packets.
     81  ************************************************************************/
     82 void
     83 ixgbe_atr(struct tx_ring *txr, struct mbuf *mp)
     84 {
     85 	struct adapter             *adapter = txr->adapter;
     86 	struct ix_queue            *que;
     87 	struct ip                  *ip;
     88 	struct tcphdr              *th;
     89 	struct udphdr              *uh;
     90 	struct ether_vlan_header   *eh;
     91 	union ixgbe_atr_hash_dword input = {.dword = 0};
     92 	union ixgbe_atr_hash_dword common = {.dword = 0};
     93 	int                        ehdrlen, ip_hlen;
     94 	u16                        etype;
     95 
     96 	eh = mtod(mp, struct ether_vlan_header *);
     97 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
     98 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
     99 		etype = eh->evl_proto;
    100 	} else {
    101 		ehdrlen = ETHER_HDR_LEN;
    102 		etype = eh->evl_encap_proto;
    103 	}
    104 
    105 	/* Only handling IPv4 */
    106 	if (etype != htons(ETHERTYPE_IP))
    107 		return;
    108 
    109 	ip = (struct ip *)(mp->m_data + ehdrlen);
    110 	ip_hlen = ip->ip_hl << 2;
    111 
    112 	/* check if we're UDP or TCP */
    113 	switch (ip->ip_p) {
    114 	case IPPROTO_TCP:
    115 		th = (struct tcphdr *)((caddr_t)ip + ip_hlen);
    116 		/* src and dst are inverted */
    117 		common.port.dst ^= th->th_sport;
    118 		common.port.src ^= th->th_dport;
    119 		input.formatted.flow_type ^= IXGBE_ATR_FLOW_TYPE_TCPV4;
    120 		break;
    121 	case IPPROTO_UDP:
    122 		uh = (struct udphdr *)((caddr_t)ip + ip_hlen);
    123 		/* src and dst are inverted */
    124 		common.port.dst ^= uh->uh_sport;
    125 		common.port.src ^= uh->uh_dport;
    126 		input.formatted.flow_type ^= IXGBE_ATR_FLOW_TYPE_UDPV4;
    127 		break;
    128 	default:
    129 		return;
    130 	}
    131 
    132 	input.formatted.vlan_id = htobe16(mp->m_pkthdr.ether_vtag);
    133 	if (mp->m_pkthdr.ether_vtag)
    134 		common.flex_bytes ^= htons(ETHERTYPE_VLAN);
    135 	else
    136 		common.flex_bytes ^= etype;
    137 	common.ip ^= ip->ip_src.s_addr ^ ip->ip_dst.s_addr;
    138 
    139 	que = &adapter->queues[txr->me];
    140 	/*
    141 	 * This assumes the Rx queue and Tx
    142 	 * queue are bound to the same CPU
    143 	 */
    144 	ixgbe_fdir_add_signature_filter_82599(&adapter->hw,
    145 	    input, common, que->msix);
    146 } /* ixgbe_atr */
    147 
    148 #else
    149 
    150 /* TASK_INIT needs this function defined regardless if it's enabled */
    151 void
    152 ixgbe_reinit_fdir(void *context)
    153 {
    154 	UNREFERENCED_1PARAMETER(context);
    155 } /* ixgbe_reinit_fdir */
    156 
    157 void
    158 ixgbe_atr(struct tx_ring *txr, struct mbuf *mp)
    159 {
    160 	UNREFERENCED_2PARAMETER(txr, mp);
    161 } /* ixgbe_atr */
    162 
    163 #endif
    164