david/ipxe
david
/
ipxe
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/drivers/net/ipoib.c

687 lines
18 KiB
C
Raw Normal View History

2007-09-17 06:04:58 +02:00
/*
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
FILE_LICENCE ( GPL2_OR_LATER );
2007-09-17 06:04:58 +02:00
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
2007-09-17 06:04:58 +02:00
#include <string.h>
#include <byteswap.h>
#include <errno.h>
#include <gpxe/if_arp.h>
#include <gpxe/iobuf.h>
#include <gpxe/netdevice.h>
#include <gpxe/infiniband.h>
#include <gpxe/ib_qset.h>
#include <gpxe/ib_pathrec.h>
#include <gpxe/ib_mcast.h>
2007-09-17 06:04:58 +02:00
#include <gpxe/ipoib.h>
/** @file
*
* IP over Infiniband
*/
/** Number of IPoIB send work queue entries */
#define IPOIB_NUM_SEND_WQES 2
2007-09-17 06:04:58 +02:00
/** Number of IPoIB receive work queue entries */
#define IPOIB_NUM_RECV_WQES 4
2007-09-17 06:04:58 +02:00
/** Number of IPoIB completion entries */
#define IPOIB_NUM_CQES 8
/** An IPoIB device */
struct ipoib_device {
/** Network device */
struct net_device *netdev;
/** Underlying Infiniband device */
struct ib_device *ibdev;
/** Queue set */
struct ib_queue_set qset;
/** Broadcast MAC */
struct ipoib_mac broadcast;
/** Joined to multicast group
*
* This flag indicates whether or not we have initiated the
* join to the IPv4 multicast group.
*/
int broadcast_joined;
2007-09-17 06:04:58 +02:00
};
/** Broadcast IPoIB address */
static struct ipoib_mac ipoib_broadcast = {
.qpn = htonl ( IB_QPN_BROADCAST ),
.gid.u.bytes = { 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff },
};
/****************************************************************************
*
* IPoIB peer cache
*
****************************************************************************
*/
2007-09-17 11:39:30 +02:00
/**
* IPoIB peer address
2007-09-17 11:39:30 +02:00
*
* The IPoIB link-layer header is only four bytes long and so does not
* have sufficient room to store IPoIB MAC address(es). We therefore
* maintain a cache of MAC addresses identified by a single-byte key,
* and abuse the spare two bytes within the link-layer header to
* communicate these MAC addresses between the link-layer code and the
* netdevice driver.
2007-09-17 11:39:30 +02:00
*/
struct ipoib_peer {
/** Key */
uint8_t key;
/** MAC address */
struct ipoib_mac mac;
2007-09-17 11:39:30 +02:00
};
/** Number of IPoIB peer cache entries
*
* Must be a power of two.
*/
#define IPOIB_NUM_CACHED_PEERS 4
2007-09-17 11:39:30 +02:00
/** IPoIB peer address cache */
static struct ipoib_peer ipoib_peer_cache[IPOIB_NUM_CACHED_PEERS];
2007-09-17 11:39:30 +02:00
/** Oldest IPoIB peer cache entry index */
static unsigned int ipoib_peer_cache_idx = 1;
2007-09-17 11:39:30 +02:00
/**
* Look up cached peer by key
*
* @v key Peer cache key
* @ret peer Peer cache entry, or NULL
*/
static struct ipoib_peer * ipoib_lookup_peer_by_key ( unsigned int key ) {
struct ipoib_peer *peer;
unsigned int i;
for ( i = 0 ; i < IPOIB_NUM_CACHED_PEERS ; i++ ) {
peer = &ipoib_peer_cache[i];
if ( peer->key == key )
return peer;
}
if ( key != 0 ) {
DBG ( "IPoIB warning: peer cache lost track of key %x while "
"still in use\n", key );
}
return NULL;
}
/**
* Store GID and QPN in peer cache
*
* @v gid Peer GID
* @v qpn Peer QPN
* @ret peer Peer cache entry
*/
static struct ipoib_peer * ipoib_cache_peer ( const struct ipoib_mac *mac ) {
struct ipoib_peer *peer;
unsigned int key;
unsigned int i;
/* Look for existing cache entry */
for ( i = 0 ; i < IPOIB_NUM_CACHED_PEERS ; i++ ) {
peer = &ipoib_peer_cache[i];
if ( memcmp ( &peer->mac, mac, sizeof ( peer->mac ) ) == 0 )
return peer;
}
/* No entry found: create a new one */
key = ipoib_peer_cache_idx++;
peer = &ipoib_peer_cache[ key % IPOIB_NUM_CACHED_PEERS ];
if ( peer->key )
DBG ( "IPoIB peer %x evicted from cache\n", peer->key );
memset ( peer, 0, sizeof ( *peer ) );
peer->key = key;
memcpy ( &peer->mac, mac, sizeof ( peer->mac ) );
DBG ( "IPoIB peer %x has MAC %s\n",
peer->key, ipoib_ntoa ( &peer->mac ) );
return peer;
}
2007-09-17 06:04:58 +02:00
/****************************************************************************
*
* IPoIB link layer
*
****************************************************************************
*/
/**
* Add IPoIB link-layer header
2007-09-17 06:04:58 +02:00
*
* @v netdev Network device
2007-09-17 06:04:58 +02:00
* @v iobuf I/O buffer
* @v ll_dest Link-layer destination address
* @v ll_source Source link-layer address
* @v net_proto Network-layer protocol, in network-byte order
* @ret rc Return status code
2007-09-17 06:04:58 +02:00
*/
static int ipoib_push ( struct net_device *netdev __unused,
struct io_buffer *iobuf, const void *ll_dest,
const void *ll_source __unused, uint16_t net_proto ) {
2007-09-17 06:04:58 +02:00
struct ipoib_hdr *ipoib_hdr =
iob_push ( iobuf, sizeof ( *ipoib_hdr ) );
const struct ipoib_mac *dest_mac = ll_dest;
const struct ipoib_mac *src_mac = ll_source;
struct ipoib_peer *dest;
struct ipoib_peer *src;
/* Add link-layer addresses to cache */
dest = ipoib_cache_peer ( dest_mac );
src = ipoib_cache_peer ( src_mac );
2007-09-17 06:04:58 +02:00
/* Build IPoIB header */
ipoib_hdr->proto = net_proto;
ipoib_hdr->u.peer.dest = dest->key;
ipoib_hdr->u.peer.src = src->key;
2007-09-17 06:04:58 +02:00
return 0;
2007-09-17 06:04:58 +02:00
}
/**
* Remove IPoIB link-layer header
2007-09-17 06:04:58 +02:00
*
* @v netdev Network device
* @v iobuf I/O buffer
* @ret ll_dest Link-layer destination address
* @ret ll_source Source link-layer address
* @ret net_proto Network-layer protocol, in network-byte order
* @ret rc Return status code
2007-09-17 06:04:58 +02:00
*/
static int ipoib_pull ( struct net_device *netdev,
struct io_buffer *iobuf, const void **ll_dest,
const void **ll_source, uint16_t *net_proto ) {
struct ipoib_device *ipoib = netdev->priv;
2007-09-17 06:04:58 +02:00
struct ipoib_hdr *ipoib_hdr = iobuf->data;
struct ipoib_peer *dest;
struct ipoib_peer *source;
2007-09-17 06:04:58 +02:00
/* Sanity check */
if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
DBG ( "IPoIB packet too short for link-layer header\n" );
DBG_HD ( iobuf->data, iob_len ( iobuf ) );
2007-09-17 06:04:58 +02:00
return -EINVAL;
}
/* Strip off IPoIB header */
iob_pull ( iobuf, sizeof ( *ipoib_hdr ) );
/* Identify source and destination addresses, and clear
* reserved word in IPoIB header
*/
dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
source = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.src );
ipoib_hdr->u.reserved = 0;
/* Fill in required fields */
*ll_dest = ( dest ? &dest->mac : &ipoib->broadcast );
*ll_source = ( source ? &source->mac : &ipoib->broadcast );
*net_proto = ipoib_hdr->proto;
return 0;
2007-09-17 06:04:58 +02:00
}
/**
* Transcribe IPoIB address
*
* @v ll_addr Link-layer address
* @ret string Link-layer address in human-readable format
*/
const char * ipoib_ntoa ( const void *ll_addr ) {
2007-09-17 11:39:30 +02:00
static char buf[45];
const struct ipoib_mac *mac = ll_addr;
snprintf ( buf, sizeof ( buf ), "%08x:%08x:%08x:%08x:%08x",
2007-09-17 11:39:30 +02:00
htonl ( mac->qpn ), htonl ( mac->gid.u.dwords[0] ),
htonl ( mac->gid.u.dwords[1] ),
htonl ( mac->gid.u.dwords[2] ),
htonl ( mac->gid.u.dwords[3] ) );
return buf;
2007-09-17 06:04:58 +02:00
}
/**
* Hash multicast address
*
* @v af Address family
* @v net_addr Network-layer address
* @v ll_addr Link-layer address to fill in
* @ret rc Return status code
*/
static int ipoib_mc_hash ( unsigned int af __unused,
const void *net_addr __unused,
void *ll_addr __unused ) {
return -ENOTSUP;
}
2007-09-17 06:04:58 +02:00
/** IPoIB protocol */
struct ll_protocol ipoib_protocol __ll_protocol = {
.name = "IPoIB",
.ll_proto = htons ( ARPHRD_INFINIBAND ),
.ll_addr_len = IPOIB_ALEN,
.ll_header_len = IPOIB_HLEN,
.push = ipoib_push,
.pull = ipoib_pull,
2007-09-17 06:04:58 +02:00
.ntoa = ipoib_ntoa,
.mc_hash = ipoib_mc_hash,
2007-09-17 06:04:58 +02:00
};
/**
* Allocate IPoIB device
*
* @v priv_size Size of driver private data
* @ret netdev Network device, or NULL
*/
struct net_device * alloc_ipoibdev ( size_t priv_size ) {
struct net_device *netdev;
netdev = alloc_netdev ( priv_size );
if ( netdev ) {
netdev->ll_protocol = &ipoib_protocol;
netdev->ll_broadcast = ( uint8_t * ) &ipoib_broadcast;
netdev->max_pkt_len = IB_MAX_PAYLOAD_SIZE;
}
return netdev;
}
2007-09-17 06:04:58 +02:00
/****************************************************************************
*
* IPoIB network device
*
****************************************************************************
*/
/**
* Transmit packet via IPoIB network device
*
* @v netdev Network device
* @v iobuf I/O buffer
* @ret rc Return status code
*/
static int ipoib_transmit ( struct net_device *netdev,
struct io_buffer *iobuf ) {
struct ipoib_device *ipoib = netdev->priv;
struct ib_device *ibdev = ipoib->ibdev;
struct ipoib_hdr *ipoib_hdr;
struct ipoib_peer *dest;
2007-09-17 11:39:30 +02:00
struct ib_address_vector av;
int rc;
2007-09-17 06:04:58 +02:00
/* Sanity check */
if ( iob_len ( iobuf ) < sizeof ( *ipoib_hdr ) ) {
2007-09-17 06:04:58 +02:00
DBGC ( ipoib, "IPoIB %p buffer too short\n", ipoib );
return -EINVAL;
}
ipoib_hdr = iobuf->data;
2007-09-17 06:04:58 +02:00
/* Attempting transmission while link is down will put the
* queue pair into an error state, so don't try it.
*/
if ( ! ib_link_ok ( ibdev ) )
return -ENETUNREACH;
/* Identify destination address */
dest = ipoib_lookup_peer_by_key ( ipoib_hdr->u.peer.dest );
if ( ! dest )
return -ENXIO;
ipoib_hdr->u.reserved = 0;
2007-09-17 11:39:30 +02:00
/* Construct address vector */
memset ( &av, 0, sizeof ( av ) );
av.qpn = ntohl ( dest->mac.qpn );
av.gid_present = 1;
memcpy ( &av.gid, &dest->mac.gid, sizeof ( av.gid ) );
if ( ( rc = ib_resolve_path ( ibdev, &av ) ) != 0 ) {
/* Path not resolved yet */
return rc;
}
return ib_post_send ( ibdev, ipoib->qset.qp, &av, iobuf );
2007-09-17 06:04:58 +02:00
}
/**
* Handle IPoIB send completion
2007-09-17 06:04:58 +02:00
*
* @v ibdev Infiniband device
* @v qp Queue pair
* @v iobuf I/O buffer
* @v rc Completion status code
2007-09-17 06:04:58 +02:00
*/
static void ipoib_complete_send ( struct ib_device *ibdev __unused,
struct ib_queue_pair *qp,
struct io_buffer *iobuf, int rc ) {
struct ipoib_device *ipoib = ib_qp_get_ownerdata ( qp );
2007-09-17 06:04:58 +02:00
netdev_tx_complete_err ( ipoib->netdev, iobuf, rc );
2007-09-17 06:04:58 +02:00
}
/**
* Handle IPoIB receive completion
2007-09-17 06:04:58 +02:00
*
* @v ibdev Infiniband device
* @v qp Queue pair
* @v av Address vector, or NULL
2007-09-17 06:04:58 +02:00
* @v iobuf I/O buffer
* @v rc Completion status code
2007-09-17 06:04:58 +02:00
*/
static void ipoib_complete_recv ( struct ib_device *ibdev __unused,
struct ib_queue_pair *qp,
struct ib_address_vector *av,
struct io_buffer *iobuf, int rc ) {
struct ipoib_device *ipoib = ib_qp_get_ownerdata ( qp );
struct net_device *netdev = ipoib->netdev;
struct ipoib_hdr *ipoib_hdr;
struct ipoib_mac ll_src;
struct ipoib_peer *src;
2007-09-17 06:04:58 +02:00
if ( rc != 0 ) {
netdev_rx_err ( netdev, iobuf, rc );
return;
}
/* Sanity check */
if ( iob_len ( iobuf ) < sizeof ( struct ipoib_hdr ) ) {
DBGC ( ipoib, "IPoIB %p received packet too short to "
"contain IPoIB header\n", ipoib );
DBGC_HD ( ipoib, iobuf->data, iob_len ( iobuf ) );
netdev_rx_err ( netdev, iobuf, -EIO );
return;
2007-09-17 06:04:58 +02:00
}
ipoib_hdr = iobuf->data;
2007-09-17 06:04:58 +02:00
/* Parse source address */
if ( av->gid_present ) {
ll_src.qpn = htonl ( av->qpn );
memcpy ( &ll_src.gid, &av->gid, sizeof ( ll_src.gid ) );
src = ipoib_cache_peer ( &ll_src );
ipoib_hdr->u.peer.src = src->key;
}
/* Hand off to network layer */
netdev_rx ( netdev, iobuf );
2007-09-17 06:04:58 +02:00
}
/** IPoIB completion operations */
static struct ib_completion_queue_operations ipoib_cq_op = {
.complete_send = ipoib_complete_send,
.complete_recv = ipoib_complete_recv,
};
2007-09-17 06:04:58 +02:00
/**
* Poll IPoIB network device
*
* @v netdev Network device
*/
static void ipoib_poll ( struct net_device *netdev ) {
struct ipoib_device *ipoib = netdev->priv;
struct ib_device *ibdev = ipoib->ibdev;
ib_poll_eq ( ibdev );
2007-09-17 06:04:58 +02:00
}
/**
* Enable/disable interrupts on IPoIB network device
*
* @v netdev Network device
* @v enable Interrupts should be enabled
*/
static void ipoib_irq ( struct net_device *netdev __unused,
int enable __unused ) {
/* No implementation */
}
/**
* Join IPv4 broadcast multicast group
*
* @v ipoib IPoIB device
* @ret rc Return status code
*/
static int ipoib_join_broadcast_group ( struct ipoib_device *ipoib ) {
int rc;
if ( ( rc = ib_mcast_join ( ipoib->ibdev, ipoib->qset.qp,
&ipoib->broadcast.gid ) ) != 0 ) {
DBGC ( ipoib, "IPoIB %p could not join broadcast group: %s\n",
ipoib, strerror ( rc ) );
return rc;
}
ipoib->broadcast_joined = 1;
return 0;
}
/**
* Leave IPv4 broadcast multicast group
*
* @v ipoib IPoIB device
*/
static void ipoib_leave_broadcast_group ( struct ipoib_device *ipoib ) {
if ( ipoib->broadcast_joined ) {
ib_mcast_leave ( ipoib->ibdev, ipoib->qset.qp,
&ipoib->broadcast.gid );
ipoib->broadcast_joined = 0;
}
}
2007-09-17 06:04:58 +02:00
/**
* Open IPoIB network device
*
* @v netdev Network device
* @ret rc Return status code
*/
static int ipoib_open ( struct net_device *netdev ) {
struct ipoib_device *ipoib = netdev->priv;
struct ib_device *ibdev = ipoib->ibdev;
struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
2007-09-17 06:04:58 +02:00
int rc;
/* Open IB device */
if ( ( rc = ib_open ( ibdev ) ) != 0 ) {
DBGC ( ipoib, "IPoIB %p could not open device: %s\n",
ipoib, strerror ( rc ) );
goto err_ib_open;
}
/* Allocate queue set */
if ( ( rc = ib_create_qset ( ibdev, &ipoib->qset, IPOIB_NUM_CQES,
&ipoib_cq_op, IPOIB_NUM_SEND_WQES,
IPOIB_NUM_RECV_WQES, 0 ) ) != 0 ) {
DBGC ( ipoib, "IPoIB %p could not allocate queue set: %s\n",
ipoib, strerror ( rc ) );
goto err_create_qset;
}
ib_qp_set_ownerdata ( ipoib->qset.qp, ipoib );
/* Update MAC address with QPN */
mac->qpn = htonl ( ipoib->qset.qp->qpn );
/* Fill receive rings */
ib_refill_recv ( ibdev, ipoib->qset.qp );
2007-09-17 06:04:58 +02:00
/* Join broadcast group */
if ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) {
DBGC ( ipoib, "IPoIB %p could not join broadcast group: %s\n",
ipoib, strerror ( rc ) );
goto err_join_broadcast;
}
2007-09-17 06:04:58 +02:00
return 0;
err_join_broadcast:
ib_destroy_qset ( ibdev, &ipoib->qset );
err_create_qset:
ib_close ( ibdev );
err_ib_open:
return rc;
2007-09-17 06:04:58 +02:00
}
/**
* Close IPoIB network device
*
* @v netdev Network device
*/
static void ipoib_close ( struct net_device *netdev ) {
struct ipoib_device *ipoib = netdev->priv;
struct ipoib_mac *mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
2007-09-17 06:04:58 +02:00
/* Leave broadcast group */
ipoib_leave_broadcast_group ( ipoib );
2007-09-17 06:04:58 +02:00
/* Remove QPN from MAC address */
mac->qpn = 0;
/* Tear down the queues */
ib_destroy_qset ( ipoib->ibdev, &ipoib->qset );
/* Close IB device */
ib_close ( ipoib->ibdev );
2007-09-17 06:04:58 +02:00
}
/** IPoIB network device operations */
static struct net_device_operations ipoib_operations = {
.open = ipoib_open,
.close = ipoib_close,
.transmit = ipoib_transmit,
.poll = ipoib_poll,
.irq = ipoib_irq,
};
/**
* Update IPoIB dynamic Infiniband parameters
*
* @v ipoib IPoIB device
*
* The Infiniband port GID and partition key will change at runtime,
* when the link is established (or lost). The MAC address is based
* on the port GID, and the broadcast GID is based on the partition
* key. This function recalculates these IPoIB device parameters.
*/
static void ipoib_set_ib_params ( struct ipoib_device *ipoib ) {
struct ib_device *ibdev = ipoib->ibdev;
struct net_device *netdev = ipoib->netdev;
struct ipoib_mac *mac;
/* Calculate GID portion of MAC address based on port GID */
mac = ( ( struct ipoib_mac * ) netdev->ll_addr );
memcpy ( &mac->gid, &ibdev->gid, sizeof ( mac->gid ) );
/* Calculate broadcast GID based on partition key */
memcpy ( &ipoib->broadcast, &ipoib_broadcast,
sizeof ( ipoib->broadcast ) );
ipoib->broadcast.gid.u.words[2] = htons ( ibdev->pkey );
/* Set net device link state to reflect Infiniband link state */
if ( ib_link_ok ( ibdev ) ) {
netdev_link_up ( netdev );
} else {
netdev_link_down ( netdev );
}
}
/**
* Handle link status change
*
* @v ibdev Infiniband device
*/
void ipoib_link_state_changed ( struct ib_device *ibdev ) {
struct net_device *netdev = ib_get_ownerdata ( ibdev );
struct ipoib_device *ipoib = netdev->priv;
int rc;
/* Leave existing broadcast group */
ipoib_leave_broadcast_group ( ipoib );
/* Update MAC address and broadcast GID based on new port GID
* and partition key.
*/
ipoib_set_ib_params ( ipoib );
/* Join new broadcast group */
if ( ( rc = ipoib_join_broadcast_group ( ipoib ) ) != 0 ) {
DBGC ( ipoib, "IPoIB %p could not rejoin broadcast group: "
"%s\n", ipoib, strerror ( rc ) );
return;
}
}
2007-09-17 06:04:58 +02:00
/**
* Probe IPoIB device
*
* @v ibdev Infiniband device
* @ret rc Return status code
*/
int ipoib_probe ( struct ib_device *ibdev ) {
struct net_device *netdev;
struct ipoib_device *ipoib;
int rc;
/* Allocate network device */
netdev = alloc_ipoibdev ( sizeof ( *ipoib ) );
if ( ! netdev )
return -ENOMEM;
netdev_init ( netdev, &ipoib_operations );
ipoib = netdev->priv;
ib_set_ownerdata ( ibdev, netdev );
netdev->dev = ibdev->dev;
netdev->ll_broadcast = ( ( uint8_t * ) &ipoib->broadcast );
2007-09-17 06:04:58 +02:00
memset ( ipoib, 0, sizeof ( *ipoib ) );
ipoib->netdev = netdev;
2007-09-17 06:04:58 +02:00
ipoib->ibdev = ibdev;
/* Calculate as much of the broadcast GID and the MAC address
* as we can. We won't know either of these in full until we
* have link-up.
*/
ipoib_set_ib_params ( ipoib );
2007-09-17 06:04:58 +02:00
/* Register network device */
if ( ( rc = register_netdev ( netdev ) ) != 0 )
goto err_register_netdev;
return 0;
err_register_netdev:
netdev_nullify ( netdev );
netdev_put ( netdev );
return rc;
}
/**
* Remove IPoIB device
*
* @v ibdev Infiniband device
*/
void ipoib_remove ( struct ib_device *ibdev ) {
struct net_device *netdev = ib_get_ownerdata ( ibdev );
unregister_netdev ( netdev );
netdev_nullify ( netdev );
netdev_put ( netdev );
}