datapath.c 53 KB

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