static int parse_args(int argc, char **argv) { int ch; struct net_port *port; struct vxlan_peer peer; #ifdef LWIP_DEBUG while ((ch = getopt(argc, argv, "P:V:e:k:d")) != -1) { #else while ((ch = getopt(argc, argv, "P:V:e:k:")) != -1) { #endif switch (ch) { case 'P': port = &BR0.plug.net_port; if (parse_port(&port->net, optarg)) return -1; port->rte_port_type = RTE_PORT_TYPE_PLUG; break; case 'V': memset(&peer, 0, sizeof(peer)); if (parse_vxlan(&peer, optarg)) return -1; if (bridge_add_vxlan(&BR0, &peer) != 0) return -1; break; case 'e': if (nr_ports >= PORT_MAX) break; port = &ports[nr_ports]; if (parse_port(&port->net, optarg)) return -1; port->rte_port_type = RTE_PORT_TYPE_ETH; nr_ports++; break; case 'k': if (nr_ports >= PORT_MAX) break; port = &ports[nr_ports]; if (parse_port(&port->net, optarg)) return -1; port->rte_port_type = RTE_PORT_TYPE_KNI; nr_ports++; break; #ifdef LWIP_DEBUG case 'd': debug_flags |= (LWIP_DBG_ON| LWIP_DBG_TRACE| LWIP_DBG_STATE| LWIP_DBG_FRESH| LWIP_DBG_HALT); break; #endif default: return -1; } } argc -= optind; argv += optind; return 0; } #define IP4_OR_NULL(ip_addr) ((ip_addr).addr == IPADDR_ANY ? 0 : &(ip_addr)) static int create_eth_port(struct net_port *net_port, int socket_id) { RTE_VERIFY(net_port->rte_port_type == RTE_PORT_TYPE_ETH); struct net *net = &net_port->net; struct rte_port_eth_params params = { .port_id = net->port_id, .nb_rx_desc = RTE_TEST_RX_DESC_DEFAULT, .nb_tx_desc = RTE_TEST_TX_DESC_DEFAULT, .mempool = pktmbuf_pool, }; if (!IP4_OR_NULL(net_port->net.ip_addr)) { struct rte_port_eth *eth_port; eth_port = rte_port_eth_create(¶ms, socket_id, net_port); if (!eth_port) rte_exit(EXIT_FAILURE, "Cannot alloc kni port\n"); bridge_add_port(&BR0, net_port); } else { struct ethif *ethif; struct netif *netif; ethif = ethif_alloc(socket_id); if (ethif == NULL) rte_exit(EXIT_FAILURE, "Cannot alloc eth port\n"); if (ethif_init(ethif, ¶ms, socket_id, net_port) != ERR_OK) rte_exit(EXIT_FAILURE, "Cannot init eth port\n"); netif = ðif->netif; netif_add(netif, IP4_OR_NULL(net->ip_addr), IP4_OR_NULL(net->netmask), IP4_OR_NULL(net->gw), ethif, ethif_added_cb, ethernet_input); netif_set_up(netif); } return 0; } static int create_kni_port(struct net_port *net_port, int socket_id) { RTE_VERIFY(net_port->rte_port_type == RTE_PORT_TYPE_KNI); struct net *net = &net_port->net; struct rte_port_kni_params params = { .name = net->name, .mbuf_size = MAX_PACKET_SZ, .mempool = pktmbuf_pool, }; if (!IP4_OR_NULL(net_port->net.ip_addr)) { struct rte_port_kni *kni_port; kni_port = rte_port_kni_create(¶ms, socket_id, net_port); if (!kni_port) rte_exit(EXIT_FAILURE, "Cannot alloc kni port\n"); bridge_add_port(&BR0, net_port); } else { struct kniif *kniif; struct netif *netif; kniif = kniif_alloc(socket_id); if (kniif == NULL) rte_exit(EXIT_FAILURE, "Cannot alloc kni interface\n"); if (kniif_init(kniif, ¶ms, socket_id, net_port) != ERR_OK) rte_exit(EXIT_FAILURE, "Cannot init kni interface\n"); netif = &kniif->netif; netif_add(netif, IP4_OR_NULL(net->ip_addr), IP4_OR_NULL(net->netmask), IP4_OR_NULL(net->gw), kniif, kniif_added_cb, ethernet_input); netif_set_up(netif); } return 0; } static int create_plug_port(struct net_port *net_port, int socket_id) { RTE_VERIFY(net_port->rte_port_type == RTE_PORT_TYPE_PLUG); struct net *net = &net_port->net; if (!IP4_OR_NULL(net_port->net.ip_addr)) { struct rte_port_plug *plug_port; struct rte_port_plug_params params = { .tx_burst = bridge_tx_vxlan_burst, .private_data = &BR0, }; plug_port = rte_port_plug_create(¶ms, socket_id, net_port); if (!plug_port) rte_exit(EXIT_FAILURE, "Cannot alloc plug port\n"); if (bridge_add_port(&BR0, net_port) != 0) rte_exit(EXIT_FAILURE, "Cannot add bridge port\n"); } else {
int network_config(void) { struct ifnet * ifn; in_addr_t ip_addr; in_addr_t netmask = INADDR_ANY; in_addr_t gw_addr = INADDR_ANY; char s[64]; char s1[16]; char s2[16]; char * env; uint8_t * ethaddr; uint64_t esn; int dhcp; /* Initialize MAC address with the MCU's UID */ ethaddr = (uint8_t *)&esn; esn = *((uint64_t *)STM32F_UID); DCC_LOG2(LOG_TRACE, "ESN=0x%08x%08x", esn >> 32, esn); ethaddr[0] = (ethaddr[0] & 0xfc) | 0x02; /* Locally administered MAC */ DCC_LOG(LOG_TRACE, "tcpip_init()."); tcpip_init(); if ((env = getenv("IPCFG")) == NULL) { tracef("IPCFG not set, using defaults!\n"); /* default configuration */ strcpy(s, "192.168.0.128 255.255.255.0 192.168.0.1 0"); /* set the default configuration */ setenv("IPCFG", s, 1); } else { strcpy(s, env); } if ((env = getenv("ETHADDR")) != NULL) { eth_strtomac(ethaddr, env); } else { trace("Ethernet MAC address not set, using defaults!"); DCC_LOG(LOG_WARNING, "Ethernet MAC address not set."); } tracef("* mac addr: %02x-%02x-%02x-%02x-%02x-%02x", ethaddr[0], ethaddr[1], ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]); if (!inet_aton(strtok(s, " ,"), (struct in_addr *)&ip_addr)) { DCC_LOG(LOG_WARNING, "inet_aton() failed."); return -1; } if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&netmask)) { if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&gw_addr)) { dhcp = strtoul(strtok(NULL, ""), NULL, 0); } } /* initialize the Ethernet interface */ /* configure the ip address */ ifn = ethif_init(ethaddr, ip_addr, netmask); // ifn = loopif_init(ip_addr, netmask); ifn_getname(ifn, s); ifn_ipv4_get(ifn, &ip_addr, &netmask); tracef("* netif %s: %s, %s", s, inet_ntop(AF_INET, (void *)&ip_addr, s1, 16), inet_ntop(AF_INET, (void *)&netmask, s2, 16)); if (gw_addr != INADDR_ANY) { /* add the default route (gateway) to ethif */ ipv4_route_add(INADDR_ANY, INADDR_ANY, gw_addr, ifn); tracef("* default route gw: %s", inet_ntop(AF_INET, (void *)&gw_addr, s1, 16)); } if (dhcp) { #if 0 /* configure the initial ip address */ dhcp_start(); /* schedule the interface to be configured through dhcp */ dhcp_ifconfig(ethif, dhcp_callback); tracef("DHCP started.\n"); #endif } return 0; }