Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2010-2016 Intel Corporation. 3 : : * Copyright(c) 2014 6WIND S.A. 4 : : * All rights reserved. 5 : : */ 6 : : 7 : : #include <string.h> 8 : : #include <unistd.h> 9 : : #include <net/if.h> 10 : : #include <sys/ioctl.h> 11 : : #include <sys/socket.h> 12 : : 13 : : #include <rte_string_fns.h> 14 : : 15 : : #include "pcap_osdep.h" 16 : : 17 : : int 18 : 3 : osdep_iface_index_get(const char *name) 19 : : { 20 : 3 : return if_nametoindex(name); 21 : : } 22 : : 23 : : int 24 : 0 : osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac) 25 : : { 26 : : struct ifreq ifr; 27 : 0 : int if_fd = socket(AF_INET, SOCK_DGRAM, 0); 28 : : 29 [ # # ]: 0 : if (if_fd == -1) 30 : : return -1; 31 : : 32 : 0 : rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name)); 33 [ # # ]: 0 : if (ioctl(if_fd, SIOCGIFHWADDR, &ifr)) { 34 : 0 : close(if_fd); 35 : 0 : return -1; 36 : : } 37 : : 38 : 0 : memcpy(mac->addr_bytes, ifr.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN); 39 : : 40 : 0 : close(if_fd); 41 : 0 : return 0; 42 : : } 43 : : 44 : : int 45 : 5 : osdep_iface_link_status(const char *if_name) 46 : : { 47 : : struct ifreq ifr; 48 : : int fd, status = 0; 49 : : 50 : 5 : fd = socket(AF_INET, SOCK_DGRAM, 0); 51 [ + - ]: 5 : if (fd == -1) 52 : : return -1; 53 : : 54 : 5 : rte_strscpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name)); 55 [ + - ]: 5 : if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0) { 56 : : /* 57 : : * IFF_UP means administratively up. 58 : : * IFF_RUNNING means operationally up (carrier detected). 59 : : * Both must be set for link to be considered up. 60 : : */ 61 [ + - ]: 5 : if ((ifr.ifr_flags & IFF_UP) && (ifr.ifr_flags & IFF_RUNNING)) 62 : : status = 1; 63 : : } else { 64 : : status = -1; 65 : : } 66 : : 67 : 5 : close(fd); 68 : 5 : return status; 69 : : }