1 /* 2 * Copyright (C) 2015-2017 Netronome Systems, Inc. 3 * 4 * This software is dual licensed under the GNU General License Version 2, 5 * June 1991 as shown in the file COPYING in the top-level directory of this 6 * source tree or the BSD 2-Clause License provided below. You have the 7 * option to license this software under the complete terms of either license. 8 * 9 * The BSD 2-Clause License: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * 1. Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #include <linux/bitfield.h> 35 #include <linux/netdevice.h> 36 #include <linux/skbuff.h> 37 #include <linux/workqueue.h> 38 #include <net/dst_metadata.h> 39 40 #include "main.h" 41 #include "../nfp_net.h" 42 #include "../nfp_net_repr.h" 43 #include "./cmsg.h" 44 45 static struct nfp_flower_cmsg_hdr * 46 nfp_flower_cmsg_get_hdr(struct sk_buff *skb) 47 { 48 return (struct nfp_flower_cmsg_hdr *)skb->data; 49 } 50 51 struct sk_buff * 52 nfp_flower_cmsg_alloc(struct nfp_app *app, unsigned int size, 53 enum nfp_flower_cmsg_type_port type, gfp_t flag) 54 { 55 struct nfp_flower_cmsg_hdr *ch; 56 struct sk_buff *skb; 57 58 size += NFP_FLOWER_CMSG_HLEN; 59 60 skb = nfp_app_ctrl_msg_alloc(app, size, flag); 61 if (!skb) 62 return NULL; 63 64 ch = nfp_flower_cmsg_get_hdr(skb); 65 ch->pad = 0; 66 ch->version = NFP_FLOWER_CMSG_VER1; 67 ch->type = type; 68 skb_put(skb, size); 69 70 return skb; 71 } 72 73 struct sk_buff * 74 nfp_flower_cmsg_mac_repr_start(struct nfp_app *app, unsigned int num_ports) 75 { 76 struct nfp_flower_cmsg_mac_repr *msg; 77 struct sk_buff *skb; 78 unsigned int size; 79 80 size = sizeof(*msg) + num_ports * sizeof(msg->ports[0]); 81 skb = nfp_flower_cmsg_alloc(app, size, NFP_FLOWER_CMSG_TYPE_MAC_REPR, 82 GFP_KERNEL); 83 if (!skb) 84 return NULL; 85 86 msg = nfp_flower_cmsg_get_data(skb); 87 memset(msg->reserved, 0, sizeof(msg->reserved)); 88 msg->num_ports = num_ports; 89 90 return skb; 91 } 92 93 void 94 nfp_flower_cmsg_mac_repr_add(struct sk_buff *skb, unsigned int idx, 95 unsigned int nbi, unsigned int nbi_port, 96 unsigned int phys_port) 97 { 98 struct nfp_flower_cmsg_mac_repr *msg; 99 100 msg = nfp_flower_cmsg_get_data(skb); 101 msg->ports[idx].idx = idx; 102 msg->ports[idx].info = nbi & NFP_FLOWER_CMSG_MAC_REPR_NBI; 103 msg->ports[idx].nbi_port = nbi_port; 104 msg->ports[idx].phys_port = phys_port; 105 } 106 107 int nfp_flower_cmsg_portmod(struct nfp_repr *repr, bool carrier_ok) 108 { 109 struct nfp_flower_cmsg_portmod *msg; 110 struct sk_buff *skb; 111 112 skb = nfp_flower_cmsg_alloc(repr->app, sizeof(*msg), 113 NFP_FLOWER_CMSG_TYPE_PORT_MOD, GFP_KERNEL); 114 if (!skb) 115 return -ENOMEM; 116 117 msg = nfp_flower_cmsg_get_data(skb); 118 msg->portnum = cpu_to_be32(repr->dst->u.port_info.port_id); 119 msg->reserved = 0; 120 msg->info = carrier_ok; 121 msg->mtu = cpu_to_be16(repr->netdev->mtu); 122 123 nfp_ctrl_tx(repr->app->ctrl, skb); 124 125 return 0; 126 } 127 128 static void 129 nfp_flower_cmsg_portmod_rx(struct nfp_app *app, struct sk_buff *skb) 130 { 131 struct nfp_flower_cmsg_portmod *msg; 132 struct net_device *netdev; 133 bool link; 134 135 msg = nfp_flower_cmsg_get_data(skb); 136 link = msg->info & NFP_FLOWER_CMSG_PORTMOD_INFO_LINK; 137 138 rtnl_lock(); 139 rcu_read_lock(); 140 netdev = nfp_app_repr_get(app, be32_to_cpu(msg->portnum)); 141 rcu_read_unlock(); 142 if (!netdev) { 143 nfp_flower_cmsg_warn(app, "ctrl msg for unknown port 0x%08x\n", 144 be32_to_cpu(msg->portnum)); 145 rtnl_unlock(); 146 return; 147 } 148 149 if (link) { 150 u16 mtu = be16_to_cpu(msg->mtu); 151 152 netif_carrier_on(netdev); 153 154 /* An MTU of 0 from the firmware should be ignored */ 155 if (mtu) 156 dev_set_mtu(netdev, mtu); 157 } else { 158 netif_carrier_off(netdev); 159 } 160 rtnl_unlock(); 161 } 162 163 static void 164 nfp_flower_cmsg_process_one_rx(struct nfp_app *app, struct sk_buff *skb) 165 { 166 struct nfp_flower_cmsg_hdr *cmsg_hdr; 167 enum nfp_flower_cmsg_type_port type; 168 169 cmsg_hdr = nfp_flower_cmsg_get_hdr(skb); 170 171 if (unlikely(cmsg_hdr->version != NFP_FLOWER_CMSG_VER1)) { 172 nfp_flower_cmsg_warn(app, "Cannot handle repr control version %u\n", 173 cmsg_hdr->version); 174 goto out; 175 } 176 177 type = cmsg_hdr->type; 178 switch (type) { 179 case NFP_FLOWER_CMSG_TYPE_PORT_MOD: 180 nfp_flower_cmsg_portmod_rx(app, skb); 181 break; 182 case NFP_FLOWER_CMSG_TYPE_FLOW_STATS: 183 nfp_flower_rx_flow_stats(app, skb); 184 break; 185 case NFP_FLOWER_CMSG_TYPE_NO_NEIGH: 186 nfp_tunnel_request_route(app, skb); 187 break; 188 case NFP_FLOWER_CMSG_TYPE_ACTIVE_TUNS: 189 nfp_tunnel_keep_alive(app, skb); 190 break; 191 case NFP_FLOWER_CMSG_TYPE_TUN_NEIGH: 192 /* Acks from the NFP that the route is added - ignore. */ 193 break; 194 default: 195 nfp_flower_cmsg_warn(app, "Cannot handle invalid repr control type %u\n", 196 type); 197 goto out; 198 } 199 200 dev_consume_skb_any(skb); 201 return; 202 out: 203 dev_kfree_skb_any(skb); 204 } 205 206 void nfp_flower_cmsg_process_rx(struct work_struct *work) 207 { 208 struct nfp_flower_priv *priv; 209 struct sk_buff *skb; 210 211 priv = container_of(work, struct nfp_flower_priv, cmsg_work); 212 213 while ((skb = skb_dequeue(&priv->cmsg_skbs))) 214 nfp_flower_cmsg_process_one_rx(priv->app, skb); 215 } 216 217 void nfp_flower_cmsg_rx(struct nfp_app *app, struct sk_buff *skb) 218 { 219 struct nfp_flower_priv *priv = app->priv; 220 221 skb_queue_tail(&priv->cmsg_skbs, skb); 222 schedule_work(&priv->cmsg_work); 223 } 224