xref: /freebsd/sys/net/if_infiniband.c (revision a6b55ee6be15a41792839095d19b589e25d0f7f7)
19d40cf60SHans Petter Selasky /*-
29d40cf60SHans Petter Selasky  * Copyright (c) 2020 Mellanox Technologies. All rights reserved.
39d40cf60SHans Petter Selasky  *
49d40cf60SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
59d40cf60SHans Petter Selasky  * modification, are permitted provided that the following conditions
69d40cf60SHans Petter Selasky  * are met:
79d40cf60SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
89d40cf60SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
99d40cf60SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
109d40cf60SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
119d40cf60SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
129d40cf60SHans Petter Selasky  *
139d40cf60SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
149d40cf60SHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
159d40cf60SHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
169d40cf60SHans Petter Selasky  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
179d40cf60SHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
189d40cf60SHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199d40cf60SHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
209d40cf60SHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
219d40cf60SHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
229d40cf60SHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
239d40cf60SHans Petter Selasky  * SUCH DAMAGE.
249d40cf60SHans Petter Selasky  */
259d40cf60SHans Petter Selasky 
269d40cf60SHans Petter Selasky #include "opt_inet.h"
279d40cf60SHans Petter Selasky #include "opt_inet6.h"
28*a6b55ee6SGleb Smirnoff #include "opt_kbd.h"
299d40cf60SHans Petter Selasky 
309d40cf60SHans Petter Selasky #include <sys/cdefs.h>
319d40cf60SHans Petter Selasky __FBSDID("$FreeBSD$");
329d40cf60SHans Petter Selasky 
33ce329aa2SHans Petter Selasky #include <sys/param.h>
341d3a22e7SHans Petter Selasky #include <sys/systm.h>
3501630a49SHans Petter Selasky #include <sys/devctl.h>
369d40cf60SHans Petter Selasky #include <sys/eventhandler.h>
3701630a49SHans Petter Selasky #include <sys/kernel.h>
3801630a49SHans Petter Selasky #include <sys/mbuf.h>
3901630a49SHans Petter Selasky #include <sys/module.h>
409d40cf60SHans Petter Selasky #include <sys/socket.h>
419d40cf60SHans Petter Selasky #include <sys/sysctl.h>
42*a6b55ee6SGleb Smirnoff #ifdef KDB
43*a6b55ee6SGleb Smirnoff #include <sys/kdb.h>
44*a6b55ee6SGleb Smirnoff #endif
459d40cf60SHans Petter Selasky 
4601630a49SHans Petter Selasky #include <net/bpf.h>
479d40cf60SHans Petter Selasky #include <net/ethernet.h>
489d40cf60SHans Petter Selasky #include <net/infiniband.h>
4901630a49SHans Petter Selasky #include <net/if.h>
5001630a49SHans Petter Selasky #include <net/if_var.h>
512c2b37adSJustin Hibbits #include <net/if_private.h>
529d40cf60SHans Petter Selasky #include <net/if_dl.h>
539d40cf60SHans Petter Selasky #include <net/if_media.h>
54a92c4bb6SHans Petter Selasky #include <net/if_lagg.h>
5501630a49SHans Petter Selasky #include <net/if_llatbl.h>
5601630a49SHans Petter Selasky #include <net/if_types.h>
5701630a49SHans Petter Selasky #include <net/netisr.h>
5801630a49SHans Petter Selasky #include <net/route.h>
599d40cf60SHans Petter Selasky #include <netinet/if_ether.h>
6001630a49SHans Petter Selasky #include <netinet/in.h>
619d40cf60SHans Petter Selasky #include <netinet/ip6.h>
629d40cf60SHans Petter Selasky #include <netinet6/in6_var.h>
639d40cf60SHans Petter Selasky #include <netinet6/nd6.h>
649d40cf60SHans Petter Selasky 
659d40cf60SHans Petter Selasky #include <security/mac/mac_framework.h>
669d40cf60SHans Petter Selasky 
67a92c4bb6SHans Petter Selasky /* if_lagg(4) support */
68a92c4bb6SHans Petter Selasky struct mbuf *(*lagg_input_infiniband_p)(struct ifnet *, struct mbuf *);
69a92c4bb6SHans Petter Selasky 
709d40cf60SHans Petter Selasky #ifdef INET
719d40cf60SHans Petter Selasky static inline void
721355e2dcSHans Petter Selasky infiniband_ipv4_multicast_map(uint32_t addr,
731355e2dcSHans Petter Selasky     const uint8_t *broadcast, uint8_t *buf)
749d40cf60SHans Petter Selasky {
759d40cf60SHans Petter Selasky 	uint8_t scope;
769d40cf60SHans Petter Selasky 
779d40cf60SHans Petter Selasky 	addr = ntohl(addr);
789d40cf60SHans Petter Selasky 	scope = broadcast[5] & 0xF;
799d40cf60SHans Petter Selasky 
809d40cf60SHans Petter Selasky 	buf[0] = 0;
819d40cf60SHans Petter Selasky 	buf[1] = 0xff;
829d40cf60SHans Petter Selasky 	buf[2] = 0xff;
839d40cf60SHans Petter Selasky 	buf[3] = 0xff;
849d40cf60SHans Petter Selasky 	buf[4] = 0xff;
859d40cf60SHans Petter Selasky 	buf[5] = 0x10 | scope;
869d40cf60SHans Petter Selasky 	buf[6] = 0x40;
879d40cf60SHans Petter Selasky 	buf[7] = 0x1b;
889d40cf60SHans Petter Selasky 	buf[8] = broadcast[8];
899d40cf60SHans Petter Selasky 	buf[9] = broadcast[9];
909d40cf60SHans Petter Selasky 	buf[10] = 0;
919d40cf60SHans Petter Selasky 	buf[11] = 0;
929d40cf60SHans Petter Selasky 	buf[12] = 0;
939d40cf60SHans Petter Selasky 	buf[13] = 0;
949d40cf60SHans Petter Selasky 	buf[14] = 0;
959d40cf60SHans Petter Selasky 	buf[15] = 0;
969d40cf60SHans Petter Selasky 	buf[16] = (addr >> 24) & 0xff;
979d40cf60SHans Petter Selasky 	buf[17] = (addr >> 16) & 0xff;
989d40cf60SHans Petter Selasky 	buf[18] = (addr >> 8) & 0xff;
999d40cf60SHans Petter Selasky 	buf[19] = addr & 0xff;
1009d40cf60SHans Petter Selasky }
1019d40cf60SHans Petter Selasky #endif
1029d40cf60SHans Petter Selasky 
1039d40cf60SHans Petter Selasky #ifdef INET6
1049d40cf60SHans Petter Selasky static inline void
1051355e2dcSHans Petter Selasky infiniband_ipv6_multicast_map(const struct in6_addr *addr,
1061355e2dcSHans Petter Selasky     const uint8_t *broadcast, uint8_t *buf)
1079d40cf60SHans Petter Selasky {
1089d40cf60SHans Petter Selasky 	uint8_t scope;
1099d40cf60SHans Petter Selasky 
1109d40cf60SHans Petter Selasky 	scope = broadcast[5] & 0xF;
1119d40cf60SHans Petter Selasky 
1129d40cf60SHans Petter Selasky 	buf[0] = 0;
1139d40cf60SHans Petter Selasky 	buf[1] = 0xff;
1149d40cf60SHans Petter Selasky 	buf[2] = 0xff;
1159d40cf60SHans Petter Selasky 	buf[3] = 0xff;
1169d40cf60SHans Petter Selasky 	buf[4] = 0xff;
1179d40cf60SHans Petter Selasky 	buf[5] = 0x10 | scope;
1189d40cf60SHans Petter Selasky 	buf[6] = 0x60;
1199d40cf60SHans Petter Selasky 	buf[7] = 0x1b;
1209d40cf60SHans Petter Selasky 	buf[8] = broadcast[8];
1219d40cf60SHans Petter Selasky 	buf[9] = broadcast[9];
1229d40cf60SHans Petter Selasky 	memcpy(&buf[10], &addr->s6_addr[6], 10);
1239d40cf60SHans Petter Selasky }
1249d40cf60SHans Petter Selasky #endif
1259d40cf60SHans Petter Selasky 
1269d40cf60SHans Petter Selasky /*
1279d40cf60SHans Petter Selasky  * This is for clients that have an infiniband_header in the mbuf.
1289d40cf60SHans Petter Selasky  */
1299d40cf60SHans Petter Selasky void
1309d40cf60SHans Petter Selasky infiniband_bpf_mtap(struct ifnet *ifp, struct mbuf *mb)
1319d40cf60SHans Petter Selasky {
1329d40cf60SHans Petter Selasky 	struct infiniband_header *ibh;
1339d40cf60SHans Petter Selasky 	struct ether_header eh;
1349d40cf60SHans Petter Selasky 
135adf62e83SJustin Hibbits 	if (!bpf_peers_present(ifp->if_bpf))
136adf62e83SJustin Hibbits 		return;
137adf62e83SJustin Hibbits 
13879aa96f9SJustin Hibbits 	M_ASSERTVALID(mb);
1399d40cf60SHans Petter Selasky 	if (mb->m_len < sizeof(*ibh))
1409d40cf60SHans Petter Selasky 		return;
1419d40cf60SHans Petter Selasky 
1429d40cf60SHans Petter Selasky 	ibh = mtod(mb, struct infiniband_header *);
1439d40cf60SHans Petter Selasky 	eh.ether_type = ibh->ib_protocol;
1449d40cf60SHans Petter Selasky 	memset(eh.ether_shost, 0, ETHER_ADDR_LEN);
1459d40cf60SHans Petter Selasky 	memcpy(eh.ether_dhost, ibh->ib_hwaddr + 4, ETHER_ADDR_LEN);
1469d40cf60SHans Petter Selasky 	mb->m_data += sizeof(*ibh);
1479d40cf60SHans Petter Selasky 	mb->m_len -= sizeof(*ibh);
1489d40cf60SHans Petter Selasky 	mb->m_pkthdr.len -= sizeof(*ibh);
1499d40cf60SHans Petter Selasky 	bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
1509d40cf60SHans Petter Selasky 	mb->m_data -= sizeof(*ibh);
1519d40cf60SHans Petter Selasky 	mb->m_len += sizeof(*ibh);
1529d40cf60SHans Petter Selasky 	mb->m_pkthdr.len += sizeof(*ibh);
1539d40cf60SHans Petter Selasky }
1549d40cf60SHans Petter Selasky 
155ec52ff6dSHans Petter Selasky static void
156ec52ff6dSHans Petter Selasky update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
157ec52ff6dSHans Petter Selasky {
158ec52ff6dSHans Petter Selasky 	int csum_flags = 0;
159ec52ff6dSHans Petter Selasky 
160ec52ff6dSHans Petter Selasky 	if (src->m_pkthdr.csum_flags & CSUM_IP)
161ec52ff6dSHans Petter Selasky 		csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
162ec52ff6dSHans Petter Selasky 	if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
163ec52ff6dSHans Petter Selasky 		csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
164ec52ff6dSHans Petter Selasky 	if (src->m_pkthdr.csum_flags & CSUM_SCTP)
165ec52ff6dSHans Petter Selasky 		csum_flags |= CSUM_SCTP_VALID;
166ec52ff6dSHans Petter Selasky 	dst->m_pkthdr.csum_flags |= csum_flags;
167ec52ff6dSHans Petter Selasky 	if (csum_flags & CSUM_DATA_VALID)
168ec52ff6dSHans Petter Selasky 		dst->m_pkthdr.csum_data = 0xffff;
169ec52ff6dSHans Petter Selasky }
170ec52ff6dSHans Petter Selasky 
171ec52ff6dSHans Petter Selasky /*
172ec52ff6dSHans Petter Selasky  * Handle link-layer encapsulation requests.
173ec52ff6dSHans Petter Selasky  */
174ec52ff6dSHans Petter Selasky static int
175ec52ff6dSHans Petter Selasky infiniband_requestencap(struct ifnet *ifp, struct if_encap_req *req)
176ec52ff6dSHans Petter Selasky {
177ec52ff6dSHans Petter Selasky 	struct infiniband_header *ih;
178ec52ff6dSHans Petter Selasky 	struct arphdr *ah;
179ec52ff6dSHans Petter Selasky 	uint16_t etype;
180ec52ff6dSHans Petter Selasky 	const uint8_t *lladdr;
181ec52ff6dSHans Petter Selasky 
182ec52ff6dSHans Petter Selasky 	if (req->rtype != IFENCAP_LL)
183ec52ff6dSHans Petter Selasky 		return (EOPNOTSUPP);
184ec52ff6dSHans Petter Selasky 
185ec52ff6dSHans Petter Selasky 	if (req->bufsize < INFINIBAND_HDR_LEN)
186ec52ff6dSHans Petter Selasky 		return (ENOMEM);
187ec52ff6dSHans Petter Selasky 
188ec52ff6dSHans Petter Selasky 	ih = (struct infiniband_header *)req->buf;
189ec52ff6dSHans Petter Selasky 	lladdr = req->lladdr;
190ec52ff6dSHans Petter Selasky 	req->lladdr_off = 0;
191ec52ff6dSHans Petter Selasky 
192ec52ff6dSHans Petter Selasky 	switch (req->family) {
193ec52ff6dSHans Petter Selasky 	case AF_INET:
194ec52ff6dSHans Petter Selasky 		etype = htons(ETHERTYPE_IP);
195ec52ff6dSHans Petter Selasky 		break;
196ec52ff6dSHans Petter Selasky 	case AF_INET6:
197ec52ff6dSHans Petter Selasky 		etype = htons(ETHERTYPE_IPV6);
198ec52ff6dSHans Petter Selasky 		break;
199ec52ff6dSHans Petter Selasky 	case AF_ARP:
200ec52ff6dSHans Petter Selasky 		ah = (struct arphdr *)req->hdata;
201ec52ff6dSHans Petter Selasky 		ah->ar_hrd = htons(ARPHRD_INFINIBAND);
202ec52ff6dSHans Petter Selasky 
203ec52ff6dSHans Petter Selasky 		switch (ntohs(ah->ar_op)) {
204ec52ff6dSHans Petter Selasky 		case ARPOP_REVREQUEST:
205ec52ff6dSHans Petter Selasky 		case ARPOP_REVREPLY:
206ec52ff6dSHans Petter Selasky 			etype = htons(ETHERTYPE_REVARP);
207ec52ff6dSHans Petter Selasky 			break;
208ec52ff6dSHans Petter Selasky 		case ARPOP_REQUEST:
209ec52ff6dSHans Petter Selasky 		case ARPOP_REPLY:
210ec52ff6dSHans Petter Selasky 		default:
211ec52ff6dSHans Petter Selasky 			etype = htons(ETHERTYPE_ARP);
212ec52ff6dSHans Petter Selasky 			break;
213ec52ff6dSHans Petter Selasky 		}
214ec52ff6dSHans Petter Selasky 
215ec52ff6dSHans Petter Selasky 		if (req->flags & IFENCAP_FLAG_BROADCAST)
216ec52ff6dSHans Petter Selasky 			lladdr = ifp->if_broadcastaddr;
217ec52ff6dSHans Petter Selasky 		break;
218ec52ff6dSHans Petter Selasky 	default:
219ec52ff6dSHans Petter Selasky 		return (EAFNOSUPPORT);
220ec52ff6dSHans Petter Selasky 	}
221ec52ff6dSHans Petter Selasky 
222ec52ff6dSHans Petter Selasky 	ih->ib_protocol = etype;
223ec52ff6dSHans Petter Selasky 	ih->ib_reserved = 0;
224ec52ff6dSHans Petter Selasky 	memcpy(ih->ib_hwaddr, lladdr, INFINIBAND_ADDR_LEN);
225ec52ff6dSHans Petter Selasky 	req->bufsize = sizeof(struct infiniband_header);
226ec52ff6dSHans Petter Selasky 
227ec52ff6dSHans Petter Selasky 	return (0);
228ec52ff6dSHans Petter Selasky }
229ec52ff6dSHans Petter Selasky 
230ec52ff6dSHans Petter Selasky static int
231ec52ff6dSHans Petter Selasky infiniband_resolve_addr(struct ifnet *ifp, struct mbuf *m,
232ec52ff6dSHans Petter Selasky     const struct sockaddr *dst, struct route *ro, uint8_t *phdr,
233ec52ff6dSHans Petter Selasky     uint32_t *pflags, struct llentry **plle)
234ec52ff6dSHans Petter Selasky {
235ac3e46faSJohn Baldwin #if defined(INET) || defined(INET6)
236ac3e46faSJohn Baldwin 	struct infiniband_header *ih = (struct infiniband_header *)phdr;
237ac3e46faSJohn Baldwin #endif
238ec52ff6dSHans Petter Selasky 	uint32_t lleflags = 0;
239ec52ff6dSHans Petter Selasky 	int error = 0;
240ec52ff6dSHans Petter Selasky 
241ec52ff6dSHans Petter Selasky 	if (plle)
242ec52ff6dSHans Petter Selasky 		*plle = NULL;
243ec52ff6dSHans Petter Selasky 
244ec52ff6dSHans Petter Selasky 	switch (dst->sa_family) {
245ec52ff6dSHans Petter Selasky #ifdef INET
246ec52ff6dSHans Petter Selasky 	case AF_INET:
247ec52ff6dSHans Petter Selasky 		if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) {
248ec52ff6dSHans Petter Selasky 			error = arpresolve(ifp, 0, m, dst, phdr, &lleflags, plle);
249ec52ff6dSHans Petter Selasky 		} else {
250ec52ff6dSHans Petter Selasky 			if (m->m_flags & M_BCAST) {
251ec52ff6dSHans Petter Selasky 				memcpy(ih->ib_hwaddr, ifp->if_broadcastaddr,
252ec52ff6dSHans Petter Selasky 				    INFINIBAND_ADDR_LEN);
253ec52ff6dSHans Petter Selasky 			} else {
254ec52ff6dSHans Petter Selasky 				infiniband_ipv4_multicast_map(
255ec52ff6dSHans Petter Selasky 				    ((const struct sockaddr_in *)dst)->sin_addr.s_addr,
256ec52ff6dSHans Petter Selasky 				    ifp->if_broadcastaddr, ih->ib_hwaddr);
257ec52ff6dSHans Petter Selasky 			}
258ec52ff6dSHans Petter Selasky 			ih->ib_protocol = htons(ETHERTYPE_IP);
259ec52ff6dSHans Petter Selasky 			ih->ib_reserved = 0;
260ec52ff6dSHans Petter Selasky 		}
261ec52ff6dSHans Petter Selasky 		break;
262ec52ff6dSHans Petter Selasky #endif
263ec52ff6dSHans Petter Selasky #ifdef INET6
264ec52ff6dSHans Petter Selasky 	case AF_INET6:
265ec52ff6dSHans Petter Selasky 		if ((m->m_flags & M_MCAST) == 0) {
26662e1a437SZhenlei Huang 			int af = RO_GET_FAMILY(ro, dst);
26762e1a437SZhenlei Huang 			error = nd6_resolve(ifp, LLE_SF(af, 0), m, dst, phdr,
26862e1a437SZhenlei Huang 			    &lleflags, plle);
269ec52ff6dSHans Petter Selasky 		} else {
270ec52ff6dSHans Petter Selasky 			infiniband_ipv6_multicast_map(
271ec52ff6dSHans Petter Selasky 			    &((const struct sockaddr_in6 *)dst)->sin6_addr,
272ec52ff6dSHans Petter Selasky 			    ifp->if_broadcastaddr, ih->ib_hwaddr);
273ec52ff6dSHans Petter Selasky 			ih->ib_protocol = htons(ETHERTYPE_IPV6);
274ec52ff6dSHans Petter Selasky 			ih->ib_reserved = 0;
275ec52ff6dSHans Petter Selasky 		}
276ec52ff6dSHans Petter Selasky 		break;
277ec52ff6dSHans Petter Selasky #endif
278ec52ff6dSHans Petter Selasky 	default:
279ec52ff6dSHans Petter Selasky 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
280ec52ff6dSHans Petter Selasky 		if (m != NULL)
281ec52ff6dSHans Petter Selasky 			m_freem(m);
282ec52ff6dSHans Petter Selasky 		return (EAFNOSUPPORT);
283ec52ff6dSHans Petter Selasky 	}
284ec52ff6dSHans Petter Selasky 
285ec52ff6dSHans Petter Selasky 	if (error == EHOSTDOWN) {
286ec52ff6dSHans Petter Selasky 		if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0)
287ec52ff6dSHans Petter Selasky 			error = EHOSTUNREACH;
288ec52ff6dSHans Petter Selasky 	}
289ec52ff6dSHans Petter Selasky 
290ec52ff6dSHans Petter Selasky 	if (error != 0)
291ec52ff6dSHans Petter Selasky 		return (error);
292ec52ff6dSHans Petter Selasky 
293ec52ff6dSHans Petter Selasky 	*pflags = RT_MAY_LOOP;
294ec52ff6dSHans Petter Selasky 	if (lleflags & LLE_IFADDR)
295ec52ff6dSHans Petter Selasky 		*pflags |= RT_L2_ME;
296ec52ff6dSHans Petter Selasky 
297ec52ff6dSHans Petter Selasky 	return (0);
298ec52ff6dSHans Petter Selasky }
299ec52ff6dSHans Petter Selasky 
3009d40cf60SHans Petter Selasky /*
3019d40cf60SHans Petter Selasky  * Infiniband output routine.
3029d40cf60SHans Petter Selasky  */
3039d40cf60SHans Petter Selasky static int
304ec52ff6dSHans Petter Selasky infiniband_output(struct ifnet *ifp, struct mbuf *m,
305ec52ff6dSHans Petter Selasky     const struct sockaddr *dst, struct route *ro)
3069d40cf60SHans Petter Selasky {
307ec52ff6dSHans Petter Selasky 	uint8_t linkhdr[INFINIBAND_HDR_LEN];
308ec52ff6dSHans Petter Selasky 	uint8_t *phdr;
3099d40cf60SHans Petter Selasky 	struct llentry *lle = NULL;
310ec52ff6dSHans Petter Selasky 	struct infiniband_header *ih;
3119d40cf60SHans Petter Selasky 	int error = 0;
312ec52ff6dSHans Petter Selasky 	int hlen;	/* link layer header length */
313ec52ff6dSHans Petter Selasky 	uint32_t pflags;
314ec52ff6dSHans Petter Selasky 	bool addref;
3159d40cf60SHans Petter Selasky 
3169d40cf60SHans Petter Selasky 	NET_EPOCH_ASSERT();
3179d40cf60SHans Petter Selasky 
318ec52ff6dSHans Petter Selasky 	addref = false;
319ec52ff6dSHans Petter Selasky 	phdr = NULL;
320ec52ff6dSHans Petter Selasky 	pflags = 0;
321ec52ff6dSHans Petter Selasky 	if (ro != NULL) {
322ec52ff6dSHans Petter Selasky 		/* XXX BPF uses ro_prepend */
323ec52ff6dSHans Petter Selasky 		if (ro->ro_prepend != NULL) {
324ec52ff6dSHans Petter Selasky 			phdr = ro->ro_prepend;
325ec52ff6dSHans Petter Selasky 			hlen = ro->ro_plen;
326ec52ff6dSHans Petter Selasky 		} else if (!(m->m_flags & (M_BCAST | M_MCAST))) {
327ec52ff6dSHans Petter Selasky 			if ((ro->ro_flags & RT_LLE_CACHE) != 0) {
328ec52ff6dSHans Petter Selasky 				lle = ro->ro_lle;
329ec52ff6dSHans Petter Selasky 				if (lle != NULL &&
330ec52ff6dSHans Petter Selasky 				    (lle->la_flags & LLE_VALID) == 0) {
331ec52ff6dSHans Petter Selasky 					LLE_FREE(lle);
332ec52ff6dSHans Petter Selasky 					lle = NULL;	/* redundant */
333ec52ff6dSHans Petter Selasky 					ro->ro_lle = NULL;
334ec52ff6dSHans Petter Selasky 				}
335ec52ff6dSHans Petter Selasky 				if (lle == NULL) {
336ec52ff6dSHans Petter Selasky 					/* if we lookup, keep cache */
337ec52ff6dSHans Petter Selasky 					addref = 1;
338ec52ff6dSHans Petter Selasky 				} else
339ec52ff6dSHans Petter Selasky 					/*
340ec52ff6dSHans Petter Selasky 					 * Notify LLE code that
341ec52ff6dSHans Petter Selasky 					 * the entry was used
342ec52ff6dSHans Petter Selasky 					 * by datapath.
343ec52ff6dSHans Petter Selasky 					 */
344f3a3b061SAlexander V. Chernikov 					llentry_provide_feedback(lle);
345ec52ff6dSHans Petter Selasky 			}
346ec52ff6dSHans Petter Selasky 			if (lle != NULL) {
347ec52ff6dSHans Petter Selasky 				phdr = lle->r_linkdata;
348ec52ff6dSHans Petter Selasky 				hlen = lle->r_hdrlen;
349ec52ff6dSHans Petter Selasky 				pflags = lle->r_flags;
350ec52ff6dSHans Petter Selasky 			}
351ec52ff6dSHans Petter Selasky 		}
352ec52ff6dSHans Petter Selasky 	}
3539d40cf60SHans Petter Selasky 
3549d40cf60SHans Petter Selasky #ifdef MAC
3559d40cf60SHans Petter Selasky 	error = mac_ifnet_check_transmit(ifp, m);
3569d40cf60SHans Petter Selasky 	if (error)
3579d40cf60SHans Petter Selasky 		goto bad;
3589d40cf60SHans Petter Selasky #endif
3599d40cf60SHans Petter Selasky 
3609d40cf60SHans Petter Selasky 	M_PROFILE(m);
3619d40cf60SHans Petter Selasky 	if (ifp->if_flags & IFF_MONITOR) {
3629d40cf60SHans Petter Selasky 		error = ENETDOWN;
3639d40cf60SHans Petter Selasky 		goto bad;
3649d40cf60SHans Petter Selasky 	}
3659d40cf60SHans Petter Selasky 	if (!((ifp->if_flags & IFF_UP) &&
3669d40cf60SHans Petter Selasky 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
3679d40cf60SHans Petter Selasky 		error = ENETDOWN;
3689d40cf60SHans Petter Selasky 		goto bad;
3699d40cf60SHans Petter Selasky 	}
3709d40cf60SHans Petter Selasky 
371ec52ff6dSHans Petter Selasky 	if (phdr == NULL) {
372ec52ff6dSHans Petter Selasky 		/* No prepend data supplied. Try to calculate ourselves. */
373ec52ff6dSHans Petter Selasky 		phdr = linkhdr;
374ec52ff6dSHans Petter Selasky 		hlen = INFINIBAND_HDR_LEN;
375ec52ff6dSHans Petter Selasky 		error = infiniband_resolve_addr(ifp, m, dst, ro, phdr, &pflags,
376ec52ff6dSHans Petter Selasky 		    addref ? &lle : NULL);
377ec52ff6dSHans Petter Selasky 		if (addref && lle != NULL)
378ec52ff6dSHans Petter Selasky 			ro->ro_lle = lle;
379ec52ff6dSHans Petter Selasky 		if (error != 0)
380ec52ff6dSHans Petter Selasky 			return (error == EWOULDBLOCK ? 0 : error);
3819d40cf60SHans Petter Selasky 	}
3829d40cf60SHans Petter Selasky 
383ec52ff6dSHans Petter Selasky 	if ((pflags & RT_L2_ME) != 0) {
384ec52ff6dSHans Petter Selasky 		update_mbuf_csumflags(m, m);
38562e1a437SZhenlei Huang 		return (if_simloop(ifp, m, RO_GET_FAMILY(ro, dst), 0));
3869d40cf60SHans Petter Selasky 	}
3879d40cf60SHans Petter Selasky 
3889d40cf60SHans Petter Selasky 	/*
389ec52ff6dSHans Petter Selasky 	 * Add local infiniband header. If no space in first mbuf,
3909d40cf60SHans Petter Selasky 	 * allocate another.
3919d40cf60SHans Petter Selasky 	 */
3929d40cf60SHans Petter Selasky 	M_PREPEND(m, INFINIBAND_HDR_LEN, M_NOWAIT);
3939d40cf60SHans Petter Selasky 	if (m == NULL) {
3949d40cf60SHans Petter Selasky 		error = ENOBUFS;
3959d40cf60SHans Petter Selasky 		goto bad;
3969d40cf60SHans Petter Selasky 	}
397ec52ff6dSHans Petter Selasky 	if ((pflags & RT_HAS_HEADER) == 0) {
398ec52ff6dSHans Petter Selasky 		ih = mtod(m, struct infiniband_header *);
399ec52ff6dSHans Petter Selasky 		memcpy(ih, phdr, hlen);
400ec52ff6dSHans Petter Selasky 	}
4019d40cf60SHans Petter Selasky 
4029d40cf60SHans Petter Selasky 	/*
4039d40cf60SHans Petter Selasky 	 * Queue message on interface, update output statistics if
4049d40cf60SHans Petter Selasky 	 * successful, and start output if interface not yet active.
4059d40cf60SHans Petter Selasky 	 */
4069d40cf60SHans Petter Selasky 	return (ifp->if_transmit(ifp, m));
4079d40cf60SHans Petter Selasky bad:
4089d40cf60SHans Petter Selasky 	if (m != NULL)
4099d40cf60SHans Petter Selasky 		m_freem(m);
4109d40cf60SHans Petter Selasky 	return (error);
4119d40cf60SHans Petter Selasky }
4129d40cf60SHans Petter Selasky 
4139d40cf60SHans Petter Selasky /*
4149d40cf60SHans Petter Selasky  * Process a received Infiniband packet.
4159d40cf60SHans Petter Selasky  */
4169d40cf60SHans Petter Selasky static void
4179d40cf60SHans Petter Selasky infiniband_input(struct ifnet *ifp, struct mbuf *m)
4189d40cf60SHans Petter Selasky {
4199d40cf60SHans Petter Selasky 	struct infiniband_header *ibh;
4209d40cf60SHans Petter Selasky 	struct epoch_tracker et;
4219d40cf60SHans Petter Selasky 	int isr;
422fc6c93b6SZhenlei Huang 	bool needs_epoch;
423fc6c93b6SZhenlei Huang 
424*a6b55ee6SGleb Smirnoff 	needs_epoch = (ifp->if_flags & IFF_NEEDSEPOCH);
425*a6b55ee6SGleb Smirnoff #ifdef INVARIANTS
426*a6b55ee6SGleb Smirnoff 	/*
427*a6b55ee6SGleb Smirnoff 	 * This temporary code is here to prevent epoch unaware and unmarked
428*a6b55ee6SGleb Smirnoff 	 * drivers to panic the system.  Once all drivers are taken care of,
429*a6b55ee6SGleb Smirnoff 	 * the whole INVARIANTS block should go away.
430*a6b55ee6SGleb Smirnoff 	 */
431*a6b55ee6SGleb Smirnoff 	if (!needs_epoch && !in_epoch(net_epoch_preempt)) {
432*a6b55ee6SGleb Smirnoff 		static bool printedonce;
433*a6b55ee6SGleb Smirnoff 
434*a6b55ee6SGleb Smirnoff 		needs_epoch = true;
435*a6b55ee6SGleb Smirnoff 		if (!printedonce) {
436*a6b55ee6SGleb Smirnoff 			printedonce = true;
437*a6b55ee6SGleb Smirnoff 			if_printf(ifp, "called %s w/o net epoch! "
438*a6b55ee6SGleb Smirnoff 			    "PLEASE file a bug report.", __func__);
439*a6b55ee6SGleb Smirnoff #ifdef KDB
440*a6b55ee6SGleb Smirnoff 			kdb_backtrace();
441*a6b55ee6SGleb Smirnoff #endif
442*a6b55ee6SGleb Smirnoff 		}
443*a6b55ee6SGleb Smirnoff 	}
444*a6b55ee6SGleb Smirnoff #endif
4459d40cf60SHans Petter Selasky 
4469d40cf60SHans Petter Selasky 	CURVNET_SET_QUIET(ifp->if_vnet);
447fc6c93b6SZhenlei Huang 	if (__predict_false(needs_epoch))
44890820ef1SZhenlei Huang 		NET_EPOCH_ENTER(et);
4499d40cf60SHans Petter Selasky 
4509d40cf60SHans Petter Selasky 	if ((ifp->if_flags & IFF_UP) == 0) {
4519d40cf60SHans Petter Selasky 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
4529d40cf60SHans Petter Selasky 		m_freem(m);
4539d40cf60SHans Petter Selasky 		goto done;
4549d40cf60SHans Petter Selasky 	}
4559d40cf60SHans Petter Selasky 
4569d40cf60SHans Petter Selasky 	ibh = mtod(m, struct infiniband_header *);
4579d40cf60SHans Petter Selasky 
4589d40cf60SHans Petter Selasky 	/*
4599d40cf60SHans Petter Selasky 	 * Reset layer specific mbuf flags to avoid confusing upper
4609d40cf60SHans Petter Selasky 	 * layers:
4619d40cf60SHans Petter Selasky 	 */
4629d40cf60SHans Petter Selasky 	m->m_flags &= ~M_VLANTAG;
4639d40cf60SHans Petter Selasky 	m_clrprotoflags(m);
4649d40cf60SHans Petter Selasky 
4659d40cf60SHans Petter Selasky 	if (INFINIBAND_IS_MULTICAST(ibh->ib_hwaddr)) {
4669d40cf60SHans Petter Selasky 		if (memcmp(ibh->ib_hwaddr, ifp->if_broadcastaddr,
4679d40cf60SHans Petter Selasky 		    ifp->if_addrlen) == 0)
4689d40cf60SHans Petter Selasky 			m->m_flags |= M_BCAST;
4699d40cf60SHans Petter Selasky 		else
4709d40cf60SHans Petter Selasky 			m->m_flags |= M_MCAST;
4719d40cf60SHans Petter Selasky 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
4729d40cf60SHans Petter Selasky 	}
4739d40cf60SHans Petter Selasky 
4749d40cf60SHans Petter Selasky 	/* Let BPF have it before we strip the header. */
475adf62e83SJustin Hibbits 	infiniband_bpf_mtap(ifp, m);
4769d40cf60SHans Petter Selasky 
4779d40cf60SHans Petter Selasky 	/* Allow monitor mode to claim this frame, after stats are updated. */
4789d40cf60SHans Petter Selasky 	if (ifp->if_flags & IFF_MONITOR) {
4799d40cf60SHans Petter Selasky 		m_freem(m);
4809d40cf60SHans Petter Selasky 		goto done;
4819d40cf60SHans Petter Selasky 	}
4829d40cf60SHans Petter Selasky 
4839d40cf60SHans Petter Selasky 	/* Direct packet to correct FIB based on interface config. */
4849d40cf60SHans Petter Selasky 	M_SETFIB(m, ifp->if_fib);
4859d40cf60SHans Petter Selasky 
486a92c4bb6SHans Petter Selasky 	/* Handle input from a lagg<N> port */
487a92c4bb6SHans Petter Selasky 	if (ifp->if_type == IFT_INFINIBANDLAG) {
488a92c4bb6SHans Petter Selasky 		KASSERT(lagg_input_infiniband_p != NULL,
489a92c4bb6SHans Petter Selasky 		    ("%s: if_lagg not loaded!", __func__));
490a92c4bb6SHans Petter Selasky 		m = (*lagg_input_infiniband_p)(ifp, m);
491a92c4bb6SHans Petter Selasky 		if (__predict_false(m == NULL))
492a92c4bb6SHans Petter Selasky 			goto done;
493a92c4bb6SHans Petter Selasky 		ifp = m->m_pkthdr.rcvif;
494a92c4bb6SHans Petter Selasky 	}
495a92c4bb6SHans Petter Selasky 
4969d40cf60SHans Petter Selasky 	/*
4979d40cf60SHans Petter Selasky 	 * Dispatch frame to upper layer.
4989d40cf60SHans Petter Selasky 	 */
4999d40cf60SHans Petter Selasky 	switch (ibh->ib_protocol) {
5009d40cf60SHans Petter Selasky #ifdef INET
5019d40cf60SHans Petter Selasky 	case htons(ETHERTYPE_IP):
5029d40cf60SHans Petter Selasky 		isr = NETISR_IP;
5039d40cf60SHans Petter Selasky 		break;
5049d40cf60SHans Petter Selasky 
5059d40cf60SHans Petter Selasky 	case htons(ETHERTYPE_ARP):
5069d40cf60SHans Petter Selasky 		if (ifp->if_flags & IFF_NOARP) {
5079d40cf60SHans Petter Selasky 			/* Discard packet if ARP is disabled on interface */
5089d40cf60SHans Petter Selasky 			m_freem(m);
5099d40cf60SHans Petter Selasky 			goto done;
5109d40cf60SHans Petter Selasky 		}
5119d40cf60SHans Petter Selasky 		isr = NETISR_ARP;
5129d40cf60SHans Petter Selasky 		break;
5139d40cf60SHans Petter Selasky #endif
5149d40cf60SHans Petter Selasky #ifdef INET6
5159d40cf60SHans Petter Selasky 	case htons(ETHERTYPE_IPV6):
5169d40cf60SHans Petter Selasky 		isr = NETISR_IPV6;
5179d40cf60SHans Petter Selasky 		break;
5189d40cf60SHans Petter Selasky #endif
5199d40cf60SHans Petter Selasky 	default:
5209d40cf60SHans Petter Selasky 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
5219d40cf60SHans Petter Selasky 		m_freem(m);
5229d40cf60SHans Petter Selasky 		goto done;
5239d40cf60SHans Petter Selasky 	}
5249d40cf60SHans Petter Selasky 
5259d40cf60SHans Petter Selasky 	/* Strip off the Infiniband header. */
5269d40cf60SHans Petter Selasky 	m_adj(m, INFINIBAND_HDR_LEN);
5279d40cf60SHans Petter Selasky 
5289d40cf60SHans Petter Selasky #ifdef MAC
5299d40cf60SHans Petter Selasky 	/*
5309d40cf60SHans Petter Selasky 	 * Tag the mbuf with an appropriate MAC label before any other
5319d40cf60SHans Petter Selasky 	 * consumers can get to it.
5329d40cf60SHans Petter Selasky 	 */
5339d40cf60SHans Petter Selasky 	mac_ifnet_create_mbuf(ifp, m);
5349d40cf60SHans Petter Selasky #endif
5359d40cf60SHans Petter Selasky 	/* Allow monitor mode to claim this frame, after stats are updated. */
5369d40cf60SHans Petter Selasky 	netisr_dispatch(isr, m);
5379d40cf60SHans Petter Selasky done:
538fc6c93b6SZhenlei Huang 	if (__predict_false(needs_epoch))
53990820ef1SZhenlei Huang 		NET_EPOCH_EXIT(et);
5409d40cf60SHans Petter Selasky 	CURVNET_RESTORE();
5419d40cf60SHans Petter Selasky }
5429d40cf60SHans Petter Selasky 
5439d40cf60SHans Petter Selasky static int
5441355e2dcSHans Petter Selasky infiniband_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
5451355e2dcSHans Petter Selasky     struct sockaddr *sa)
5469d40cf60SHans Petter Selasky {
5479d40cf60SHans Petter Selasky 	struct sockaddr_dl *sdl;
5489d40cf60SHans Petter Selasky #ifdef INET
5499d40cf60SHans Petter Selasky 	struct sockaddr_in *sin;
5509d40cf60SHans Petter Selasky #endif
5519d40cf60SHans Petter Selasky #ifdef INET6
5529d40cf60SHans Petter Selasky 	struct sockaddr_in6 *sin6;
5539d40cf60SHans Petter Selasky #endif
5549d40cf60SHans Petter Selasky 	uint8_t *e_addr;
5559d40cf60SHans Petter Selasky 
5569d40cf60SHans Petter Selasky 	switch (sa->sa_family) {
5579d40cf60SHans Petter Selasky 	case AF_LINK:
5589d40cf60SHans Petter Selasky 		/*
5599d40cf60SHans Petter Selasky 		 * No mapping needed. Just check that it's a valid MC address.
5609d40cf60SHans Petter Selasky 		 */
5619d40cf60SHans Petter Selasky 		sdl = (struct sockaddr_dl *)sa;
5629d40cf60SHans Petter Selasky 		e_addr = LLADDR(sdl);
5639d40cf60SHans Petter Selasky 		if (!INFINIBAND_IS_MULTICAST(e_addr))
5649d40cf60SHans Petter Selasky 			return (EADDRNOTAVAIL);
5659d40cf60SHans Petter Selasky 		*llsa = NULL;
5669d40cf60SHans Petter Selasky 		return 0;
5679d40cf60SHans Petter Selasky 
5689d40cf60SHans Petter Selasky #ifdef INET
5699d40cf60SHans Petter Selasky 	case AF_INET:
5709d40cf60SHans Petter Selasky 		sin = (struct sockaddr_in *)sa;
5719d40cf60SHans Petter Selasky 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
5729d40cf60SHans Petter Selasky 			return (EADDRNOTAVAIL);
5739d40cf60SHans Petter Selasky 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
5749d40cf60SHans Petter Selasky 		sdl->sdl_alen = INFINIBAND_ADDR_LEN;
5759d40cf60SHans Petter Selasky 		e_addr = LLADDR(sdl);
57601630a49SHans Petter Selasky 		infiniband_ipv4_multicast_map(
57701630a49SHans Petter Selasky 		    sin->sin_addr.s_addr, ifp->if_broadcastaddr, e_addr);
5789d40cf60SHans Petter Selasky 		*llsa = (struct sockaddr *)sdl;
5799d40cf60SHans Petter Selasky 		return (0);
5809d40cf60SHans Petter Selasky #endif
5819d40cf60SHans Petter Selasky #ifdef INET6
5829d40cf60SHans Petter Selasky 	case AF_INET6:
5839d40cf60SHans Petter Selasky 		sin6 = (struct sockaddr_in6 *)sa;
5849d40cf60SHans Petter Selasky 		/*
5859d40cf60SHans Petter Selasky 		 * An IP6 address of 0 means listen to all of the
5869d40cf60SHans Petter Selasky 		 * multicast address used for IP6. This has no meaning
5879d40cf60SHans Petter Selasky 		 * in infiniband.
5889d40cf60SHans Petter Selasky 		 */
5899d40cf60SHans Petter Selasky 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
5909d40cf60SHans Petter Selasky 			return (EADDRNOTAVAIL);
5919d40cf60SHans Petter Selasky 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
5929d40cf60SHans Petter Selasky 			return (EADDRNOTAVAIL);
5939d40cf60SHans Petter Selasky 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
5949d40cf60SHans Petter Selasky 		sdl->sdl_alen = INFINIBAND_ADDR_LEN;
5959d40cf60SHans Petter Selasky 		e_addr = LLADDR(sdl);
59601630a49SHans Petter Selasky 		infiniband_ipv6_multicast_map(
59701630a49SHans Petter Selasky 		    &sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
5989d40cf60SHans Petter Selasky 		*llsa = (struct sockaddr *)sdl;
5999d40cf60SHans Petter Selasky 		return (0);
6009d40cf60SHans Petter Selasky #endif
6019d40cf60SHans Petter Selasky 	default:
6029d40cf60SHans Petter Selasky 		return (EAFNOSUPPORT);
6039d40cf60SHans Petter Selasky 	}
6049d40cf60SHans Petter Selasky }
6059d40cf60SHans Petter Selasky 
6069d40cf60SHans Petter Selasky void
6079d40cf60SHans Petter Selasky infiniband_ifattach(struct ifnet *ifp, const uint8_t *lla, const uint8_t *llb)
6089d40cf60SHans Petter Selasky {
6099d40cf60SHans Petter Selasky 	struct sockaddr_dl *sdl;
6109d40cf60SHans Petter Selasky 	struct ifaddr *ifa;
6119d40cf60SHans Petter Selasky 	int i;
6129d40cf60SHans Petter Selasky 
6139d40cf60SHans Petter Selasky 	ifp->if_addrlen = INFINIBAND_ADDR_LEN;
6149d40cf60SHans Petter Selasky 	ifp->if_hdrlen = INFINIBAND_HDR_LEN;
6159d40cf60SHans Petter Selasky 	ifp->if_mtu = INFINIBAND_MTU;
6169d40cf60SHans Petter Selasky 	if_attach(ifp);
6179d40cf60SHans Petter Selasky 	ifp->if_output = infiniband_output;
6189d40cf60SHans Petter Selasky 	ifp->if_input = infiniband_input;
6199d40cf60SHans Petter Selasky 	ifp->if_resolvemulti = infiniband_resolvemulti;
620ec52ff6dSHans Petter Selasky 	ifp->if_requestencap = infiniband_requestencap;
6219d40cf60SHans Petter Selasky 
6229d40cf60SHans Petter Selasky 	if (ifp->if_baudrate == 0)
6239d40cf60SHans Petter Selasky 		ifp->if_baudrate = IF_Gbps(10); /* default value */
6249d40cf60SHans Petter Selasky 	if (llb != NULL)
6259d40cf60SHans Petter Selasky 		ifp->if_broadcastaddr = llb;
6269d40cf60SHans Petter Selasky 
6279d40cf60SHans Petter Selasky 	ifa = ifp->if_addr;
6289d40cf60SHans Petter Selasky 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
6299d40cf60SHans Petter Selasky 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
6309d40cf60SHans Petter Selasky 	sdl->sdl_type = IFT_INFINIBAND;
6319d40cf60SHans Petter Selasky 	sdl->sdl_alen = ifp->if_addrlen;
6329d40cf60SHans Petter Selasky 
6339d40cf60SHans Petter Selasky 	if (lla != NULL) {
6349d40cf60SHans Petter Selasky 		memcpy(LLADDR(sdl), lla, ifp->if_addrlen);
6359d40cf60SHans Petter Selasky 
6369d40cf60SHans Petter Selasky 		if (ifp->if_hw_addr != NULL)
6379d40cf60SHans Petter Selasky 			memcpy(ifp->if_hw_addr, lla, ifp->if_addrlen);
6389d40cf60SHans Petter Selasky 	} else {
6399d40cf60SHans Petter Selasky 		lla = LLADDR(sdl);
6409d40cf60SHans Petter Selasky 	}
6419d40cf60SHans Petter Selasky 
6429d40cf60SHans Petter Selasky 	/* Attach ethernet compatible network device */
6439d40cf60SHans Petter Selasky 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
6449d40cf60SHans Petter Selasky 
6459d40cf60SHans Petter Selasky 	/* Announce Infiniband MAC address if non-zero. */
6469d40cf60SHans Petter Selasky 	for (i = 0; i < ifp->if_addrlen; i++)
6479d40cf60SHans Petter Selasky 		if (lla[i] != 0)
6489d40cf60SHans Petter Selasky 			break;
6499d40cf60SHans Petter Selasky 	if (i != ifp->if_addrlen)
6509d40cf60SHans Petter Selasky 		if_printf(ifp, "Infiniband address: %20D\n", lla, ":");
6519d40cf60SHans Petter Selasky 
6529d40cf60SHans Petter Selasky 	/* Add necessary bits are setup; announce it now. */
6539d40cf60SHans Petter Selasky 	EVENTHANDLER_INVOKE(infiniband_ifattach_event, ifp);
6549d40cf60SHans Petter Selasky 
6559d40cf60SHans Petter Selasky 	if (IS_DEFAULT_VNET(curvnet))
6569d40cf60SHans Petter Selasky 		devctl_notify("INFINIBAND", ifp->if_xname, "IFATTACH", NULL);
6579d40cf60SHans Petter Selasky }
6589d40cf60SHans Petter Selasky 
6599d40cf60SHans Petter Selasky /*
6609d40cf60SHans Petter Selasky  * Perform common duties while detaching an Infiniband interface
6619d40cf60SHans Petter Selasky  */
6629d40cf60SHans Petter Selasky void
6639d40cf60SHans Petter Selasky infiniband_ifdetach(struct ifnet *ifp)
6649d40cf60SHans Petter Selasky {
6659d40cf60SHans Petter Selasky 	bpfdetach(ifp);
6669d40cf60SHans Petter Selasky 	if_detach(ifp);
6679d40cf60SHans Petter Selasky }
6689d40cf60SHans Petter Selasky 
6699d40cf60SHans Petter Selasky static int
6709d40cf60SHans Petter Selasky infiniband_modevent(module_t mod, int type, void *data)
6719d40cf60SHans Petter Selasky {
6729d40cf60SHans Petter Selasky 	switch (type) {
6739d40cf60SHans Petter Selasky 	case MOD_LOAD:
6749d40cf60SHans Petter Selasky 	case MOD_UNLOAD:
6759d40cf60SHans Petter Selasky 		return (0);
6769d40cf60SHans Petter Selasky 	default:
6779d40cf60SHans Petter Selasky 		return (EOPNOTSUPP);
6789d40cf60SHans Petter Selasky 	}
6799d40cf60SHans Petter Selasky }
6809d40cf60SHans Petter Selasky 
6819d40cf60SHans Petter Selasky static moduledata_t infiniband_mod = {
6829d40cf60SHans Petter Selasky 	.name = "if_infiniband",
6839d40cf60SHans Petter Selasky 	.evhand = &infiniband_modevent,
6849d40cf60SHans Petter Selasky };
6859d40cf60SHans Petter Selasky 
6869d40cf60SHans Petter Selasky DECLARE_MODULE(if_infiniband, infiniband_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
6879d40cf60SHans Petter Selasky MODULE_VERSION(if_infiniband, 1);
688