datapath.c 51 KB

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