Skip to content

Commit

Permalink
can: skb: add skb CAN frame data length helpers
Browse files Browse the repository at this point in the history
Add two helpers to retrieve the data length from CAN sk_buffs and prepare
the length information to be a uint16 value for the CAN XL support.

Acked-by: Vincent Mailhol <[email protected]>
Signed-off-by: Oliver Hartkopp <[email protected]>
Link: https://lore.kernel.org/all/[email protected]
Signed-off-by: Marc Kleine-Budde <[email protected]>
  • Loading branch information
hartkopp authored and marckleinebudde committed Sep 15, 2022
1 parent 96a7457 commit 467ef4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion drivers/net/can/dev/rx-offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
struct net_device *dev = offload->dev;
struct net_device_stats *stats = &dev->stats;
struct sk_buff *skb;
u8 len;
unsigned int len;
int err;

skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
Expand Down
12 changes: 4 additions & 8 deletions drivers/net/can/dev/skb.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
EXPORT_SYMBOL_GPL(can_put_echo_skb);

struct sk_buff *
__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr,
unsigned int *frame_len_ptr)
__can_get_echo_skb(struct net_device *dev, unsigned int idx,
unsigned int *len_ptr, unsigned int *frame_len_ptr)
{
struct can_priv *priv = netdev_priv(dev);

Expand All @@ -108,16 +108,12 @@ __can_get_echo_skb(struct net_device *dev, unsigned int idx, u8 *len_ptr,
*/
struct sk_buff *skb = priv->echo_skb[idx];
struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
struct canfd_frame *cf = (struct canfd_frame *)skb->data;

if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)
skb_tstamp_tx(skb, skb_hwtstamps(skb));

/* get the real payload length for netdev statistics */
if (cf->can_id & CAN_RTR_FLAG)
*len_ptr = 0;
else
*len_ptr = cf->len;
*len_ptr = can_skb_get_data_len(skb);

if (frame_len_ptr)
*frame_len_ptr = can_skb_priv->frame_len;
Expand Down Expand Up @@ -147,7 +143,7 @@ unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx,
unsigned int *frame_len_ptr)
{
struct sk_buff *skb;
u8 len;
unsigned int len;

skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
if (!skb)
Expand Down
24 changes: 23 additions & 1 deletion include/linux/can/skb.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void can_flush_echo_skb(struct net_device *dev);
int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
unsigned int idx, unsigned int frame_len);
struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
u8 *len_ptr, unsigned int *frame_len_ptr);
unsigned int *len_ptr,
unsigned int *frame_len_ptr);
unsigned int __must_check can_get_echo_skb(struct net_device *dev,
unsigned int idx,
unsigned int *frame_len_ptr);
Expand Down Expand Up @@ -113,4 +114,25 @@ static inline bool can_is_canfd_skb(const struct sk_buff *skb)
return (skb->len == CANFD_MTU && cfd->len <= CANFD_MAX_DLEN);
}

/* get length element value from can[fd]_frame structure */
static inline unsigned int can_skb_get_len_val(struct sk_buff *skb)
{
const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;

return cfd->len;
}

/* get needed data length inside CAN frame for all frame types (RTR aware) */
static inline unsigned int can_skb_get_data_len(struct sk_buff *skb)
{
unsigned int len = can_skb_get_len_val(skb);
const struct can_frame *cf = (struct can_frame *)skb->data;

/* RTR frames have an actual length of zero */
if (can_is_can_skb(skb) && cf->can_id & CAN_RTR_FLAG)
return 0;

return len;
}

#endif /* !_CAN_SKB_H */

0 comments on commit 467ef4c

Please sign in to comment.