/* Local echo of CAN messages * * CAN network devices *should* support a local echo functionality * (see Documentation/networking/can.rst). To test the handling of CAN * interfaces that do not support the local echo both driver types are * implemented. In the case that the driver does not support the echo * the IFF_ECHO remains clear in dev->flags. This causes the PF_CAN core * to perform the echo as a fallback solution.
*/ void can_flush_echo_skb(struct net_device *dev)
{ struct can_priv *priv = netdev_priv(dev); struct net_device_stats *stats = &dev->stats; int i;
for (i = 0; i < priv->echo_skb_max; i++) { if (priv->echo_skb[i]) {
kfree_skb(priv->echo_skb[i]);
priv->echo_skb[i] = NULL;
stats->tx_dropped++;
stats->tx_aborted_errors++;
}
}
}
/* Put the skb on the stack to be looped backed locally lateron * * The function is typically called in the start_xmit function * of the device driver. The driver must protect access to * priv->echo_skb, if necessary.
*/ int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev, unsignedint idx, unsignedint frame_len)
{ struct can_priv *priv = netdev_priv(dev);
if (idx >= priv->echo_skb_max) {
netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
__func__, idx, priv->echo_skb_max); return -EINVAL;
}
/* check flag whether this packet has to be looped back */ if (!(dev->flags & IFF_ECHO) ||
(skb->protocol != htons(ETH_P_CAN) &&
skb->protocol != htons(ETH_P_CANFD) &&
skb->protocol != htons(ETH_P_CANXL))) {
kfree_skb(skb); return 0;
}
if (!priv->echo_skb[idx]) {
skb = can_create_echo_skb(skb); if (!skb) return -ENOMEM;
/* make settings for echo to reduce code in irq context */
skb->ip_summed = CHECKSUM_UNNECESSARY;
skb->dev = dev;
/* save frame_len to reuse it when transmission is completed */
can_skb_prv(skb)->frame_len = frame_len;
if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
skb_tx_timestamp(skb);
/* save this skb for tx interrupt echo handling */
priv->echo_skb[idx] = skb;
} else { /* locking problem with netif_stop_queue() ?? */
netdev_err(dev, "%s: BUG! echo_skb %d is occupied!\n", __func__, idx);
kfree_skb(skb); return -EBUSY;
}
if (idx >= priv->echo_skb_max) {
netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
__func__, idx, priv->echo_skb_max); return NULL;
}
if (priv->echo_skb[idx]) { /* Using "struct canfd_frame::len" for the frame * length is supported on both CAN and CANFD frames.
*/ struct sk_buff *skb = priv->echo_skb[idx]; struct can_skb_priv *can_skb_priv = can_skb_prv(skb);
if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)
skb_tstamp_tx(skb, skb_hwtstamps(skb));
/* get the real payload length for netdev statistics */
*len_ptr = can_skb_get_data_len(skb);
if (frame_len_ptr)
*frame_len_ptr = can_skb_priv->frame_len;
/* Get the skb from the stack and loop it back locally * * The function is typically called when the TX done interrupt * is handled in the device driver. The driver must protect * access to priv->echo_skb, if necessary.
*/ unsignedint can_get_echo_skb(struct net_device *dev, unsignedint idx, unsignedint *frame_len_ptr)
{ struct sk_buff *skb; unsignedint len;
skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr); if (!skb) return 0;
skb_get(skb); if (netif_rx(skb) == NET_RX_SUCCESS)
dev_consume_skb_any(skb); else
dev_kfree_skb_any(skb);
/* Remove the skb from the stack and free it. * * The function is typically called when TX failed.
*/ void can_free_echo_skb(struct net_device *dev, unsignedint idx, unsignedint *frame_len_ptr)
{ struct can_priv *priv = netdev_priv(dev);
if (idx >= priv->echo_skb_max) {
netdev_err(dev, "%s: BUG! Trying to access can_priv::echo_skb out of bounds (%u/max %u)\n",
__func__, idx, priv->echo_skb_max); return;
}
/* fill common values for CAN sk_buffs */ staticvoid init_can_skb_reserve(struct sk_buff *skb)
{
skb->pkt_type = PACKET_BROADCAST;
skb->ip_summed = CHECKSUM_UNNECESSARY;
/* Check for outgoing skbs that have not been created by the CAN subsystem */ staticbool can_skb_headroom_valid(struct net_device *dev, struct sk_buff *skb)
{ /* af_packet creates a headroom of HH_DATA_MOD bytes which is fine */ if (WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct can_skb_priv))) returnfalse;
/* af_packet does not apply CAN skb specific settings */ if (skb->ip_summed == CHECKSUM_NONE) { /* init headroom */
can_skb_prv(skb)->ifindex = dev->ifindex;
can_skb_prv(skb)->skbcnt = 0;
skb->ip_summed = CHECKSUM_UNNECESSARY;
/* perform proper loopback on capable devices */ if (dev->flags & IFF_ECHO)
skb->pkt_type = PACKET_LOOPBACK; else
skb->pkt_type = PACKET_HOST;
/* Drop a given socketbuffer if it does not contain a valid CAN frame. */ bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb)
{ switch (ntohs(skb->protocol)) { case ETH_P_CAN: if (!can_is_can_skb(skb)) goto inval_skb; break;
case ETH_P_CANFD: if (!can_is_canfd_skb(skb)) goto inval_skb; break;
case ETH_P_CANXL: if (!can_is_canxl_skb(skb)) goto inval_skb; break;
default: goto inval_skb;
}
if (!can_skb_headroom_valid(dev, skb)) goto inval_skb;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.