LCOV - code coverage report
Current view: top level - network - networkd-can.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 128 0.0 %
Date: 2019-08-22 15:41:25 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <net/if.h>
       4             : #include <linux/can/netlink.h>
       5             : 
       6             : #include "netlink-util.h"
       7             : #include "networkd-can.h"
       8             : #include "networkd-link.h"
       9             : #include "networkd-manager.h"
      10             : #include "string-util.h"
      11             : 
      12           0 : static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
      13             :         int r;
      14             : 
      15           0 :         assert(link);
      16             : 
      17           0 :         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
      18           0 :                 return 1;
      19             : 
      20           0 :         r = sd_netlink_message_get_errno(m);
      21           0 :         if (r < 0)
      22             :                 /* we warn but don't fail the link, as it may be brought up later */
      23           0 :                 log_link_warning_errno(link, r, "Could not bring up interface: %m");
      24             : 
      25           0 :         return 1;
      26             : }
      27             : 
      28           0 : static int link_up_can(Link *link) {
      29           0 :         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
      30             :         int r;
      31             : 
      32           0 :         assert(link);
      33             : 
      34           0 :         log_link_debug(link, "Bringing CAN link up");
      35             : 
      36           0 :         r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
      37           0 :         if (r < 0)
      38           0 :                 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
      39             : 
      40           0 :         r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
      41           0 :         if (r < 0)
      42           0 :                 return log_link_error_errno(link, r, "Could not set link flags: %m");
      43             : 
      44           0 :         r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler,
      45             :                                link_netlink_destroy_callback, link);
      46           0 :         if (r < 0)
      47           0 :                 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
      48             : 
      49           0 :         link_ref(link);
      50             : 
      51           0 :         return 0;
      52             : }
      53             : 
      54           0 : static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
      55             :         int r;
      56             : 
      57           0 :         assert(link);
      58             : 
      59           0 :         log_link_debug(link, "Set link");
      60             : 
      61           0 :         r = sd_netlink_message_get_errno(m);
      62           0 :         if (r < 0 && r != -EEXIST) {
      63           0 :                 log_link_error_errno(link, r, "Failed to configure CAN link: %m");
      64           0 :                 link_enter_failed(link);
      65             :         }
      66             : 
      67           0 :         return 1;
      68             : }
      69             : 
      70           0 : static int link_set_can(Link *link) {
      71           0 :         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
      72             :         int r;
      73             : 
      74           0 :         assert(link);
      75           0 :         assert(link->network);
      76           0 :         assert(link->manager);
      77           0 :         assert(link->manager->rtnl);
      78             : 
      79           0 :         log_link_debug(link, "Configuring CAN link.");
      80             : 
      81           0 :         r = sd_rtnl_message_new_link(link->manager->rtnl, &m, RTM_NEWLINK, link->ifindex);
      82           0 :         if (r < 0)
      83           0 :                 return log_link_error_errno(link, r, "Failed to allocate netlink message: %m");
      84             : 
      85           0 :         r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK);
      86           0 :         if (r < 0)
      87           0 :                 return log_link_error_errno(link, r, "Could not set netlink flags: %m");
      88             : 
      89           0 :         r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
      90           0 :         if (r < 0)
      91           0 :                 return log_link_error_errno(link, r, "Failed to open netlink container: %m");
      92             : 
      93           0 :         r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind);
      94           0 :         if (r < 0)
      95           0 :                 return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m");
      96             : 
      97           0 :         if (link->network->can_bitrate > 0 || link->network->can_sample_point > 0) {
      98           0 :                 struct can_bittiming bt = {
      99           0 :                         .bitrate = link->network->can_bitrate,
     100           0 :                         .sample_point = link->network->can_sample_point,
     101             :                 };
     102             : 
     103           0 :                 if (link->network->can_bitrate > UINT32_MAX) {
     104           0 :                         log_link_error(link, "bitrate (%zu) too big.", link->network->can_bitrate);
     105           0 :                         return -ERANGE;
     106             :                 }
     107             : 
     108           0 :                 log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate);
     109           0 :                 if (link->network->can_sample_point > 0)
     110           0 :                         log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10);
     111             :                 else
     112           0 :                         log_link_debug(link, "Using default sample point");
     113             : 
     114           0 :                 r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt));
     115           0 :                 if (r < 0)
     116           0 :                         return log_link_error_errno(link, r, "Could not append IFLA_CAN_BITTIMING attribute: %m");
     117             :         }
     118             : 
     119           0 :         if (link->network->can_restart_us > 0) {
     120             :                 char time_string[FORMAT_TIMESPAN_MAX];
     121             :                 uint64_t restart_ms;
     122             : 
     123           0 :                 if (link->network->can_restart_us == USEC_INFINITY)
     124           0 :                         restart_ms = 0;
     125             :                 else
     126           0 :                         restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC);
     127             : 
     128           0 :                 format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC);
     129             : 
     130           0 :                 if (restart_ms > UINT32_MAX) {
     131           0 :                         log_link_error(link, "restart timeout (%s) too big.", time_string);
     132           0 :                         return -ERANGE;
     133             :                 }
     134             : 
     135           0 :                 log_link_debug(link, "Setting restart = %s", time_string);
     136             : 
     137           0 :                 r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms);
     138           0 :                 if (r < 0)
     139           0 :                         return log_link_error_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
     140             :         }
     141             : 
     142           0 :         if (link->network->can_triple_sampling >= 0) {
     143           0 :                 struct can_ctrlmode cm = {
     144             :                         .mask = CAN_CTRLMODE_3_SAMPLES,
     145           0 :                         .flags = link->network->can_triple_sampling ? CAN_CTRLMODE_3_SAMPLES : 0,
     146             :                 };
     147             : 
     148           0 :                 log_link_debug(link, "%sabling triple-sampling", link->network->can_triple_sampling ? "En" : "Dis");
     149             : 
     150           0 :                 r = sd_netlink_message_append_data(m, IFLA_CAN_CTRLMODE, &cm, sizeof(cm));
     151           0 :                 if (r < 0)
     152           0 :                         return log_link_error_errno(link, r, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
     153             :         }
     154             : 
     155           0 :         r = sd_netlink_message_close_container(m);
     156           0 :         if (r < 0)
     157           0 :                 return log_link_error_errno(link, r, "Failed to close netlink container: %m");
     158             : 
     159           0 :         r = sd_netlink_message_close_container(m);
     160           0 :         if (r < 0)
     161           0 :                 return log_link_error_errno(link, r, "Failed to close netlink container: %m");
     162             : 
     163           0 :         r = netlink_call_async(link->manager->rtnl, NULL, m, link_set_handler,
     164             :                                link_netlink_destroy_callback, link);
     165           0 :         if (r < 0)
     166           0 :                 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
     167             : 
     168           0 :         link_ref(link);
     169             : 
     170           0 :         if (!(link->flags & IFF_UP))
     171           0 :                 return link_up_can(link);
     172             : 
     173           0 :         return 0;
     174             : }
     175             : 
     176           0 : static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
     177             :         int r;
     178             : 
     179           0 :         assert(link);
     180             : 
     181           0 :         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
     182           0 :                 return 1;
     183             : 
     184           0 :         r = sd_netlink_message_get_errno(m);
     185           0 :         if (r < 0) {
     186           0 :                 log_link_warning_errno(link, r, "Could not bring down interface: %m");
     187           0 :                 link_enter_failed(link);
     188           0 :                 return 1;
     189             :         }
     190             : 
     191           0 :         r = link_set_can(link);
     192           0 :         if (r < 0)
     193           0 :                 link_enter_failed(link);
     194             : 
     195           0 :         return 1;
     196             : }
     197             : 
     198           0 : int link_configure_can(Link *link) {
     199             :         int r;
     200             : 
     201           0 :         link_set_state(link, LINK_STATE_CONFIGURING);
     202             : 
     203           0 :         if (streq_ptr(link->kind, "can")) {
     204             :                 /* The CAN interface must be down to configure bitrate, etc... */
     205           0 :                 if ((link->flags & IFF_UP)) {
     206           0 :                         r = link_down(link, link_down_handler);
     207           0 :                         if (r < 0) {
     208           0 :                                 link_enter_failed(link);
     209           0 :                                 return r;
     210             :                         }
     211             :                 } else {
     212           0 :                         r = link_set_can(link);
     213           0 :                         if (r < 0) {
     214           0 :                                 link_enter_failed(link);
     215           0 :                                 return r;
     216             :                         }
     217             :                 }
     218             : 
     219           0 :                 return 0;
     220             :         }
     221             : 
     222           0 :         if (!(link->flags & IFF_UP)) {
     223           0 :                 r = link_up_can(link);
     224           0 :                 if (r < 0) {
     225           0 :                         link_enter_failed(link);
     226           0 :                         return r;
     227             :                 }
     228             :         }
     229             : 
     230           0 :         return 0;
     231             : }

Generated by: LCOV version 1.14