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