| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup inet
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief IP link provider for Ethernet
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <async.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <inet/iplink_srv.h>
|
|---|
| 40 | #include <io/log.h>
|
|---|
| 41 | #include <loc.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "ethip.h"
|
|---|
| 46 | #include "ethip_nic.h"
|
|---|
| 47 |
|
|---|
| 48 | #define NAME "eth"
|
|---|
| 49 |
|
|---|
| 50 | static int ethip_open(iplink_conn_t *conn);
|
|---|
| 51 | static int ethip_close(iplink_conn_t *conn);
|
|---|
| 52 | static int ethip_send(iplink_conn_t *conn, iplink_srv_sdu_t *sdu);
|
|---|
| 53 | static int ethip_get_mtu(iplink_conn_t *conn, size_t *mtu);
|
|---|
| 54 |
|
|---|
| 55 | static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
|---|
| 56 |
|
|---|
| 57 | static iplink_ops_t ethip_iplink_ops = {
|
|---|
| 58 | .open = ethip_open,
|
|---|
| 59 | .close = ethip_close,
|
|---|
| 60 | .send = ethip_send,
|
|---|
| 61 | .get_mtu = ethip_get_mtu
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | static int ethip_init(void)
|
|---|
| 65 | {
|
|---|
| 66 | int rc;
|
|---|
| 67 |
|
|---|
| 68 | async_set_client_connection(ethip_client_conn);
|
|---|
| 69 |
|
|---|
| 70 | rc = loc_server_register(NAME);
|
|---|
| 71 | if (rc != EOK) {
|
|---|
| 72 | log_msg(LVL_ERROR, "Failed registering server.");
|
|---|
| 73 | return rc;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | rc = ethip_nic_discovery_start();
|
|---|
| 77 | if (rc != EOK)
|
|---|
| 78 | return rc;
|
|---|
| 79 |
|
|---|
| 80 | return EOK;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int ethip_iplink_init(ethip_nic_t *nic)
|
|---|
| 84 | {
|
|---|
| 85 | int rc;
|
|---|
| 86 | service_id_t sid;
|
|---|
| 87 | category_id_t iplink_cat;
|
|---|
| 88 | static unsigned link_num = 0;
|
|---|
| 89 | char *svc_name = NULL;
|
|---|
| 90 |
|
|---|
| 91 | log_msg(LVL_DEBUG, "ethip_iplink_init()");
|
|---|
| 92 |
|
|---|
| 93 | nic->iplink.ops = ðip_iplink_ops;
|
|---|
| 94 | nic->iplink.arg = nic;
|
|---|
| 95 |
|
|---|
| 96 | rc = asprintf(&svc_name, "net/eth%u", ++link_num);
|
|---|
| 97 | if (rc < 0) {
|
|---|
| 98 | log_msg(LVL_ERROR, "Out of memory.");
|
|---|
| 99 | goto error;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | rc = loc_service_register(svc_name, &sid);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| 104 | log_msg(LVL_ERROR, "Failed registering service %s.", svc_name);
|
|---|
| 105 | goto error;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
|
|---|
| 109 | if (rc != EOK) {
|
|---|
| 110 | log_msg(LVL_ERROR, "Failed resolving category 'iplink'.");
|
|---|
| 111 | goto error;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | rc = loc_service_add_to_cat(sid, iplink_cat);
|
|---|
| 115 | if (rc != EOK) {
|
|---|
| 116 | log_msg(LVL_ERROR, "Failed adding %s to category.", svc_name);
|
|---|
| 117 | goto error;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | return EOK;
|
|---|
| 121 |
|
|---|
| 122 | error:
|
|---|
| 123 | if (svc_name != NULL)
|
|---|
| 124 | free(svc_name);
|
|---|
| 125 | return rc;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 129 | {
|
|---|
| 130 | log_msg(LVL_DEBUG, "ethip_client_conn(%u)", (unsigned) IPC_GET_ARG1(*icall));
|
|---|
| 131 | if (0) iplink_conn(iid, icall, NULL);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | static int ethip_open(iplink_conn_t *conn)
|
|---|
| 135 | {
|
|---|
| 136 | log_msg(LVL_DEBUG, "ethip_open()");
|
|---|
| 137 | return EOK;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | static int ethip_close(iplink_conn_t *conn)
|
|---|
| 141 | {
|
|---|
| 142 | log_msg(LVL_DEBUG, "ethip_open()");
|
|---|
| 143 | return EOK;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | static int ethip_send(iplink_conn_t *conn, iplink_srv_sdu_t *sdu)
|
|---|
| 147 | {
|
|---|
| 148 | log_msg(LVL_DEBUG, "ethip_send()");
|
|---|
| 149 | return EOK;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | static int ethip_get_mtu(iplink_conn_t *conn, size_t *mtu)
|
|---|
| 153 | {
|
|---|
| 154 | log_msg(LVL_DEBUG, "ethip_get_mtu()");
|
|---|
| 155 | *mtu = 1500;
|
|---|
| 156 | return EOK;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | int main(int argc, char *argv[])
|
|---|
| 160 | {
|
|---|
| 161 | int rc;
|
|---|
| 162 |
|
|---|
| 163 | printf(NAME ": HelenOS IP over Ethernet service\n");
|
|---|
| 164 |
|
|---|
| 165 | if (log_init(NAME, LVL_DEBUG) != EOK) {
|
|---|
| 166 | printf(NAME ": Failed to initialize logging.\n");
|
|---|
| 167 | return 1;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | rc = ethip_init();
|
|---|
| 171 | if (rc != EOK)
|
|---|
| 172 | return 1;
|
|---|
| 173 |
|
|---|
| 174 | printf(NAME ": Accepting connections.\n");
|
|---|
| 175 | task_retval(0);
|
|---|
| 176 | async_manager();
|
|---|
| 177 |
|
|---|
| 178 | /* Not reached */
|
|---|
| 179 | return 0;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** @}
|
|---|
| 183 | */
|
|---|