datapath.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <linux/genetlink.h>
  49. #include <net/genetlink.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "flow_table.h"
  56. #include "flow_netlink.h"
  57. #include "vport-internal_dev.h"
  58. #include "vport-netdev.h"
  59. int ovs_net_id __read_mostly;
  60. static struct genl_family dp_packet_genl_family;
  61. static struct genl_family dp_flow_genl_family;
  62. static struct genl_family dp_datapath_genl_family;
  63. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  64. .name = OVS_FLOW_MCGROUP
  65. };
  66. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  67. .name = OVS_DATAPATH_MCGROUP
  68. };
  69. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  70. .name = OVS_VPORT_MCGROUP
  71. };
  72. /* Check if need to build a reply message.
  73. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  74. static bool ovs_must_notify(struct genl_info *info,
  75. const struct genl_multicast_group *grp)
  76. {
  77. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  78. netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
  79. }
  80. static void ovs_notify(struct genl_family *family,
  81. struct sk_buff *skb, struct genl_info *info)
  82. {
  83. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  84. 0, info->nlhdr, GFP_KERNEL);
  85. }
  86. /**
  87. * DOC: Locking:
  88. *
  89. * All writes e.g. Writes to device state (add/remove datapath, port, set
  90. * operations on vports, etc.), Writes to other state (flow table
  91. * modifications, set miscellaneous datapath parameters, etc.) are protected
  92. * by ovs_lock.
  93. *
  94. * Reads are protected by RCU.
  95. *
  96. * There are a few special cases (mostly stats) that have their own
  97. * synchronization but they nest under all of above and don't interact with
  98. * each other.
  99. *
  100. * The RTNL lock nests inside ovs_mutex.
  101. */
  102. static DEFINE_MUTEX(ovs_mutex);
  103. void ovs_lock(void)
  104. {
  105. mutex_lock(&ovs_mutex);
  106. }
  107. void ovs_unlock(void)
  108. {
  109. mutex_unlock(&ovs_mutex);
  110. }
  111. #ifdef CONFIG_LOCKDEP
  112. int lockdep_ovsl_is_held(void)
  113. {
  114. if (debug_locks)
  115. return lockdep_is_held(&ovs_mutex);
  116. else
  117. return 1;
  118. }
  119. #endif
  120. static struct vport *new_vport(const struct vport_parms *);
  121. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  122. const struct dp_upcall_info *);
  123. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  124. const struct dp_upcall_info *);
  125. /* Must be called with rcu_read_lock or ovs_mutex. */
  126. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  127. {
  128. struct datapath *dp = NULL;
  129. struct net_device *dev;
  130. rcu_read_lock();
  131. dev = dev_get_by_index_rcu(net, dp_ifindex);
  132. if (dev) {
  133. struct vport *vport = ovs_internal_dev_get_vport(dev);
  134. if (vport)
  135. dp = vport->dp;
  136. }
  137. rcu_read_unlock();
  138. return dp;
  139. }
  140. /* Must be called with rcu_read_lock or ovs_mutex. */
  141. static const char *ovs_dp_name(const struct datapath *dp)
  142. {
  143. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  144. return vport->ops->get_name(vport);
  145. }
  146. static int get_dpifindex(struct datapath *dp)
  147. {
  148. struct vport *local;
  149. int ifindex;
  150. rcu_read_lock();
  151. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  152. if (local)
  153. ifindex = netdev_vport_priv(local)->dev->ifindex;
  154. else
  155. ifindex = 0;
  156. rcu_read_unlock();
  157. return ifindex;
  158. }
  159. static void destroy_dp_rcu(struct rcu_head *rcu)
  160. {
  161. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  162. free_percpu(dp->stats_percpu);
  163. release_net(ovs_dp_get_net(dp));
  164. kfree(dp->ports);
  165. kfree(dp);
  166. }
  167. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  168. u16 port_no)
  169. {
  170. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  171. }
  172. /* Called with ovs_mutex or RCU read lock. */
  173. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  174. {
  175. struct vport *vport;
  176. struct hlist_head *head;
  177. head = vport_hash_bucket(dp, port_no);
  178. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  179. if (vport->port_no == port_no)
  180. return vport;
  181. }
  182. return NULL;
  183. }
  184. /* Called with ovs_mutex. */
  185. static struct vport *new_vport(const struct vport_parms *parms)
  186. {
  187. struct vport *vport;
  188. vport = ovs_vport_add(parms);
  189. if (!IS_ERR(vport)) {
  190. struct datapath *dp = parms->dp;
  191. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  192. hlist_add_head_rcu(&vport->dp_hash_node, head);
  193. }
  194. return vport;
  195. }
  196. void ovs_dp_detach_port(struct vport *p)
  197. {
  198. ASSERT_OVSL();
  199. /* First drop references to device. */
  200. hlist_del_rcu(&p->dp_hash_node);
  201. /* Then destroy it. */
  202. ovs_vport_del(p);
  203. }
  204. /* Must be called with rcu_read_lock. */
  205. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  206. {
  207. struct datapath *dp = p->dp;
  208. struct sw_flow *flow;
  209. struct dp_stats_percpu *stats;
  210. struct sw_flow_key key;
  211. u64 *stats_counter;
  212. u32 n_mask_hit;
  213. int error;
  214. stats = this_cpu_ptr(dp->stats_percpu);
  215. /* Extract flow from 'skb' into 'key'. */
  216. error = ovs_flow_extract(skb, p->port_no, &key);
  217. if (unlikely(error)) {
  218. kfree_skb(skb);
  219. return;
  220. }
  221. /* Look up flow. */
  222. flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
  223. if (unlikely(!flow)) {
  224. struct dp_upcall_info upcall;
  225. upcall.cmd = OVS_PACKET_CMD_MISS;
  226. upcall.key = &key;
  227. upcall.userdata = NULL;
  228. upcall.portid = p->upcall_portid;
  229. ovs_dp_upcall(dp, skb, &upcall);
  230. consume_skb(skb);
  231. stats_counter = &stats->n_missed;
  232. goto out;
  233. }
  234. OVS_CB(skb)->flow = flow;
  235. OVS_CB(skb)->pkt_key = &key;
  236. ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
  237. ovs_execute_actions(dp, skb);
  238. stats_counter = &stats->n_hit;
  239. out:
  240. /* Update datapath statistics. */
  241. u64_stats_update_begin(&stats->syncp);
  242. (*stats_counter)++;
  243. stats->n_mask_hit += n_mask_hit;
  244. u64_stats_update_end(&stats->syncp);
  245. }
  246. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  247. const struct dp_upcall_info *upcall_info)
  248. {
  249. struct dp_stats_percpu *stats;
  250. int err;
  251. if (upcall_info->portid == 0) {
  252. err = -ENOTCONN;
  253. goto err;
  254. }
  255. if (!skb_is_gso(skb))
  256. err = queue_userspace_packet(dp, skb, upcall_info);
  257. else
  258. err = queue_gso_packets(dp, skb, upcall_info);
  259. if (err)
  260. goto err;
  261. return 0;
  262. err:
  263. stats = this_cpu_ptr(dp->stats_percpu);
  264. u64_stats_update_begin(&stats->syncp);
  265. stats->n_lost++;
  266. u64_stats_update_end(&stats->syncp);
  267. return err;
  268. }
  269. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  270. const struct dp_upcall_info *upcall_info)
  271. {
  272. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  273. struct dp_upcall_info later_info;
  274. struct sw_flow_key later_key;
  275. struct sk_buff *segs, *nskb;
  276. int err;
  277. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  278. if (IS_ERR(segs))
  279. return PTR_ERR(segs);
  280. /* Queue all of the segments. */
  281. skb = segs;
  282. do {
  283. err = queue_userspace_packet(dp, skb, upcall_info);
  284. if (err)
  285. break;
  286. if (skb == segs && gso_type & SKB_GSO_UDP) {
  287. /* The initial flow key extracted by ovs_flow_extract()
  288. * in this case is for a first fragment, so we need to
  289. * properly mark later fragments.
  290. */
  291. later_key = *upcall_info->key;
  292. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  293. later_info = *upcall_info;
  294. later_info.key = &later_key;
  295. upcall_info = &later_info;
  296. }
  297. } while ((skb = skb->next));
  298. /* Free all of the segments. */
  299. skb = segs;
  300. do {
  301. nskb = skb->next;
  302. if (err)
  303. kfree_skb(skb);
  304. else
  305. consume_skb(skb);
  306. } while ((skb = nskb));
  307. return err;
  308. }
  309. static size_t key_attr_size(void)
  310. {
  311. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  312. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  313. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  314. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  315. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  316. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  317. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  318. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  319. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  320. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  321. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  322. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  323. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  324. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  325. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  326. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  327. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  328. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  329. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  330. }
  331. static size_t upcall_msg_size(const struct nlattr *userdata,
  332. unsigned int hdrlen)
  333. {
  334. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  335. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  336. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  337. /* OVS_PACKET_ATTR_USERDATA */
  338. if (userdata)
  339. size += NLA_ALIGN(userdata->nla_len);
  340. return size;
  341. }
  342. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  343. const struct dp_upcall_info *upcall_info)
  344. {
  345. struct ovs_header *upcall;
  346. struct sk_buff *nskb = NULL;
  347. struct sk_buff *user_skb; /* to be queued to userspace */
  348. struct nlattr *nla;
  349. struct genl_info info = {
  350. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  351. .snd_portid = upcall_info->portid,
  352. };
  353. size_t len;
  354. unsigned int hlen;
  355. int err, dp_ifindex;
  356. dp_ifindex = get_dpifindex(dp);
  357. if (!dp_ifindex)
  358. return -ENODEV;
  359. if (vlan_tx_tag_present(skb)) {
  360. nskb = skb_clone(skb, GFP_ATOMIC);
  361. if (!nskb)
  362. return -ENOMEM;
  363. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  364. if (!nskb)
  365. return -ENOMEM;
  366. nskb->vlan_tci = 0;
  367. skb = nskb;
  368. }
  369. if (nla_attr_size(skb->len) > USHRT_MAX) {
  370. err = -EFBIG;
  371. goto out;
  372. }
  373. /* Complete checksum if needed */
  374. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  375. (err = skb_checksum_help(skb)))
  376. goto out;
  377. /* Older versions of OVS user space enforce alignment of the last
  378. * Netlink attribute to NLA_ALIGNTO which would require extensive
  379. * padding logic. Only perform zerocopy if padding is not required.
  380. */
  381. if (dp->user_features & OVS_DP_F_UNALIGNED)
  382. hlen = skb_zerocopy_headlen(skb);
  383. else
  384. hlen = skb->len;
  385. len = upcall_msg_size(upcall_info->userdata, hlen);
  386. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  387. if (!user_skb) {
  388. err = -ENOMEM;
  389. goto out;
  390. }
  391. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  392. 0, upcall_info->cmd);
  393. upcall->dp_ifindex = dp_ifindex;
  394. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  395. ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  396. nla_nest_end(user_skb, nla);
  397. if (upcall_info->userdata)
  398. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  399. nla_len(upcall_info->userdata),
  400. nla_data(upcall_info->userdata));
  401. /* Only reserve room for attribute header, packet data is added
  402. * in skb_zerocopy() */
  403. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  404. err = -ENOBUFS;
  405. goto out;
  406. }
  407. nla->nla_len = nla_attr_size(skb->len);
  408. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  409. if (err)
  410. goto out;
  411. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  412. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  413. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  414. if (plen > 0)
  415. memset(skb_put(user_skb, plen), 0, plen);
  416. }
  417. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  418. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  419. out:
  420. if (err)
  421. skb_tx_error(skb);
  422. kfree_skb(nskb);
  423. return err;
  424. }
  425. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  426. {
  427. struct ovs_header *ovs_header = info->userhdr;
  428. struct nlattr **a = info->attrs;
  429. struct sw_flow_actions *acts;
  430. struct sk_buff *packet;
  431. struct sw_flow *flow;
  432. struct datapath *dp;
  433. struct ethhdr *eth;
  434. int len;
  435. int err;
  436. err = -EINVAL;
  437. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  438. !a[OVS_PACKET_ATTR_ACTIONS])
  439. goto err;
  440. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  441. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  442. err = -ENOMEM;
  443. if (!packet)
  444. goto err;
  445. skb_reserve(packet, NET_IP_ALIGN);
  446. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  447. skb_reset_mac_header(packet);
  448. eth = eth_hdr(packet);
  449. /* Normally, setting the skb 'protocol' field would be handled by a
  450. * call to eth_type_trans(), but it assumes there's a sending
  451. * device, which we may not have. */
  452. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  453. packet->protocol = eth->h_proto;
  454. else
  455. packet->protocol = htons(ETH_P_802_2);
  456. /* Build an sw_flow for sending this packet. */
  457. flow = ovs_flow_alloc();
  458. err = PTR_ERR(flow);
  459. if (IS_ERR(flow))
  460. goto err_kfree_skb;
  461. err = ovs_flow_extract(packet, -1, &flow->key);
  462. if (err)
  463. goto err_flow_free;
  464. err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
  465. if (err)
  466. goto err_flow_free;
  467. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  468. err = PTR_ERR(acts);
  469. if (IS_ERR(acts))
  470. goto err_flow_free;
  471. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  472. &flow->key, 0, &acts);
  473. rcu_assign_pointer(flow->sf_acts, acts);
  474. if (err)
  475. goto err_flow_free;
  476. OVS_CB(packet)->flow = flow;
  477. OVS_CB(packet)->pkt_key = &flow->key;
  478. packet->priority = flow->key.phy.priority;
  479. packet->mark = flow->key.phy.skb_mark;
  480. rcu_read_lock();
  481. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  482. err = -ENODEV;
  483. if (!dp)
  484. goto err_unlock;
  485. local_bh_disable();
  486. err = ovs_execute_actions(dp, packet);
  487. local_bh_enable();
  488. rcu_read_unlock();
  489. ovs_flow_free(flow, false);
  490. return err;
  491. err_unlock:
  492. rcu_read_unlock();
  493. err_flow_free:
  494. ovs_flow_free(flow, false);
  495. err_kfree_skb:
  496. kfree_skb(packet);
  497. err:
  498. return err;
  499. }
  500. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  501. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  502. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  503. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  504. };
  505. static const struct genl_ops dp_packet_genl_ops[] = {
  506. { .cmd = OVS_PACKET_CMD_EXECUTE,
  507. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  508. .policy = packet_policy,
  509. .doit = ovs_packet_cmd_execute
  510. }
  511. };
  512. static struct genl_family dp_packet_genl_family = {
  513. .id = GENL_ID_GENERATE,
  514. .hdrsize = sizeof(struct ovs_header),
  515. .name = OVS_PACKET_FAMILY,
  516. .version = OVS_PACKET_VERSION,
  517. .maxattr = OVS_PACKET_ATTR_MAX,
  518. .netnsok = true,
  519. .parallel_ops = true,
  520. .ops = dp_packet_genl_ops,
  521. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  522. };
  523. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  524. struct ovs_dp_megaflow_stats *mega_stats)
  525. {
  526. int i;
  527. memset(mega_stats, 0, sizeof(*mega_stats));
  528. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  529. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  530. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  531. for_each_possible_cpu(i) {
  532. const struct dp_stats_percpu *percpu_stats;
  533. struct dp_stats_percpu local_stats;
  534. unsigned int start;
  535. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  536. do {
  537. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  538. local_stats = *percpu_stats;
  539. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  540. stats->n_hit += local_stats.n_hit;
  541. stats->n_missed += local_stats.n_missed;
  542. stats->n_lost += local_stats.n_lost;
  543. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  544. }
  545. }
  546. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  547. {
  548. return NLMSG_ALIGN(sizeof(struct ovs_header))
  549. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  550. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  551. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  552. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  553. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  554. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  555. }
  556. /* Called with ovs_mutex or RCU read lock. */
  557. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  558. struct sk_buff *skb, u32 portid,
  559. u32 seq, u32 flags, u8 cmd)
  560. {
  561. const int skb_orig_len = skb->len;
  562. struct nlattr *start;
  563. struct ovs_flow_stats stats;
  564. __be16 tcp_flags;
  565. unsigned long used;
  566. struct ovs_header *ovs_header;
  567. struct nlattr *nla;
  568. int err;
  569. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  570. if (!ovs_header)
  571. return -EMSGSIZE;
  572. ovs_header->dp_ifindex = dp_ifindex;
  573. /* Fill flow key. */
  574. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  575. if (!nla)
  576. goto nla_put_failure;
  577. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  578. if (err)
  579. goto error;
  580. nla_nest_end(skb, nla);
  581. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  582. if (!nla)
  583. goto nla_put_failure;
  584. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  585. if (err)
  586. goto error;
  587. nla_nest_end(skb, nla);
  588. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  589. if (used &&
  590. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  591. goto nla_put_failure;
  592. if (stats.n_packets &&
  593. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  594. goto nla_put_failure;
  595. if ((u8)ntohs(tcp_flags) &&
  596. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  597. goto nla_put_failure;
  598. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  599. * this is the first flow to be dumped into 'skb'. This is unusual for
  600. * Netlink but individual action lists can be longer than
  601. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  602. * The userspace caller can always fetch the actions separately if it
  603. * really wants them. (Most userspace callers in fact don't care.)
  604. *
  605. * This can only fail for dump operations because the skb is always
  606. * properly sized for single flows.
  607. */
  608. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  609. if (start) {
  610. const struct sw_flow_actions *sf_acts;
  611. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  612. err = ovs_nla_put_actions(sf_acts->actions,
  613. sf_acts->actions_len, skb);
  614. if (!err)
  615. nla_nest_end(skb, start);
  616. else {
  617. if (skb_orig_len)
  618. goto error;
  619. nla_nest_cancel(skb, start);
  620. }
  621. } else if (skb_orig_len)
  622. goto nla_put_failure;
  623. return genlmsg_end(skb, ovs_header);
  624. nla_put_failure:
  625. err = -EMSGSIZE;
  626. error:
  627. genlmsg_cancel(skb, ovs_header);
  628. return err;
  629. }
  630. /* May not be called with RCU read lock. */
  631. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  632. struct genl_info *info,
  633. bool always)
  634. {
  635. struct sk_buff *skb;
  636. if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
  637. return NULL;
  638. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  639. if (!skb)
  640. return ERR_PTR(-ENOMEM);
  641. return skb;
  642. }
  643. /* Called with ovs_mutex. */
  644. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  645. int dp_ifindex,
  646. struct genl_info *info, u8 cmd,
  647. bool always)
  648. {
  649. struct sk_buff *skb;
  650. int retval;
  651. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  652. always);
  653. if (!skb || IS_ERR(skb))
  654. return skb;
  655. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  656. info->snd_portid, info->snd_seq, 0,
  657. cmd);
  658. BUG_ON(retval < 0);
  659. return skb;
  660. }
  661. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  662. {
  663. struct nlattr **a = info->attrs;
  664. struct ovs_header *ovs_header = info->userhdr;
  665. struct sw_flow *flow, *new_flow;
  666. struct sw_flow_mask mask;
  667. struct sk_buff *reply;
  668. struct datapath *dp;
  669. struct sw_flow_actions *acts;
  670. struct sw_flow_match match;
  671. int error;
  672. /* Must have key and actions. */
  673. error = -EINVAL;
  674. if (!a[OVS_FLOW_ATTR_KEY])
  675. goto error;
  676. if (!a[OVS_FLOW_ATTR_ACTIONS])
  677. goto error;
  678. /* Most of the time we need to allocate a new flow, do it before
  679. * locking.
  680. */
  681. new_flow = ovs_flow_alloc();
  682. if (IS_ERR(new_flow)) {
  683. error = PTR_ERR(new_flow);
  684. goto error;
  685. }
  686. /* Extract key. */
  687. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  688. error = ovs_nla_get_match(&match,
  689. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  690. if (error)
  691. goto err_kfree_flow;
  692. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  693. /* Validate actions. */
  694. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  695. error = PTR_ERR(acts);
  696. if (IS_ERR(acts))
  697. goto err_kfree_flow;
  698. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  699. 0, &acts);
  700. if (error) {
  701. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  702. goto err_kfree_acts;
  703. }
  704. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  705. if (IS_ERR(reply)) {
  706. error = PTR_ERR(reply);
  707. goto err_kfree_acts;
  708. }
  709. ovs_lock();
  710. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  711. if (unlikely(!dp)) {
  712. error = -ENODEV;
  713. goto err_unlock_ovs;
  714. }
  715. /* Check if this is a duplicate flow */
  716. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  717. if (likely(!flow)) {
  718. rcu_assign_pointer(new_flow->sf_acts, acts);
  719. /* Put flow in bucket. */
  720. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  721. if (unlikely(error)) {
  722. acts = NULL;
  723. goto err_unlock_ovs;
  724. }
  725. if (unlikely(reply)) {
  726. error = ovs_flow_cmd_fill_info(new_flow,
  727. ovs_header->dp_ifindex,
  728. reply, info->snd_portid,
  729. info->snd_seq, 0,
  730. OVS_FLOW_CMD_NEW);
  731. BUG_ON(error < 0);
  732. }
  733. ovs_unlock();
  734. } else {
  735. struct sw_flow_actions *old_acts;
  736. /* Bail out if we're not allowed to modify an existing flow.
  737. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  738. * because Generic Netlink treats the latter as a dump
  739. * request. We also accept NLM_F_EXCL in case that bug ever
  740. * gets fixed.
  741. */
  742. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  743. | NLM_F_EXCL))) {
  744. error = -EEXIST;
  745. goto err_unlock_ovs;
  746. }
  747. /* The unmasked key has to be the same for flow updates. */
  748. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  749. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  750. if (!flow) {
  751. error = -ENOENT;
  752. goto err_unlock_ovs;
  753. }
  754. }
  755. /* Update actions. */
  756. old_acts = ovsl_dereference(flow->sf_acts);
  757. rcu_assign_pointer(flow->sf_acts, acts);
  758. if (unlikely(reply)) {
  759. error = ovs_flow_cmd_fill_info(flow,
  760. ovs_header->dp_ifindex,
  761. reply, info->snd_portid,
  762. info->snd_seq, 0,
  763. OVS_FLOW_CMD_NEW);
  764. BUG_ON(error < 0);
  765. }
  766. ovs_unlock();
  767. ovs_nla_free_flow_actions(old_acts);
  768. ovs_flow_free(new_flow, false);
  769. }
  770. if (reply)
  771. ovs_notify(&dp_flow_genl_family, reply, info);
  772. return 0;
  773. err_unlock_ovs:
  774. ovs_unlock();
  775. kfree_skb(reply);
  776. err_kfree_acts:
  777. kfree(acts);
  778. err_kfree_flow:
  779. ovs_flow_free(new_flow, false);
  780. error:
  781. return error;
  782. }
  783. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  784. {
  785. struct nlattr **a = info->attrs;
  786. struct ovs_header *ovs_header = info->userhdr;
  787. struct sw_flow_key key, masked_key;
  788. struct sw_flow *flow;
  789. struct sw_flow_mask mask;
  790. struct sk_buff *reply = NULL;
  791. struct datapath *dp;
  792. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  793. struct sw_flow_match match;
  794. int error;
  795. /* Extract key. */
  796. error = -EINVAL;
  797. if (!a[OVS_FLOW_ATTR_KEY])
  798. goto error;
  799. ovs_match_init(&match, &key, &mask);
  800. error = ovs_nla_get_match(&match,
  801. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  802. if (error)
  803. goto error;
  804. /* Validate actions. */
  805. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  806. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  807. error = PTR_ERR(acts);
  808. if (IS_ERR(acts))
  809. goto error;
  810. ovs_flow_mask_key(&masked_key, &key, &mask);
  811. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  812. &masked_key, 0, &acts);
  813. if (error) {
  814. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  815. goto err_kfree_acts;
  816. }
  817. }
  818. /* Can allocate before locking if have acts. */
  819. if (acts) {
  820. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  821. if (IS_ERR(reply)) {
  822. error = PTR_ERR(reply);
  823. goto err_kfree_acts;
  824. }
  825. }
  826. ovs_lock();
  827. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  828. if (unlikely(!dp)) {
  829. error = -ENODEV;
  830. goto err_unlock_ovs;
  831. }
  832. /* Check that the flow exists. */
  833. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  834. if (unlikely(!flow)) {
  835. error = -ENOENT;
  836. goto err_unlock_ovs;
  837. }
  838. /* Update actions, if present. */
  839. if (likely(acts)) {
  840. old_acts = ovsl_dereference(flow->sf_acts);
  841. rcu_assign_pointer(flow->sf_acts, acts);
  842. if (unlikely(reply)) {
  843. error = ovs_flow_cmd_fill_info(flow,
  844. ovs_header->dp_ifindex,
  845. reply, info->snd_portid,
  846. info->snd_seq, 0,
  847. OVS_FLOW_CMD_NEW);
  848. BUG_ON(error < 0);
  849. }
  850. } else {
  851. /* Could not alloc without acts before locking. */
  852. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  853. info, OVS_FLOW_CMD_NEW, false);
  854. if (unlikely(IS_ERR(reply))) {
  855. error = PTR_ERR(reply);
  856. goto err_unlock_ovs;
  857. }
  858. }
  859. /* Clear stats. */
  860. if (a[OVS_FLOW_ATTR_CLEAR])
  861. ovs_flow_stats_clear(flow);
  862. ovs_unlock();
  863. if (reply)
  864. ovs_notify(&dp_flow_genl_family, reply, info);
  865. if (old_acts)
  866. ovs_nla_free_flow_actions(old_acts);
  867. return 0;
  868. err_unlock_ovs:
  869. ovs_unlock();
  870. kfree_skb(reply);
  871. err_kfree_acts:
  872. kfree(acts);
  873. error:
  874. return error;
  875. }
  876. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  877. {
  878. struct nlattr **a = info->attrs;
  879. struct ovs_header *ovs_header = info->userhdr;
  880. struct sw_flow_key key;
  881. struct sk_buff *reply;
  882. struct sw_flow *flow;
  883. struct datapath *dp;
  884. struct sw_flow_match match;
  885. int err;
  886. if (!a[OVS_FLOW_ATTR_KEY]) {
  887. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  888. return -EINVAL;
  889. }
  890. ovs_match_init(&match, &key, NULL);
  891. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  892. if (err)
  893. return err;
  894. ovs_lock();
  895. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  896. if (!dp) {
  897. err = -ENODEV;
  898. goto unlock;
  899. }
  900. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  901. if (!flow) {
  902. err = -ENOENT;
  903. goto unlock;
  904. }
  905. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  906. OVS_FLOW_CMD_NEW, true);
  907. if (IS_ERR(reply)) {
  908. err = PTR_ERR(reply);
  909. goto unlock;
  910. }
  911. ovs_unlock();
  912. return genlmsg_reply(reply, info);
  913. unlock:
  914. ovs_unlock();
  915. return err;
  916. }
  917. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  918. {
  919. struct nlattr **a = info->attrs;
  920. struct ovs_header *ovs_header = info->userhdr;
  921. struct sw_flow_key key;
  922. struct sk_buff *reply;
  923. struct sw_flow *flow;
  924. struct datapath *dp;
  925. struct sw_flow_match match;
  926. int err;
  927. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  928. ovs_match_init(&match, &key, NULL);
  929. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  930. if (unlikely(err))
  931. return err;
  932. }
  933. ovs_lock();
  934. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  935. if (unlikely(!dp)) {
  936. err = -ENODEV;
  937. goto unlock;
  938. }
  939. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  940. err = ovs_flow_tbl_flush(&dp->table);
  941. goto unlock;
  942. }
  943. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  944. if (unlikely(!flow)) {
  945. err = -ENOENT;
  946. goto unlock;
  947. }
  948. ovs_flow_tbl_remove(&dp->table, flow);
  949. ovs_unlock();
  950. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  951. info, false);
  952. if (likely(reply)) {
  953. if (likely(!IS_ERR(reply))) {
  954. rcu_read_lock(); /*To keep RCU checker happy. */
  955. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  956. reply, info->snd_portid,
  957. info->snd_seq, 0,
  958. OVS_FLOW_CMD_DEL);
  959. rcu_read_unlock();
  960. BUG_ON(err < 0);
  961. ovs_notify(&dp_flow_genl_family, reply, info);
  962. } else {
  963. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  964. }
  965. }
  966. ovs_flow_free(flow, true);
  967. return 0;
  968. unlock:
  969. ovs_unlock();
  970. return err;
  971. }
  972. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  973. {
  974. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  975. struct table_instance *ti;
  976. struct datapath *dp;
  977. rcu_read_lock();
  978. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  979. if (!dp) {
  980. rcu_read_unlock();
  981. return -ENODEV;
  982. }
  983. ti = rcu_dereference(dp->table.ti);
  984. for (;;) {
  985. struct sw_flow *flow;
  986. u32 bucket, obj;
  987. bucket = cb->args[0];
  988. obj = cb->args[1];
  989. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  990. if (!flow)
  991. break;
  992. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  993. NETLINK_CB(cb->skb).portid,
  994. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  995. OVS_FLOW_CMD_NEW) < 0)
  996. break;
  997. cb->args[0] = bucket;
  998. cb->args[1] = obj;
  999. }
  1000. rcu_read_unlock();
  1001. return skb->len;
  1002. }
  1003. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1004. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1005. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1006. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1007. };
  1008. static struct genl_ops dp_flow_genl_ops[] = {
  1009. { .cmd = OVS_FLOW_CMD_NEW,
  1010. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1011. .policy = flow_policy,
  1012. .doit = ovs_flow_cmd_new
  1013. },
  1014. { .cmd = OVS_FLOW_CMD_DEL,
  1015. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1016. .policy = flow_policy,
  1017. .doit = ovs_flow_cmd_del
  1018. },
  1019. { .cmd = OVS_FLOW_CMD_GET,
  1020. .flags = 0, /* OK for unprivileged users. */
  1021. .policy = flow_policy,
  1022. .doit = ovs_flow_cmd_get,
  1023. .dumpit = ovs_flow_cmd_dump
  1024. },
  1025. { .cmd = OVS_FLOW_CMD_SET,
  1026. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1027. .policy = flow_policy,
  1028. .doit = ovs_flow_cmd_set,
  1029. },
  1030. };
  1031. static struct genl_family dp_flow_genl_family = {
  1032. .id = GENL_ID_GENERATE,
  1033. .hdrsize = sizeof(struct ovs_header),
  1034. .name = OVS_FLOW_FAMILY,
  1035. .version = OVS_FLOW_VERSION,
  1036. .maxattr = OVS_FLOW_ATTR_MAX,
  1037. .netnsok = true,
  1038. .parallel_ops = true,
  1039. .ops = dp_flow_genl_ops,
  1040. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1041. .mcgrps = &ovs_dp_flow_multicast_group,
  1042. .n_mcgrps = 1,
  1043. };
  1044. static size_t ovs_dp_cmd_msg_size(void)
  1045. {
  1046. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1047. msgsize += nla_total_size(IFNAMSIZ);
  1048. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1049. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1050. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1051. return msgsize;
  1052. }
  1053. /* Called with ovs_mutex or RCU read lock. */
  1054. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1055. u32 portid, u32 seq, u32 flags, u8 cmd)
  1056. {
  1057. struct ovs_header *ovs_header;
  1058. struct ovs_dp_stats dp_stats;
  1059. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1060. int err;
  1061. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1062. flags, cmd);
  1063. if (!ovs_header)
  1064. goto error;
  1065. ovs_header->dp_ifindex = get_dpifindex(dp);
  1066. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1067. if (err)
  1068. goto nla_put_failure;
  1069. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1070. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1071. &dp_stats))
  1072. goto nla_put_failure;
  1073. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1074. sizeof(struct ovs_dp_megaflow_stats),
  1075. &dp_megaflow_stats))
  1076. goto nla_put_failure;
  1077. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1078. goto nla_put_failure;
  1079. return genlmsg_end(skb, ovs_header);
  1080. nla_put_failure:
  1081. genlmsg_cancel(skb, ovs_header);
  1082. error:
  1083. return -EMSGSIZE;
  1084. }
  1085. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1086. {
  1087. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1088. }
  1089. /* Called with rcu_read_lock or ovs_mutex. */
  1090. static struct datapath *lookup_datapath(struct net *net,
  1091. struct ovs_header *ovs_header,
  1092. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1093. {
  1094. struct datapath *dp;
  1095. if (!a[OVS_DP_ATTR_NAME])
  1096. dp = get_dp(net, ovs_header->dp_ifindex);
  1097. else {
  1098. struct vport *vport;
  1099. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1100. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1101. }
  1102. return dp ? dp : ERR_PTR(-ENODEV);
  1103. }
  1104. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1105. {
  1106. struct datapath *dp;
  1107. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1108. if (IS_ERR(dp))
  1109. return;
  1110. WARN(dp->user_features, "Dropping previously announced user features\n");
  1111. dp->user_features = 0;
  1112. }
  1113. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  1114. {
  1115. if (a[OVS_DP_ATTR_USER_FEATURES])
  1116. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1117. }
  1118. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1119. {
  1120. struct nlattr **a = info->attrs;
  1121. struct vport_parms parms;
  1122. struct sk_buff *reply;
  1123. struct datapath *dp;
  1124. struct vport *vport;
  1125. struct ovs_net *ovs_net;
  1126. int err, i;
  1127. err = -EINVAL;
  1128. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1129. goto err;
  1130. reply = ovs_dp_cmd_alloc_info(info);
  1131. if (!reply)
  1132. return -ENOMEM;
  1133. err = -ENOMEM;
  1134. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1135. if (dp == NULL)
  1136. goto err_free_reply;
  1137. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1138. /* Allocate table. */
  1139. err = ovs_flow_tbl_init(&dp->table);
  1140. if (err)
  1141. goto err_free_dp;
  1142. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1143. if (!dp->stats_percpu) {
  1144. err = -ENOMEM;
  1145. goto err_destroy_table;
  1146. }
  1147. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1148. GFP_KERNEL);
  1149. if (!dp->ports) {
  1150. err = -ENOMEM;
  1151. goto err_destroy_percpu;
  1152. }
  1153. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1154. INIT_HLIST_HEAD(&dp->ports[i]);
  1155. /* Set up our datapath device. */
  1156. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1157. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1158. parms.options = NULL;
  1159. parms.dp = dp;
  1160. parms.port_no = OVSP_LOCAL;
  1161. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1162. ovs_dp_change(dp, a);
  1163. /* So far only local changes have been made, now need the lock. */
  1164. ovs_lock();
  1165. vport = new_vport(&parms);
  1166. if (IS_ERR(vport)) {
  1167. err = PTR_ERR(vport);
  1168. if (err == -EBUSY)
  1169. err = -EEXIST;
  1170. if (err == -EEXIST) {
  1171. /* An outdated user space instance that does not understand
  1172. * the concept of user_features has attempted to create a new
  1173. * datapath and is likely to reuse it. Drop all user features.
  1174. */
  1175. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1176. ovs_dp_reset_user_features(skb, info);
  1177. }
  1178. goto err_destroy_ports_array;
  1179. }
  1180. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1181. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1182. BUG_ON(err < 0);
  1183. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1184. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1185. ovs_unlock();
  1186. ovs_notify(&dp_datapath_genl_family, reply, info);
  1187. return 0;
  1188. err_destroy_ports_array:
  1189. ovs_unlock();
  1190. kfree(dp->ports);
  1191. err_destroy_percpu:
  1192. free_percpu(dp->stats_percpu);
  1193. err_destroy_table:
  1194. ovs_flow_tbl_destroy(&dp->table, false);
  1195. err_free_dp:
  1196. release_net(ovs_dp_get_net(dp));
  1197. kfree(dp);
  1198. err_free_reply:
  1199. kfree_skb(reply);
  1200. err:
  1201. return err;
  1202. }
  1203. /* Called with ovs_mutex. */
  1204. static void __dp_destroy(struct datapath *dp)
  1205. {
  1206. int i;
  1207. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1208. struct vport *vport;
  1209. struct hlist_node *n;
  1210. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1211. if (vport->port_no != OVSP_LOCAL)
  1212. ovs_dp_detach_port(vport);
  1213. }
  1214. list_del_rcu(&dp->list_node);
  1215. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1216. * all ports in datapath are destroyed first before freeing datapath.
  1217. */
  1218. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1219. /* RCU destroy the flow table */
  1220. ovs_flow_tbl_destroy(&dp->table, true);
  1221. call_rcu(&dp->rcu, destroy_dp_rcu);
  1222. }
  1223. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1224. {
  1225. struct sk_buff *reply;
  1226. struct datapath *dp;
  1227. int err;
  1228. reply = ovs_dp_cmd_alloc_info(info);
  1229. if (!reply)
  1230. return -ENOMEM;
  1231. ovs_lock();
  1232. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1233. err = PTR_ERR(dp);
  1234. if (IS_ERR(dp))
  1235. goto err_unlock_free;
  1236. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1237. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1238. BUG_ON(err < 0);
  1239. __dp_destroy(dp);
  1240. ovs_unlock();
  1241. ovs_notify(&dp_datapath_genl_family, reply, info);
  1242. return 0;
  1243. err_unlock_free:
  1244. ovs_unlock();
  1245. kfree_skb(reply);
  1246. return err;
  1247. }
  1248. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1249. {
  1250. struct sk_buff *reply;
  1251. struct datapath *dp;
  1252. int err;
  1253. reply = ovs_dp_cmd_alloc_info(info);
  1254. if (!reply)
  1255. return -ENOMEM;
  1256. ovs_lock();
  1257. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1258. err = PTR_ERR(dp);
  1259. if (IS_ERR(dp))
  1260. goto err_unlock_free;
  1261. ovs_dp_change(dp, info->attrs);
  1262. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1263. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1264. BUG_ON(err < 0);
  1265. ovs_unlock();
  1266. ovs_notify(&dp_datapath_genl_family, reply, info);
  1267. return 0;
  1268. err_unlock_free:
  1269. ovs_unlock();
  1270. kfree_skb(reply);
  1271. return err;
  1272. }
  1273. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1274. {
  1275. struct sk_buff *reply;
  1276. struct datapath *dp;
  1277. int err;
  1278. reply = ovs_dp_cmd_alloc_info(info);
  1279. if (!reply)
  1280. return -ENOMEM;
  1281. rcu_read_lock();
  1282. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1283. if (IS_ERR(dp)) {
  1284. err = PTR_ERR(dp);
  1285. goto err_unlock_free;
  1286. }
  1287. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1288. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1289. BUG_ON(err < 0);
  1290. rcu_read_unlock();
  1291. return genlmsg_reply(reply, info);
  1292. err_unlock_free:
  1293. rcu_read_unlock();
  1294. kfree_skb(reply);
  1295. return err;
  1296. }
  1297. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1298. {
  1299. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1300. struct datapath *dp;
  1301. int skip = cb->args[0];
  1302. int i = 0;
  1303. rcu_read_lock();
  1304. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1305. if (i >= skip &&
  1306. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1307. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1308. OVS_DP_CMD_NEW) < 0)
  1309. break;
  1310. i++;
  1311. }
  1312. rcu_read_unlock();
  1313. cb->args[0] = i;
  1314. return skb->len;
  1315. }
  1316. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1317. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1318. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1319. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1320. };
  1321. static struct genl_ops dp_datapath_genl_ops[] = {
  1322. { .cmd = OVS_DP_CMD_NEW,
  1323. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1324. .policy = datapath_policy,
  1325. .doit = ovs_dp_cmd_new
  1326. },
  1327. { .cmd = OVS_DP_CMD_DEL,
  1328. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1329. .policy = datapath_policy,
  1330. .doit = ovs_dp_cmd_del
  1331. },
  1332. { .cmd = OVS_DP_CMD_GET,
  1333. .flags = 0, /* OK for unprivileged users. */
  1334. .policy = datapath_policy,
  1335. .doit = ovs_dp_cmd_get,
  1336. .dumpit = ovs_dp_cmd_dump
  1337. },
  1338. { .cmd = OVS_DP_CMD_SET,
  1339. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1340. .policy = datapath_policy,
  1341. .doit = ovs_dp_cmd_set,
  1342. },
  1343. };
  1344. static struct genl_family dp_datapath_genl_family = {
  1345. .id = GENL_ID_GENERATE,
  1346. .hdrsize = sizeof(struct ovs_header),
  1347. .name = OVS_DATAPATH_FAMILY,
  1348. .version = OVS_DATAPATH_VERSION,
  1349. .maxattr = OVS_DP_ATTR_MAX,
  1350. .netnsok = true,
  1351. .parallel_ops = true,
  1352. .ops = dp_datapath_genl_ops,
  1353. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1354. .mcgrps = &ovs_dp_datapath_multicast_group,
  1355. .n_mcgrps = 1,
  1356. };
  1357. /* Called with ovs_mutex or RCU read lock. */
  1358. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1359. u32 portid, u32 seq, u32 flags, u8 cmd)
  1360. {
  1361. struct ovs_header *ovs_header;
  1362. struct ovs_vport_stats vport_stats;
  1363. int err;
  1364. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1365. flags, cmd);
  1366. if (!ovs_header)
  1367. return -EMSGSIZE;
  1368. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1369. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1370. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1371. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1372. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1373. goto nla_put_failure;
  1374. ovs_vport_get_stats(vport, &vport_stats);
  1375. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1376. &vport_stats))
  1377. goto nla_put_failure;
  1378. err = ovs_vport_get_options(vport, skb);
  1379. if (err == -EMSGSIZE)
  1380. goto error;
  1381. return genlmsg_end(skb, ovs_header);
  1382. nla_put_failure:
  1383. err = -EMSGSIZE;
  1384. error:
  1385. genlmsg_cancel(skb, ovs_header);
  1386. return err;
  1387. }
  1388. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1389. {
  1390. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1391. }
  1392. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1393. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1394. u32 seq, u8 cmd)
  1395. {
  1396. struct sk_buff *skb;
  1397. int retval;
  1398. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1399. if (!skb)
  1400. return ERR_PTR(-ENOMEM);
  1401. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1402. BUG_ON(retval < 0);
  1403. return skb;
  1404. }
  1405. /* Called with ovs_mutex or RCU read lock. */
  1406. static struct vport *lookup_vport(struct net *net,
  1407. struct ovs_header *ovs_header,
  1408. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1409. {
  1410. struct datapath *dp;
  1411. struct vport *vport;
  1412. if (a[OVS_VPORT_ATTR_NAME]) {
  1413. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1414. if (!vport)
  1415. return ERR_PTR(-ENODEV);
  1416. if (ovs_header->dp_ifindex &&
  1417. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1418. return ERR_PTR(-ENODEV);
  1419. return vport;
  1420. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1421. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1422. if (port_no >= DP_MAX_PORTS)
  1423. return ERR_PTR(-EFBIG);
  1424. dp = get_dp(net, ovs_header->dp_ifindex);
  1425. if (!dp)
  1426. return ERR_PTR(-ENODEV);
  1427. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1428. if (!vport)
  1429. return ERR_PTR(-ENODEV);
  1430. return vport;
  1431. } else
  1432. return ERR_PTR(-EINVAL);
  1433. }
  1434. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1435. {
  1436. struct nlattr **a = info->attrs;
  1437. struct ovs_header *ovs_header = info->userhdr;
  1438. struct vport_parms parms;
  1439. struct sk_buff *reply;
  1440. struct vport *vport;
  1441. struct datapath *dp;
  1442. u32 port_no;
  1443. int err;
  1444. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1445. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1446. return -EINVAL;
  1447. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1448. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1449. if (port_no >= DP_MAX_PORTS)
  1450. return -EFBIG;
  1451. reply = ovs_vport_cmd_alloc_info();
  1452. if (!reply)
  1453. return -ENOMEM;
  1454. ovs_lock();
  1455. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1456. err = -ENODEV;
  1457. if (!dp)
  1458. goto exit_unlock_free;
  1459. if (port_no) {
  1460. vport = ovs_vport_ovsl(dp, port_no);
  1461. err = -EBUSY;
  1462. if (vport)
  1463. goto exit_unlock_free;
  1464. } else {
  1465. for (port_no = 1; ; port_no++) {
  1466. if (port_no >= DP_MAX_PORTS) {
  1467. err = -EFBIG;
  1468. goto exit_unlock_free;
  1469. }
  1470. vport = ovs_vport_ovsl(dp, port_no);
  1471. if (!vport)
  1472. break;
  1473. }
  1474. }
  1475. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1476. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1477. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1478. parms.dp = dp;
  1479. parms.port_no = port_no;
  1480. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1481. vport = new_vport(&parms);
  1482. err = PTR_ERR(vport);
  1483. if (IS_ERR(vport))
  1484. goto exit_unlock_free;
  1485. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1486. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1487. BUG_ON(err < 0);
  1488. ovs_unlock();
  1489. ovs_notify(&dp_vport_genl_family, reply, info);
  1490. return 0;
  1491. exit_unlock_free:
  1492. ovs_unlock();
  1493. kfree_skb(reply);
  1494. return err;
  1495. }
  1496. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1497. {
  1498. struct nlattr **a = info->attrs;
  1499. struct sk_buff *reply;
  1500. struct vport *vport;
  1501. int err;
  1502. reply = ovs_vport_cmd_alloc_info();
  1503. if (!reply)
  1504. return -ENOMEM;
  1505. ovs_lock();
  1506. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1507. err = PTR_ERR(vport);
  1508. if (IS_ERR(vport))
  1509. goto exit_unlock_free;
  1510. if (a[OVS_VPORT_ATTR_TYPE] &&
  1511. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1512. err = -EINVAL;
  1513. goto exit_unlock_free;
  1514. }
  1515. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1516. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1517. if (err)
  1518. goto exit_unlock_free;
  1519. }
  1520. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1521. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1522. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1523. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1524. BUG_ON(err < 0);
  1525. ovs_unlock();
  1526. ovs_notify(&dp_vport_genl_family, reply, info);
  1527. return 0;
  1528. exit_unlock_free:
  1529. ovs_unlock();
  1530. kfree_skb(reply);
  1531. return err;
  1532. }
  1533. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1534. {
  1535. struct nlattr **a = info->attrs;
  1536. struct sk_buff *reply;
  1537. struct vport *vport;
  1538. int err;
  1539. reply = ovs_vport_cmd_alloc_info();
  1540. if (!reply)
  1541. return -ENOMEM;
  1542. ovs_lock();
  1543. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1544. err = PTR_ERR(vport);
  1545. if (IS_ERR(vport))
  1546. goto exit_unlock_free;
  1547. if (vport->port_no == OVSP_LOCAL) {
  1548. err = -EINVAL;
  1549. goto exit_unlock_free;
  1550. }
  1551. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1552. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1553. BUG_ON(err < 0);
  1554. ovs_dp_detach_port(vport);
  1555. ovs_unlock();
  1556. ovs_notify(&dp_vport_genl_family, reply, info);
  1557. return 0;
  1558. exit_unlock_free:
  1559. ovs_unlock();
  1560. kfree_skb(reply);
  1561. return err;
  1562. }
  1563. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1564. {
  1565. struct nlattr **a = info->attrs;
  1566. struct ovs_header *ovs_header = info->userhdr;
  1567. struct sk_buff *reply;
  1568. struct vport *vport;
  1569. int err;
  1570. reply = ovs_vport_cmd_alloc_info();
  1571. if (!reply)
  1572. return -ENOMEM;
  1573. rcu_read_lock();
  1574. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1575. err = PTR_ERR(vport);
  1576. if (IS_ERR(vport))
  1577. goto exit_unlock_free;
  1578. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1579. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1580. BUG_ON(err < 0);
  1581. rcu_read_unlock();
  1582. return genlmsg_reply(reply, info);
  1583. exit_unlock_free:
  1584. rcu_read_unlock();
  1585. kfree_skb(reply);
  1586. return err;
  1587. }
  1588. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1589. {
  1590. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1591. struct datapath *dp;
  1592. int bucket = cb->args[0], skip = cb->args[1];
  1593. int i, j = 0;
  1594. rcu_read_lock();
  1595. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1596. if (!dp) {
  1597. rcu_read_unlock();
  1598. return -ENODEV;
  1599. }
  1600. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1601. struct vport *vport;
  1602. j = 0;
  1603. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1604. if (j >= skip &&
  1605. ovs_vport_cmd_fill_info(vport, skb,
  1606. NETLINK_CB(cb->skb).portid,
  1607. cb->nlh->nlmsg_seq,
  1608. NLM_F_MULTI,
  1609. OVS_VPORT_CMD_NEW) < 0)
  1610. goto out;
  1611. j++;
  1612. }
  1613. skip = 0;
  1614. }
  1615. out:
  1616. rcu_read_unlock();
  1617. cb->args[0] = i;
  1618. cb->args[1] = j;
  1619. return skb->len;
  1620. }
  1621. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1622. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1623. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1624. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1625. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1626. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1627. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1628. };
  1629. static struct genl_ops dp_vport_genl_ops[] = {
  1630. { .cmd = OVS_VPORT_CMD_NEW,
  1631. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1632. .policy = vport_policy,
  1633. .doit = ovs_vport_cmd_new
  1634. },
  1635. { .cmd = OVS_VPORT_CMD_DEL,
  1636. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1637. .policy = vport_policy,
  1638. .doit = ovs_vport_cmd_del
  1639. },
  1640. { .cmd = OVS_VPORT_CMD_GET,
  1641. .flags = 0, /* OK for unprivileged users. */
  1642. .policy = vport_policy,
  1643. .doit = ovs_vport_cmd_get,
  1644. .dumpit = ovs_vport_cmd_dump
  1645. },
  1646. { .cmd = OVS_VPORT_CMD_SET,
  1647. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1648. .policy = vport_policy,
  1649. .doit = ovs_vport_cmd_set,
  1650. },
  1651. };
  1652. struct genl_family dp_vport_genl_family = {
  1653. .id = GENL_ID_GENERATE,
  1654. .hdrsize = sizeof(struct ovs_header),
  1655. .name = OVS_VPORT_FAMILY,
  1656. .version = OVS_VPORT_VERSION,
  1657. .maxattr = OVS_VPORT_ATTR_MAX,
  1658. .netnsok = true,
  1659. .parallel_ops = true,
  1660. .ops = dp_vport_genl_ops,
  1661. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1662. .mcgrps = &ovs_dp_vport_multicast_group,
  1663. .n_mcgrps = 1,
  1664. };
  1665. static struct genl_family * const dp_genl_families[] = {
  1666. &dp_datapath_genl_family,
  1667. &dp_vport_genl_family,
  1668. &dp_flow_genl_family,
  1669. &dp_packet_genl_family,
  1670. };
  1671. static void dp_unregister_genl(int n_families)
  1672. {
  1673. int i;
  1674. for (i = 0; i < n_families; i++)
  1675. genl_unregister_family(dp_genl_families[i]);
  1676. }
  1677. static int dp_register_genl(void)
  1678. {
  1679. int err;
  1680. int i;
  1681. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1682. err = genl_register_family(dp_genl_families[i]);
  1683. if (err)
  1684. goto error;
  1685. }
  1686. return 0;
  1687. error:
  1688. dp_unregister_genl(i);
  1689. return err;
  1690. }
  1691. static int __net_init ovs_init_net(struct net *net)
  1692. {
  1693. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1694. INIT_LIST_HEAD(&ovs_net->dps);
  1695. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1696. return 0;
  1697. }
  1698. static void __net_exit ovs_exit_net(struct net *net)
  1699. {
  1700. struct datapath *dp, *dp_next;
  1701. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1702. ovs_lock();
  1703. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1704. __dp_destroy(dp);
  1705. ovs_unlock();
  1706. cancel_work_sync(&ovs_net->dp_notify_work);
  1707. }
  1708. static struct pernet_operations ovs_net_ops = {
  1709. .init = ovs_init_net,
  1710. .exit = ovs_exit_net,
  1711. .id = &ovs_net_id,
  1712. .size = sizeof(struct ovs_net),
  1713. };
  1714. static int __init dp_init(void)
  1715. {
  1716. int err;
  1717. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1718. pr_info("Open vSwitch switching datapath\n");
  1719. err = ovs_flow_init();
  1720. if (err)
  1721. goto error;
  1722. err = ovs_vport_init();
  1723. if (err)
  1724. goto error_flow_exit;
  1725. err = register_pernet_device(&ovs_net_ops);
  1726. if (err)
  1727. goto error_vport_exit;
  1728. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1729. if (err)
  1730. goto error_netns_exit;
  1731. err = dp_register_genl();
  1732. if (err < 0)
  1733. goto error_unreg_notifier;
  1734. return 0;
  1735. error_unreg_notifier:
  1736. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1737. error_netns_exit:
  1738. unregister_pernet_device(&ovs_net_ops);
  1739. error_vport_exit:
  1740. ovs_vport_exit();
  1741. error_flow_exit:
  1742. ovs_flow_exit();
  1743. error:
  1744. return err;
  1745. }
  1746. static void dp_cleanup(void)
  1747. {
  1748. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1749. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1750. unregister_pernet_device(&ovs_net_ops);
  1751. rcu_barrier();
  1752. ovs_vport_exit();
  1753. ovs_flow_exit();
  1754. }
  1755. module_init(dp_init);
  1756. module_exit(dp_cleanup);
  1757. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1758. MODULE_LICENSE("GPL");