Home | History | Annotate | Line # | Download | only in ixgbe
if_fdir.c revision 1.1.4.4
      1 /* $NetBSD: if_fdir.c,v 1.1.4.4 2021/09/15 16:38:00 martin 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 <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: if_fdir.c,v 1.1.4.4 2021/09/15 16:38:00 martin Exp $");
     38 
     39 #include "ixgbe.h"
     40 
     41 #ifdef IXGBE_FDIR
     42 
     43 void
     44 ixgbe_init_fdir(struct adapter *adapter)
     45 {
     46 	u32 hdrm = 32 << fdir_pballoc;
     47 
     48 	if (!(adapter->feat_en & IXGBE_FEATURE_FDIR))
     49 		return;
     50 
     51 	adapter->hw.mac.ops.setup_rxpba(&adapter->hw, 0, hdrm,
     52 	    PBA_STRATEGY_EQUAL);
     53 	ixgbe_init_fdir_signature_82599(&adapter->hw, fdir_pballoc);
     54 } /* ixgbe_init_fdir */
     55 
     56 void
     57 ixgbe_reinit_fdir(void *context)
     58 {
     59 	struct adapter *adapter = context;
     60 	struct ifnet   *ifp = adapter->ifp;
     61 
     62 	if (!(adapter->feat_en & IXGBE_FEATURE_FDIR))
     63 		return;
     64 	if (adapter->fdir_reinit != 1) /* Shouldn't happen */
     65 		return;
     66 	ixgbe_reinit_fdir_tables_82599(&adapter->hw);
     67 	adapter->fdir_reinit = 0;
     68 	/* re-enable flow director interrupts */
     69 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
     70 	/* Restart the interface */
     71 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
     72 } /* ixgbe_reinit_fdir */
     73 
     74 /************************************************************************
     75  * ixgbe_atr
     76  *
     77  *   Parse packet headers so that Flow Director can make
     78  *   a hashed filter table entry allowing traffic flows
     79  *   to be identified and kept on the same cpu.  This
     80  *   would be a performance hit, but we only do it at
     81  *   IXGBE_FDIR_RATE of packets.
     82  ************************************************************************/
     83 void
     84 ixgbe_atr(struct tx_ring *txr, struct mbuf *mp)
     85 {
     86 	struct adapter             *adapter = txr->adapter;
     87 	struct ix_queue            *que;
     88 	struct ip                  *ip;
     89 	struct tcphdr              *th;
     90 	struct udphdr              *uh;
     91 	struct ether_vlan_header   *eh;
     92 	union ixgbe_atr_hash_dword input = {.dword = 0};
     93 	union ixgbe_atr_hash_dword common = {.dword = 0};
     94 	int                        ehdrlen, ip_hlen;
     95 	u16                        etype;
     96 
     97 	eh = mtod(mp, struct ether_vlan_header *);
     98 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
     99 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
    100 		etype = eh->evl_proto;
    101 	} else {
    102 		ehdrlen = ETHER_HDR_LEN;
    103 		etype = eh->evl_encap_proto;
    104 	}
    105 
    106 	/* Only handling IPv4 */
    107 	if (etype != htons(ETHERTYPE_IP))
    108 		return;
    109 
    110 	ip = (struct ip *)(mp->m_data + ehdrlen);
    111 	ip_hlen = ip->ip_hl << 2;
    112 
    113 	/* check if we're UDP or TCP */
    114 	switch (ip->ip_p) {
    115 	case IPPROTO_TCP:
    116 		th = (struct tcphdr *)((caddr_t)ip + ip_hlen);
    117 		/* src and dst are inverted */
    118 		common.port.dst ^= th->th_sport;
    119 		common.port.src ^= th->th_dport;
    120 		input.formatted.flow_type ^= IXGBE_ATR_FLOW_TYPE_TCPV4;
    121 		break;
    122 	case IPPROTO_UDP:
    123 		uh = (struct udphdr *)((caddr_t)ip + ip_hlen);
    124 		/* src and dst are inverted */
    125 		common.port.dst ^= uh->uh_sport;
    126 		common.port.src ^= uh->uh_dport;
    127 		input.formatted.flow_type ^= IXGBE_ATR_FLOW_TYPE_UDPV4;
    128 		break;
    129 	default:
    130 		return;
    131 	}
    132 
    133 	input.formatted.vlan_id = htobe16(mp->m_pkthdr.ether_vtag);
    134 	if (mp->m_pkthdr.ether_vtag)
    135 		common.flex_bytes ^= htons(ETHERTYPE_VLAN);
    136 	else
    137 		common.flex_bytes ^= etype;
    138 	common.ip ^= ip->ip_src.s_addr ^ ip->ip_dst.s_addr;
    139 
    140 	que = &adapter->queues[txr->me];
    141 	/*
    142 	 * This assumes the Rx queue and Tx
    143 	 * queue are bound to the same CPU
    144 	 */
    145 	ixgbe_fdir_add_signature_filter_82599(&adapter->hw,
    146 	    input, common, que->msix);
    147 } /* ixgbe_atr */
    148 
    149 #else
    150 
    151 /* TASK_INIT needs this function defined regardless if it's enabled */
    152 void
    153 ixgbe_reinit_fdir(void *context)
    154 {
    155 	UNREFERENCED_1PARAMETER(context);
    156 } /* ixgbe_reinit_fdir */
    157 
    158 void
    159 ixgbe_atr(struct tx_ring *txr, struct mbuf *mp)
    160 {
    161 	UNREFERENCED_2PARAMETER(txr, mp);
    162 } /* ixgbe_atr */
    163 
    164 #endif
    165