Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp38484iog; Sun, 12 Jun 2022 18:05:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwLkLwvtz+SWuYoQ0nEvRHkAYprn9DKV3ewG1/ZWiBnX9suFKlIsLXtrfe+lwFICgmZs4O7 X-Received: by 2002:a17:907:2c43:b0:715:85cb:21a with SMTP id hf3-20020a1709072c4300b0071585cb021amr4291881ejc.541.1655082335481; Sun, 12 Jun 2022 18:05:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655082335; cv=none; d=google.com; s=arc-20160816; b=DTIsdh6QIQ0hIClpgzevTCVLNlc5rogSVWG9j2tIXvs3OZVuwMWQiTXF8xj2mRbhEz W1KE8as0Ve3HeAM3fQ/TsCS8XMDSeW9S/TNhvkzfGdrYGi8DAd2fYSk47vqT3+/+ilxl Sss5S9d3RgqtzEd0NFczG2UgYef9TlH7j5zOgiye6rsXXJ2KFnceHXHOoddCXdKuOOHU 4vW5yzcahXc6rCHbqLBNDFyCSlhJrqEmuwEtmhsTZ9+PWUWLt1MfSQ66hbovRTNdQy3k WVbGBMI0LlIOoJFg5cTIlqC0/eVR3U7UlcHwAGoFhp+rktmtIxpkzdM7zrrEe5C1s7Ra CkaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=K0omjWVAQknKh80VLWsQETRXyxuJ8wx+RydBE1BQOZE=; b=zhozJrDelEfFII/GT9g9i4A7MCrXFoOGbJUqtmFgxZlTL8jpqcEiWuiplWdqpmTUMm HgsZls1JL9J5mWP5Y7XULd0ifbvWVz0ZDFDDXC3JBnTLWNhGsR6Rtjd9gprpZIR5jRSE IqtBbNnFcyR7xTMJrgwAV8tObrtdwlureoc202loAveaT502woLInW+cbH5xThdG44EC ZufNRTMLg2JB5/k4SXDZX92sg6zGnZ3O7OLefGJZqT0SWDT1L3Z46pL5u0IVTNAy74RT KwG7WH6S6ayDlwWWxB9E9ZrH2UWOQoHNPOXyQ3VfTU8uzdp0M/adX4S++LDsWlD8F6we Nu9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=I9pBc0a6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ht9-20020a170907608900b00711fd60fa17si7175331ejc.34.2022.06.12.18.04.43; Sun, 12 Jun 2022 18:05:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=I9pBc0a6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236838AbiFLVkE (ORCPT + 99 others); Sun, 12 Jun 2022 17:40:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236630AbiFLVjy (ORCPT ); Sun, 12 Jun 2022 17:39:54 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6FB5A1A83B for ; Sun, 12 Jun 2022 14:39:51 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id h23so7654237ejj.12 for ; Sun, 12 Jun 2022 14:39:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K0omjWVAQknKh80VLWsQETRXyxuJ8wx+RydBE1BQOZE=; b=I9pBc0a6INon93gsw110Q3R49kNnqiUZv/rIvYZKM6Bt65S7egtDb33jfbKVH1vf68 7RfGVpcK9vb59CattQvz0uuAL07URs4DuRaBvEv2OpPRJkfLtbQkuEpxlLOlT687oMQT kmEKHXPuoJ1NenOaHO8ijhW1jw48K9d023e8Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K0omjWVAQknKh80VLWsQETRXyxuJ8wx+RydBE1BQOZE=; b=6YM5lQnomvvWZZxBXsLWQgbrpCRmM0HmyFxyuTD0UpMTICvmsfimgwXt7VeXYxWTuc Ba6IORoH0rKKuiaikwnrjk/00CxdlnO9mZCLkTVKkqBzI8fPUHhP3uioxJKRZE1Upx/d 6gwwoutwG10oLmuhtuGbw7howi/vLVi4O5KKbbF09l9fEwHyroIAeLgyhWYiCEctx3Ho /zopdgMjzE/4lXc+UYfVdrUpy9VfXkbVhCrR8G39WScR2X4QpinpN0Mf3wx0sPRqvV2p YVLs3UjeVGopj8/IteEwEqJNVm/okaVhjTTqWPvUqhG718oOBXc7rCTjdeKva0buWjgx qx+g== X-Gm-Message-State: AOAM530S49pdqZDa00o1Wh6svYmpBNls4voWR6YKMEMd9dEGuxFpgSvA CwpwsHS4hW13Guty5GRreRyJLK3qabaZnA== X-Received: by 2002:a17:907:1b07:b0:6fe:2cbc:15c5 with SMTP id mp7-20020a1709071b0700b006fe2cbc15c5mr49898642ejc.677.1655069989671; Sun, 12 Jun 2022 14:39:49 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-80-116-90-174.pool80116.interbusiness.it. [80.116.90.174]) by smtp.gmail.com with ESMTPSA id u10-20020a1709061daa00b00711d546f8a8sm2909398ejh.139.2022.06.12.14.39.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jun 2022 14:39:49 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: michael@amarulasolutions.com, Amarula patchwork , Oliver Hartkopp , Dario Binacchi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Marc Kleine-Budde , Paolo Abeni , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v3 03/13] can: slcan: use the alloc_can_skb() helper Date: Sun, 12 Jun 2022 23:39:17 +0200 Message-Id: <20220612213927.3004444-4-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220612213927.3004444-1-dario.binacchi@amarulasolutions.com> References: <20220612213927.3004444-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It is used successfully by most (if not all) CAN device drivers. It allows to remove replicated code. Signed-off-by: Dario Binacchi --- Changes in v3: - Increment the error counter in case of decoding failure. Changes in v2: - Put the data into the allocated skb directly instead of first filling the "cf" on the stack and then doing a memcpy(). drivers/net/can/slcan.c | 70 +++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index 6162a9c21672..c39580b142e0 100644 --- a/drivers/net/can/slcan.c +++ b/drivers/net/can/slcan.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include @@ -143,85 +144,80 @@ static struct net_device **slcan_devs; static void slc_bump(struct slcan *sl) { struct sk_buff *skb; - struct can_frame cf; + struct can_frame *cf; int i, tmp; u32 tmpid; char *cmd = sl->rbuff; - memset(&cf, 0, sizeof(cf)); + skb = alloc_can_skb(sl->dev, &cf); + if (unlikely(!skb)) { + sl->dev->stats.rx_dropped++; + return; + } switch (*cmd) { case 'r': - cf.can_id = CAN_RTR_FLAG; + cf->can_id = CAN_RTR_FLAG; fallthrough; case 't': /* store dlc ASCII value and terminate SFF CAN ID string */ - cf.len = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN]; + cf->len = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN]; sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0; /* point to payload data behind the dlc */ cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1; break; case 'R': - cf.can_id = CAN_RTR_FLAG; + cf->can_id = CAN_RTR_FLAG; fallthrough; case 'T': - cf.can_id |= CAN_EFF_FLAG; + cf->can_id |= CAN_EFF_FLAG; /* store dlc ASCII value and terminate EFF CAN ID string */ - cf.len = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN]; + cf->len = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN]; sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0; /* point to payload data behind the dlc */ cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1; break; default: - return; + goto decode_failed; } if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid)) - return; + goto decode_failed; - cf.can_id |= tmpid; + cf->can_id |= tmpid; /* get len from sanitized ASCII value */ - if (cf.len >= '0' && cf.len < '9') - cf.len -= '0'; + if (cf->len >= '0' && cf->len < '9') + cf->len -= '0'; else - return; + goto decode_failed; /* RTR frames may have a dlc > 0 but they never have any data bytes */ - if (!(cf.can_id & CAN_RTR_FLAG)) { - for (i = 0; i < cf.len; i++) { + if (!(cf->can_id & CAN_RTR_FLAG)) { + for (i = 0; i < cf->len; i++) { tmp = hex_to_bin(*cmd++); if (tmp < 0) - return; - cf.data[i] = (tmp << 4); + goto decode_failed; + + cf->data[i] = (tmp << 4); tmp = hex_to_bin(*cmd++); if (tmp < 0) - return; - cf.data[i] |= tmp; + goto decode_failed; + + cf->data[i] |= tmp; } } - skb = dev_alloc_skb(sizeof(struct can_frame) + - sizeof(struct can_skb_priv)); - if (!skb) - return; - - skb->dev = sl->dev; - skb->protocol = htons(ETH_P_CAN); - skb->pkt_type = PACKET_BROADCAST; - skb->ip_summed = CHECKSUM_UNNECESSARY; - - can_skb_reserve(skb); - can_skb_prv(skb)->ifindex = sl->dev->ifindex; - can_skb_prv(skb)->skbcnt = 0; - - skb_put_data(skb, &cf, sizeof(struct can_frame)); - sl->dev->stats.rx_packets++; - if (!(cf.can_id & CAN_RTR_FLAG)) - sl->dev->stats.rx_bytes += cf.len; + if (!(cf->can_id & CAN_RTR_FLAG)) + sl->dev->stats.rx_bytes += cf->len; netif_rx(skb); + return; + +decode_failed: + sl->dev->stats.rx_errors++; + dev_kfree_skb(skb); } /* parse tty input stream */ -- 2.32.0