datapath.c 51 KB

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