8000 minix/minix/net/lwip/ndev.c at master · Amogh-Dasture/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1019 lines (824 loc) · 29 KB

ndev.c

File metadata and controls

1019 lines (824 loc) · 29 KB
continue;
if (ds_retrieve_label_endpt(ndev_array[slot].ndev_label,
&endpt) != OK)
ndev_down(&ndev_array[slot]);
}
}
/*
* A network device driver has sent a reply to our initialization request.
*/
static void
ndev_init_reply(struct ndev * ndev, const message * m_ptr)
{
struct ndev_hwaddr hwaddr;
uint8_t hwaddr_len, max_send, max_recv;
const char *name;
int enabled;
/*
* Make sure that we were waiting for a reply to an initialization
* request, and that this is the reply to that request.
*/
if (NDEV_ACTIVE(ndev) ||
m_ptr->m_netdriver_ndev_init_reply.id != ndev->ndev_sendq.nq_head)
return;
/*
* Do just enough sanity checking on the data to pass it up to the
* ethif layer, which will check the rest (e.g., name duplicates).
*/
if (memchr(m_ptr->m_netdriver_ndev_init_reply.name, '\0',
sizeof(m_ptr->m_netdriver_ndev_init_reply.name)) == NULL ||
m_ptr->m_netdriver_ndev_init_reply.name[0] == '\0') {
printf("LWIP: driver %d provided invalid name\n",
m_ptr->m_source);
ndev_down(ndev);
return;
}
hwaddr_len = m_ptr->m_netdriver_ndev_init_reply.hwaddr_len;
if (hwaddr_len < 1 || hwaddr_len > __arraycount(hwaddr.nhwa_addr)) {
printf("LWIP: driver %d provided invalid HW-addr length\n",
m_ptr->m_source);
ndev_down(ndev);
return;
}
if ((max_send = m_ptr->m_netdriver_ndev_init_reply.max_send) < 1 ||
(max_recv = m_ptr->m_netdriver_ndev_init_reply.max_recv) < 1) {
printf("LWIP: driver %d provided invalid queue maximum\n",
m_ptr->m_source);
ndev_down(ndev);
return;
}
/*
* If the driver is new, allocate a new ethif object for it. On
* success, or if the driver was restarted, (re)enable the interface.
* Both calls may fail, in which case we should forget about the
* driver. It may continue to send us messages, which we should then
* discard.
*/
name = m_ptr->m_netdriver_ndev_init_reply.name;
if (ndev->ndev_ethif == NULL) {
ndev->ndev_ethif = ethif_add((ndev_id_t)(ndev - ndev_array),
name, m_ptr->m_netdriver_ndev_init_reply.caps);
name = NULL;
}
if (ndev->ndev_ethif != NULL) {
/*
* Set the maximum numbers of pending requests (for each
* direction) first, because enabling the interface may cause
* the ethif layer to start sending requests immediately.
*/
ndev->ndev_sendq.nq_max = max_send;
ndev->ndev_sendq.nq_head++;
/*
* Limit the maximum number of concurrently pending receive
* requests to our configured maximum. For send requests, we
* use a more dynamic approach with spare request objects.
*/
if (max_recv > NDEV_RECVQ)
max_recv = NDEV_RECVQ;
ndev->ndev_recvq.nq_max = max_recv;
ndev->ndev_recvq.nq_head++;
memset(&hwaddr, 0, sizeof(hwaddr));
memcpy(hwaddr.nhwa_addr,
m_ptr->m_netdriver_ndev_init_reply.hwaddr, hwaddr_len);
/*
* Provide a NULL pointer for the name if we have only just
* added the interface at all. The callee may use this to
* determine whether the driver is new or has been restarted.
*/
enabled = ethif_enable(ndev->ndev_ethif, name, &hwaddr,
m_ptr->m_netdriver_ndev_init_reply.hwaddr_len,
m_ptr->m_netdriver_ndev_init_reply.caps,
m_ptr->m_netdriver_ndev_init_reply.link,
m_ptr->m_netdriver_ndev_init_reply.media);
} else
enabled = FALSE;
/*
* If we did not manage to enable the interface, remove it again,
* possibly also from the ethif layer.
*/
if (!enabled)
ndev_down(ndev);
else
ndev_pending--;
}
/*
* Request that a network device driver change its configuration. This
* function allows for configuration of various different driver and device
* aspects: the I/O mode (and multicast receipt list), the enabled (sub)set of
* capabilities, the driver-specific flags, and the hardware address. Each of
* these settings may be changed by setting the corresponding NDEV_SET_ flag in
* the 'set' field of the given configuration structure. It is explicitly
* allowed to generate a request with no NDEV_SET_ flags; such a request will
* be sent to the driver and ultimately generate a response. Return OK if the
* configuration request was sent to the driver, EBUSY if no (more) requests
* can be sent to the driver right now, or ENOMEM on grant allocation failure.
*/
int
ndev_conf(ndev_id_t id, const struct ndev_conf * nconf)
{
struct ndev *ndev;
struct ndev_req *nreq;
uint32_t seq;
message m;
cp_grant_id_t grant;
int r;
assert(id < __arraycount(ndev_array));
ndev = &ndev_array[id];
assert(ndev->ndev_endpt != NONE);
assert(NDEV_ACTIVE(ndev));
if ((nreq = ndev_queue_get(&ndev->ndev_sendq, NDEV_CONF,
&seq)) == NULL)
return EBUSY;
memset(&m, 0, sizeof(m));
m.m_type = NDEV_CONF;
m.m_ndev_netdriver_conf.id = seq;
m.m_ndev_netdriver_conf.set = nconf->nconf_set;
grant = GRANT_INVALID;
if (nconf->nconf_set & NDEV_SET_MODE) {
m.m_ndev_netdriver_conf.mode = nconf->nconf_mode;
if (nconf->nconf_mode & NDEV_MODE_MCAST_LIST) {
assert(nconf->nconf_mclist != NULL);
assert(nconf->nconf_mccount != 0);
grant = cpf_grant_direct(ndev->ndev_endpt,
(vir_bytes)nconf->nconf_mclist,
sizeof(nconf->nconf_mclist[0]) *
nconf->nconf_mccount, CPF_READ);
if (!GRANT_VALID(grant))
return ENOMEM;
m.m_ndev_netdriver_conf.mcast_count =
nconf->nconf_mccount;
}
}
m.m_ndev_netdriver_conf.mcast_grant = grant;
if (nconf->nconf_set & NDEV_SET_CAPS)
m.m_ndev_netdriver_conf.caps = nconf->nconf_caps;
if (nconf->nconf_set & NDEV_SET_FLAGS)
m.m_ndev_netdriver_conf.flags = nconf->nconf_flags;
if (nconf->nconf_set & NDEV_SET_MEDIA)
m.m_ndev_netdriver_conf.media = nconf->nconf_media;
if (nconf->nconf_set & NDEV_SET_HWADDR)
memcpy(m.m_ndev_netdriver_conf.hwaddr,
nconf->nconf_hwaddr.nhwa_addr,
__arraycount(m.m_ndev_netdriver_conf.hwaddr));
if ((r = asynsend3(ndev->ndev_endpt, &m, AMF_NOREPLY)) != OK)
panic("asynsend to driver failed: %d", r);
nreq->nreq_grant[0] = grant; /* may also be invalid */
nreq->nreq_grant[1] = GRANT_INVALID;
ndev_queue_add(&ndev->ndev_sendq, nreq);
return OK;
}
/*
* The network device driver has sent a reply to a configuration request.
*/
static void
ndev_conf_reply(struct ndev * ndev, const message * m_ptr)
{
/*
* Was this the request we were waiting for? If so, remove it from the
* send queue. Otherwise, ignore this reply message.
*/
if (!NDEV_ACTIVE(ndev) || !ndev_queue_remove(&ndev->ndev_sendq,
NDEV_CONF, m_ptr->m_netdriver_ndev_reply.id))
return;
/* Tell the ethif layer about the updated configuration. */
assert(ndev->ndev_ethif != NULL);
ethif_configured(ndev->ndev_ethif,
m_ptr->m_netdriver_ndev_reply.result);
}
/*
* Construct a packet send or receive request and send it off to a network
* driver. The given pbuf chain may be part of a queue. Return OK if the
* request was successfully sent, or ENOMEM on grant allocation failure.
*/
static int
ndev_transfer(struct ndev * ndev, const struct pbuf * pbuf, int do_send,
uint32_t seq, struct ndev_req * nreq)
{
cp_grant_id_t grant;
message m;
unsigned int i;
size_t left;
int r;
memset(&m, 0, sizeof(m));
m.m_type = (do_send) ? NDEV_SEND : NDEV_RECV;
m.m_ndev_netdriver_transfer.id = seq;
left = pbuf->tot_len;
for (i = 0; left > 0; i++) {
assert(i < NDEV_IOV_MAX);
grant = cpf_grant_direct(ndev->ndev_endpt,
(vir_bytes)pbuf->payload, pbuf->len,
(do_send) ? CPF_READ : CPF_WRITE);
if (!GRANT_VALID(grant)) {
while (i-- > 0)
(void)cpf_revoke(nreq->nreq_grant[i]);
return ENOMEM;
}
m.m_ndev_netdriver_transfer.grant[i] = grant;
m.m_ndev_netdriver_transfer.len[i] = pbuf->len;
nreq->nreq_grant[i] = grant;
assert(left >= pbuf->len);
left -= pbuf->len;
pbuf = pbuf->next;
}
m.m_ndev_netdriver_transfer.count = i;
/*
* Unless the array is full, an invalid grant marks the end of the list
* of invalid grants.
*/
if (i < __arraycount(nreq->nreq_grant))
nreq->nreq_grant[i] = GRANT_INVALID;
if ((r = asynsend3(ndev->ndev_endpt, &m, AMF_NOREPLY)) != OK)
panic("asynsend to driver failed: %d", r);
return OK;
}
/*
* Send a packet to the given network driver. Return OK if the packet is sent
* off to the driver, EBUSY if no (more) packets can be sent to the driver at
* this time, or ENOMEM on grant allocation failure.
*
* The use of 'pbuf' in this interface is a bit ugly, but it saves us from
* having to go through an intermediate representation (e.g. an iovec array)
* for the data being sent. The same applies to ndev_receive().
*/
int
ndev_send(ndev_id_t id, const struct pbuf * pbuf)
{
struct ndev *ndev;
struct ndev_req *nreq;
uint32_t seq;
int r;
assert(id < __arraycount(ndev_array));
ndev = &ndev_array[id];
assert(ndev->ndev_endpt != NONE);
assert(NDEV_ACTIVE(ndev));
if ((nreq = ndev_queue_get(&ndev->ndev_sendq, NDEV_SEND,
&seq)) == NULL)
return EBUSY;
if ((r = ndev_transfer(ndev, pbuf, TRUE /*do_send*/, seq, nreq)) != OK)
return r;
ndev_queue_add(&ndev->ndev_sendq, nreq);
return OK;
}
/*
* The network device driver has sent a reply to a send request.
*/
static void
ndev_send_reply(struct ndev * ndev, const message * m_ptr)
{
/*
* Was this the request we were waiting for? If so, remove it from the
* send queue. Otherwise, ignore this reply message.
*/
if (!NDEV_ACTIVE(ndev) || !ndev_queue_remove(&ndev->ndev_sendq,
NDEV_SEND, m_ptr->m_netdriver_ndev_reply.id))
return;
/* Tell the ethif layer about the result of the transmission. */
assert(ndev->ndev_ethif != NULL);
ethif_sent(ndev->ndev_ethif,
m_ptr->m_netdriver_ndev_reply.result);
}
/*
* Return TRUE if a new receive request can be spawned for a particular network
* driver, or FALSE if its queue of receive requests is full. This call exists
* merely to avoid needless buffer allocatin in the case that ndev_recv() is
* going to return EBUSY anyway.
*/
int
ndev_can_recv(ndev_id_t id)
{
struct ndev *ndev;
assert(id < __arraycount(ndev_array));
ndev = &ndev_array[id];
assert(ndev->ndev_endpt != NONE);
assert(NDEV_ACTIVE(ndev));
return (ndev->ndev_recvq.nq_count < ndev->ndev_recvq.nq_max);
}
/*
* Start the process of receiving a packet from a network driver. The packet
* will be stored in the given pbuf chain upon completion. Return OK if the
* receive request is sent to the driver, EBUSY if the maximum number of
* concurrent receive requests has been reached for this driver, or ENOMEM on
* grant allocation failure.
*/
int
ndev_recv(ndev_id_t id, struct pbuf * pbuf)
{
struct ndev *ndev;
struct ndev_req *nreq;
uint32_t seq;
int r;
assert(id < __arraycount(ndev_array));
ndev = &ndev_array[id];
assert(ndev->ndev_endpt != NONE);
assert(NDEV_ACTIVE(ndev));
if ((nreq = ndev_queue_get(&ndev->ndev_recvq, NDEV_RECV,
&seq)) == NULL)
return EBUSY;
if ((r = ndev_transfer(ndev, pbuf, FALSE /*do_send*/, seq,
nreq)) != OK)
return r;
ndev_queue_add(&ndev->ndev_recvq, nreq);
return OK;
}
/*
* The network device driver has sent a reply to a receive request.
*/
static void
ndev_recv_reply(struct ndev * ndev, const message * m_ptr)
{
/*
* Was this the request we were waiting for? If so, remove it from the
* receive queue. Otherwise, ignore this reply message.
*/
if (!NDEV_ACTIVE(ndev) || !ndev_queue_remove(&ndev->ndev_recvq,
NDEV_RECV, m_ptr->m_netdriver_ndev_reply.id))
return;
/* Tell the ethif layer about the result of the receipt. */
assert(ndev->ndev_ethif != NULL);
ethif_received(ndev->ndev_ethif,
m_ptr->m_netdriver_ndev_reply.result);
}
/*
* A network device driver sent a status report to us. Process it and send a
* reply.
*/
static void
ndev_status(struct ndev * ndev, const message * m_ptr)
{
message m;
int r;
if (!NDEV_ACTIVE(ndev))
return;
/* Tell the ethif layer about the status update. */
assert(ndev->ndev_ethif != NULL);
ethif_status(ndev->ndev_ethif, m_ptr->m_netdriver_ndev_status.link,
m_ptr->m_netdriver_ndev_status.media,
m_ptr->m_netdriver_ndev_status.oerror,
m_ptr->m_netdriver_ndev_status.coll,
m_ptr->m_netdriver_ndev_status.ierror,
m_ptr->m_netdriver_ndev_status.iqdrop);
/*
* Send a reply, so that the driver knows it can send a new status
* update without risking asynsend queue overflows. The ID of these
* messages is chosen by the driver and and we simply echo it.
*/
memset(&m, 0, sizeof(m));
m.m_type = NDEV_STATUS_REPLY;
m.m_ndev_netdriver_status_reply.id = m_ptr->m_netdriver_ndev_status.id;
if ((r = asynsend(m_ptr->m_source, &m)) != OK)
panic("asynsend to driver failed: %d", r);
}
/*
* Process a network driver reply message.
*/
void
ndev_process(const message * m_ptr, int ipc_status)
{
struct ndev *ndev;
endpoint_t endpt;
ndev_id_t slot;
/* Find the slot of the driver that sent the message, if any. */
endpt = m_ptr->m_source;
for (slot = 0, ndev = ndev_array; slot < ndev_max; slot++, ndev++)
if (ndev->ndev_endpt == endpt)
break;
/*
* If we cannot find a slot for the driver, drop the message. We may
* be ignoring the driver because it misbehaved or we are out of slots.
*/
if (slot == ndev_max)
return;
/*
* Process the reply message. For future compatibility, ignore any
* unrecognized message types.
*/
switch (m_ptr->m_type) {
case NDEV_INIT_REPLY:
ndev_init_reply(ndev, m_ptr);
break;
case NDEV_CONF_REPLY:
ndev_conf_reply(ndev, m_ptr);
0